通过单个程序通过subprocess.Popen方法运行3个python程序 [英] Running 3 python programs by a single program via subprocess.Popen method
问题描述
我试图通过运行单个python程序同时运行3个python程序
我在一个单独的python程序中使用以下脚本sample.py
Sample.py:
import subprocess
subprocess.Popen ['AppFlatRent.py'])
subprocess.Popen(['AppForSale.py'])
subprocess.Popen(['LandForSale.py'])
所有三个程序,包括python.py在同一个文件夹中。
错误:OSError:[Errno 2]没有这样的文件或目录
有人指导我如何使用subprocess.Popen方法?
解决方案可能有shebang缺少(
chmod + x
)。
您可以显式提供Python执行档:
#!/ usr / bin / env python
import inspect
import os
import sys
from subprocess import Popen
scripts = ['AppFlatRent.py','AppForSale.py','LandForSale。 py']
def realpath(filename):
dir = os.path.realpath(os.path.dirname(inspect.getsourcefile(realpath)))
return os。 path.join(dir,filename)
#开始子进程
processes = [Popen([sys.executable或'python',realpath(scriptname)])
在脚本]
#等待进程完成
在进程中的p:
p.wait()
上面假设脚本名称是相对于模块给定的。
考虑导入模块并运行相应的函数使用
threading
,multiprocessing
模块,而不是直接将其作为脚本运行。I am trying to run 3 python programs simultaneously by running a single python program
I am using the following script in a separate python program sample.py
Sample.py:
import subprocess subprocess.Popen(['AppFlatRent.py']) subprocess.Popen(['AppForSale.py']) subprocess.Popen(['LandForSale.py'])
All the three programs including python.py is in the same folder.
Error: OSError: [Errno 2] No such file or directory
Can someone guide me how can i do it using subprocess.Popen method?
解决方案There might be shebang missing (
#!..
) in some of the scripts or executable permission is not set (chmod +x
).You could provide Python executable explicitly:
#!/usr/bin/env python import inspect import os import sys from subprocess import Popen scripts = ['AppFlatRent.py', 'AppForSale.py', 'LandForSale.py'] def realpath(filename): dir = os.path.realpath(os.path.dirname(inspect.getsourcefile(realpath))) return os.path.join(dir, filename) # start child processes processes = [Popen([sys.executable or 'python', realpath(scriptname)]) for scriptname in scripts] # wait for processes to complete for p in processes: p.wait()
The above assumes that script names are given relative to the module.
Consider importing the modules and running corresponding functions concurently using
threading
,multiprocessing
modules instead of running them as scripts directly.这篇关于通过单个程序通过subprocess.Popen方法运行3个python程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!