Python 多处理
这个问题更多的是事实发现和思考过程,而不是面向代码。
我有许多已编译的 C++ 程序,需要在不同时间并使用不同参数运行。我正在考虑使用 Python 多处理从作业队列 (rabbitmq) 读取作业,然后将该作业提供给 C++ 程序来运行(可能是子进程)。我正在研究多处理模块,因为这将全部在双 Xeon 服务器上运行,所以我想充分利用我的服务器的多处理器能力。
Python 程序将是中央管理器,只需从队列中读取作业,使用适当的 C++ 程序生成一个进程(或子进程?)来运行作业,获取结果(子进程 stdout 和 stderr),将其提供给回调并将进程放回进程队列中,等待下一个作业运行。
首先,这听起来像是一个有效的策略吗?
其次,有没有类似的例子?
先感谢您。
This question is more fact finding and thought process than code oriented.
I have many compiled C++ programs that I need to run at different times and with different parameters. I'm looking at using Python multiprocessing to read a job from job queue (rabbitmq) and then feed that job to a C++ program to run (maybe subprocess). I was looking at the multiprocessing module because this will all run on dual Xeon server so I want to take full advantage of the multiprocessor ability of my server.
The Python program would be the central manager and would simply read jobs from the queue, spawn a process (or subprocess?) with the appropriate C++ program to run the job, get the results (subprocess stdout & stderr), feed that to a callback and put the process back in a queue of processes waiting for the next job to run.
First, does this sound like a valid strategy?
Second, are there any type of examples of something similar to this?
Thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为此,您不需要
multiprocessing
模块。multiprocessing
模块非常适合将 Python 函数作为单独的进程运行。要运行 C++ 程序并从 stdout 读取结果,您只需要subprocess
模块。队列可以是一个列表,当列表非空时,您的 Python 程序将简单地循环。但是,如果您想
生成 C++ 程序(并行)
要将新项目放入队列中
,您可以使用
multiprocessing
来完成,如下所示:test.py:
test2.py (一个简单的替代品你的 C++ 程序):
运行
test.py
可能会产生如下结果:请注意,右侧列中的数字被反馈到队列中,并(最终)用作
test2.py
并显示为数字左硬列。You don't need the
multiprocessing
module for this. Themultiprocessing
module is good for running Python functions as separate processes. To run a C++ program and read results from stdout, you'd only need thesubprocess
module. The queue could be a list, and your Python program would simply loop while the list is non-empty.However, if you want to
spawn C++ programs (in parallel)
to put new items in the queue
then you could do it with
multiprocessing
like this:test.py:
test2.py (a simple substitute for your C++ program):
Running
test.py
might yield something like this:Notice that the numbers in the right-hand column are fed back into the queue, and are (eventually) used as arguments to
test2.py
and show up as numbers in the left-hard column.是的。
芹菜
Yes.
Celery
听起来是一个不错的策略,但您不需要
multiprocessing
模块,而是需要subprocess
模块。subprocess
用于从 Python 程序运行子进程并与它们交互(stdio、stdout、管道等),而multiprocessing
更多的是关于分发 Python 代码 在多个进程中运行以通过并行性获得性能。根据响应策略,您可能还需要查看
线程
以从线程启动子进程。这将允许您等待一个子进程,同时仍然响应队列以接受其他作业。Sounds like a good strategy, but you don't need the
multiprocessing
module for it, but rather thesubprocess
module.subprocess
is for running child processes from a Python program and interacting with them (stdio, stdout, pipes, etc.), whilemultiprocessing
is more about distributing Python code to run in multiple processes to gain performance through parallelism.Depending on the responsiveness strategy, you may also want to look at
threading
for launching subprocesses from a thread. This will allow you to wait on one subprocess while still being responsive on the queue to accept other jobs.