python multiprocessing无法控制多个长时间运行的控制台exe?
我是Python新手。我最近尝试使用Python脚本来调用控制台exe,这是一个需要很长时间的过程。我将允许 exe 在 CPU 允许的情况下被调用多次。当 exe 完成其工作时。它应该将 CPU 释放给其他新作业。所以我想我可能需要多进程控制机制。
由于多处理只能调用Python可调用函数。它不能直接调用控制台exe。我将 subprocess.Popen(cmd) 包装在 python 函数中。但是,在我这样做之后,我发现在使用 multiprocessing.Process.start() 之前。 exe已经启动了。问题是在它完成正在做的事情之前(需要很长时间),它不会将控制权归还给我。程序被冻结等待。那不是我想要的。
我发布的代码如下:
import sys
import os
import multiprocessing
import subprocess
def subprocessExe(cmd):
return subprocess.call(cmd, shell=False, stdout=subprocess.PIPE, \
stderr=subprocess.PIPE,creationflags=0x08000000)
if __name__ == '__main__':
p = multiprocessing.Process(target=self.subprocessExe(exeFileName))
p.start()
print p, p.is_alive()
感谢您的宝贵时间!
I am a newbie in Python. I recently tried to use Python script to call a console exe which is a process need long time. I will allow the exe being called as many times as the CPU can permit. And when the exe has finish its job. It should release the CPU to other new jobs. So I think I may need the multiple process control mechanism.
Since multiprocessing can only call python callable function. It can not directly call the console exe. I wrapped the subprocess.Popen(cmd) in a python function. However, after I did so, I found that before multiprocessing.Process.start() is used. The exe has already started. And problem is before it finish what it is doing (need long time), it does not return me the control. The program is freezed to wait. That is not what I want.
I am posting the codes as below:
import sys
import os
import multiprocessing
import subprocess
def subprocessExe(cmd):
return subprocess.call(cmd, shell=False, stdout=subprocess.PIPE, \
stderr=subprocess.PIPE,creationflags=0x08000000)
if __name__ == '__main__':
p = multiprocessing.Process(target=self.subprocessExe(exeFileName))
p.start()
print p, p.is_alive()
Thank you for your time!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您创建
multiprocessing.Process
对象时,您将调用subprocessExec
。你应该这样做:然后它应该可以工作。
You're calling
subprocessExec
when you create themultiprocessing.Process
object. You should instead do this:And then it should work.
您的测试用例中有很多错误的地方。以下工作对我来说:
或
或
There are number of things that are wrong in your test case. The following work for me:
OR
OR