python multiprocessing无法控制多个长时间运行的控制台exe?

发布于 2024-11-09 21:01:18 字数 800 浏览 3 评论 0原文

我是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

遗忘曾经 2024-11-16 21:01:18

当您创建 multiprocessing.Process 对象时,您将调用 subprocessExec。你应该这样做:

p = multiprocessing.Process(target=subprocessExec, args=(exeFileName,))

然后它应该可以工作。

You're calling subprocessExec when you create the multiprocessing.Process object. You should instead do this:

p = multiprocessing.Process(target=subprocessExec, args=(exeFileName,))

And then it should work.

意犹 2024-11-16 21:01:18

您的测试用例中有很多错误的地方。以下工作对我来说:

import multiprocessing, subprocess

def subprocessExe(cmd):
   subprocess.call([cmd], shell=False)

p = multiprocessing.Process(target=subprocessExe, args=('/full/path/to/script.sh',))
p.start()

subprocess.call([cmd], shell=True)
p = multiprocessing.Process(target=subprocessExe, args=('script.sh',))

subprocess.Popen([cmd], shell=True, stdout=subprocess.PIPE, \
                               stderr=subprocess.PIPE)

There are number of things that are wrong in your test case. The following work for me:

import multiprocessing, subprocess

def subprocessExe(cmd):
   subprocess.call([cmd], shell=False)

p = multiprocessing.Process(target=subprocessExe, args=('/full/path/to/script.sh',))
p.start()

OR

subprocess.call([cmd], shell=True)
p = multiprocessing.Process(target=subprocessExe, args=('script.sh',))

OR

subprocess.Popen([cmd], shell=True, stdout=subprocess.PIPE, \
                               stderr=subprocess.PIPE)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文