使用子流程模块并行工作(多流程) [英] Using subprocess module to work in parallel (Multi-process)
问题描述
Python中的多处理新手,请考虑您具有以下功能:
New to multiprocessing in python, consider that you have the following function:
def do_something_parallel(self):
result_operation1 = doit.main(A,B)
do_something_else(C)
现在的关键是我希望doit.main
在另一个进程中运行并且无阻塞,因此do_something_else中的代码将在另一个进程中第一个启动后立即运行.
Now the point is that I want the doit.main
to run in another process and to be non blocking, so the code in do_something_else will run immediately after the first has been launched in another process.
- 如何使用python
subprocess
模块来做到这一点? - 子处理与创建另一个新的处理之间是否有区别,为什么我们需要另一个处理的子处理?
- How can I do it using python
subprocess
module? - Is there a difference between subprocessing and creating new process aside to another one, why would we need a child processes of other process?
注意:我不想在这里使用多线程方法.
Note: I do not want to use multithreaded approach here..
编辑:我想知道是否禁止在同一功能中使用subprocess module
和multiprocess module
吗?
我想要这样做的原因是我有两件事要运行:首先是一个exe文件,其次是一个函数,每一个都需要它自己的进程.
EDIT: I wondered whether using a subprocess module
and multiprocess module
in the same function is prohibited?
Reason I want this is that I have two things to run: first an exe file, and second a function, each needs it own process.
推荐答案
如果要在单独的进程中运行Python代码,则可以使用 multiprocessing
模块:
If you want to run a Python code in a separate process, you could use multiprocessing
module:
import multiprocessing
if __name__ == "__main__":
multiprocessing.Process(target=doit.main, args=[A, B]).start()
do_something_else() # this runs immmediately without waiting for main() to return
我想知道是否禁止在同一功能中使用子进程模块和多进程模块?
I wondered whether using a subprocess module and multiprocess module in the same function is prohibited?
不.您可以在同一函数中同时使用subprocess
和multiprocessing
(此外,multiprocessing
可能会使用subprocess
在内部启动其工作进程).
No. You can use both subprocess
and multiprocessing
in the same function (moreover, multiprocessing
may use subprocess
to start its worker processes internally).
原因是我要运行两件事:第一是exe文件,第二是函数,每一个都需要它自己的进程.
Reason I want this is that I have two things to run: first an exe file, and second a function, each needs it own process.
您不需要multprocessing
即可在不阻塞的情况下运行外部命令(显然是在其自身的过程中); subprocess.Popen()
就足够了:
You don't need multprocessing
to run an external command without blocking (obviously, in its own process); subprocess.Popen()
is enough:
import subprocess
p = subprocess.Popen(['command', 'arg 1', 'arg 2'])
do_something_else() # this runs immediately without waiting for command to exit
p.wait() # this waits for the command to finish
这篇关于使用子流程模块并行工作(多流程)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!