用子进程生成进程 [英] spawning a process with subprocess

查看:70
本文介绍了用子进程生成进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我已经多次使用子进程2.4来执行一个进程,等待

来完成它,然后看看它的输出。现在我想单独生成

的过程,稍后检查一下它是否已经完成,如果它是b / b $ b $看看它的输出。我可能想在某个时候发送一个信号给b
杀死这个过程。这看起来很简单,但似乎没有工作。


这是我的测试用例:

导入子流程,时间


cmd =" cat somefile"

thread = subprocess.Popen(args = cmd.split(), shell = True,

stdout = subprocess.PIPE,stdin = subprocess.PIPE,

stderr = subprocess.STDOUT,close_fds = True)


while(1):

time.sleep(1)

if(thread.returncode):

break

else:

print thread.returncode


print" returncode =" ;, thread.returncode

for line in thread.stdout:

print" stdout:\t",line

这将直接打印None的返回码,直到我按Ctrl-C为止。


当然,如果我调用thread.communicate(),程序运行正常,但

因为等待进程完成,那就是不是我想要的。


任何帮助都会很感激iated。

解决方案

11月26日上午10点54分,bhunter< brian.p.hun ... @ gmail。编写:





我已经多次使用子进程2.4执行进程,等待

完成它,然后看看它的输出。现在我想单独生成

的过程,稍后检查一下它是否已经完成,如果它是b / b $ b $看看它的输出。我可能想在某个时候发送一个信号给b
杀死这个过程。这看起来很简单,但似乎没有工作。


这是我的测试用例:

导入子流程,时间


cmd =" cat somefile"

thread = subprocess.Popen(args = cmd.split(), shell = True,

stdout = subprocess.PIPE,stdin = subprocess.PIPE,

stderr = subprocess.STDOUT,close_fds = True)


while(1):

time.sleep(1)

if(thread.returncode):

break

else:

print thread.returncode


print" returncode =" ;, thread.returncode

for line in thread.stdout:

print" stdout:\t",line


这将只打印None的返回码,直到我按Ctrl键-C它。


当然,如果我调用thread.communicate(),程序工作正常,但

因为等待进程完成,这不是我想要的。


任何帮助将不胜感激。



我读过这种事情可能会很痛苦。我确定有人会发布b
并发表其他观点。我使用

Python的线程模块取得了一些成功。这里有一个非常好的演练

(在其示例中使用wxPython):

http://wiki.wxpython.org/LongRunningTasks


其他景点包括:

http://aspn.activestate.com/ASPN/ Coo ... / Recipe / 491281
http: //uucode.com/texts/pylongopgui/pyguiapp.html
http://sayspy.blogspot.com/2007/11/i...ncurrency.html


如果我我正在做这样的事情,我会把这个过程写成

它输出到一个文件并定期检查文件是否有

数据。


希望s很快就会有更多的知识。


Mike


我读过这样的事情可能是一个痛。我相信有人会


发布并有其他观点。我使用

Python的线程模块取得了一些成功。这里有一个非常好的演练

(在其示例中使用wxPython):

http://wiki.wxpython.org/LongRunningTasks


其他景点包括:

http://aspn.activestate.com/ASPN/Coo...ncurrency.html


如果我正在做这样的事情,我会让这个过程写成

它输出到一个文件并定期检查文件是否有

数据。


希望有更多知识的人很快就会出现。


Mike


Darn。穿线是唯一的方法吗?我希望没有

来避免这种情况。本来以为

子流程可能有办法自动处理。


感谢您的帮助,

Brian


11月26日下午12:27,bhunter< brian.p.hun ... @ gmail.comwrote:


我读过这种事情可能会很痛苦。我确定有人会发布b
并发表其他观点。我使用

Python的线程模块取得了一些成功。这里有一个非常好的演练

(在其示例中使用wxPython):

http://wiki.wxpython.org/LongRunningTasks


其他景点包括:

http://aspn.activestate.com/ASPN/Coo.../491281http:// ...


如果我正在做这样的事情,我会让进程写出

它输出到文件并定期检查文件是否有

数据。


希望有更多知识的人很快就会出现。


Mike



Darn。穿线是唯一的方法吗?我希望没有

来避免这种情况。本来以为

子流程可能有办法自动处理。


感谢您的帮助,

Brian



这就是我这样做的方式......正如我所说,可能还有一些

其他人将会有其他意见。顺便说一句,

你的陈述我希望不必避免这种情况。意味着你希望使用线程...
希望使用线程...我认为这与你的b $ b意味着什么相矛盾。


Mike


Hi,

I''ve used subprocess with 2.4 several times to execute a process, wait
for it to finish, and then look at its output. Now I want to spawn
the process separately, later check to see if it''s finished, and if it
is look at its output. I may want to send a signal at some point to
kill the process. This seems straightforward, but it doesn''t seem to
be working.

Here''s my test case:

import subprocess, time

cmd = "cat somefile"
thread = subprocess.Popen(args=cmd.split(), shell=True,
stdout=subprocess.PIPE, stdin=subprocess.PIPE,
stderr=subprocess.STDOUT, close_fds=True)

while(1):
time.sleep(1)
if(thread.returncode):
break
else:
print thread.returncode

print "returncode = ", thread.returncode
for line in thread.stdout:
print "stdout:\t",line
This will just print the returncode of None forever until I Ctrl-C it.

Of course, the program works fine if I call thread.communicate(), but
since this waits for the process to finish, that''s not what I want.

Any help would be appreciated.

解决方案

On Nov 26, 10:54 am, bhunter <brian.p.hun...@gmail.comwrote:

Hi,

I''ve used subprocess with 2.4 several times to execute a process, wait
for it to finish, and then look at its output. Now I want to spawn
the process separately, later check to see if it''s finished, and if it
is look at its output. I may want to send a signal at some point to
kill the process. This seems straightforward, but it doesn''t seem to
be working.

Here''s my test case:

import subprocess, time

cmd = "cat somefile"
thread = subprocess.Popen(args=cmd.split(), shell=True,
stdout=subprocess.PIPE, stdin=subprocess.PIPE,
stderr=subprocess.STDOUT, close_fds=True)

while(1):
time.sleep(1)
if(thread.returncode):
break
else:
print thread.returncode

print "returncode = ", thread.returncode
for line in thread.stdout:
print "stdout:\t",line

This will just print the returncode of None forever until I Ctrl-C it.

Of course, the program works fine if I call thread.communicate(), but
since this waits for the process to finish, that''s not what I want.

Any help would be appreciated.

I''ve read that this sort of thing can be a pain. I''m sure someone will
post and have other views though. I have had some success using
Python''s threading module though. There''s a pretty good walkthrough
here (it uses wxPython in its example):

http://wiki.wxpython.org/LongRunningTasks

Other places of interest include:

http://aspn.activestate.com/ASPN/Coo.../Recipe/491281
http://uucode.com/texts/pylongopgui/pyguiapp.html
http://sayspy.blogspot.com/2007/11/i...ncurrency.html

If I were doing something like this, I would have the process write
it''s output to a file and periodically check to see if the file has
data.

Hopefully someone with more knowledge will come along soon.

Mike


I''ve read that this sort of thing can be a pain. I''m sure someone will

post and have other views though. I have had some success using
Python''s threading module though. There''s a pretty good walkthrough
here (it uses wxPython in its example):

http://wiki.wxpython.org/LongRunningTasks

Other places of interest include:

http://aspn.activestate.com/ASPN/Coo...ncurrency.html

If I were doing something like this, I would have the process write
it''s output to a file and periodically check to see if the file has
data.

Hopefully someone with more knowledge will come along soon.

Mike

Darn. Is threading the only way to do it? I was hoping not to have
to avoid that. Would have thought that there might be a way for
subprocess to handle this automatically.

Thanks for your help,
Brian


On Nov 26, 12:27 pm, bhunter <brian.p.hun...@gmail.comwrote:

I''ve read that this sort of thing can be a pain. I''m sure someone will
post and have other views though. I have had some success using
Python''s threading module though. There''s a pretty good walkthrough
here (it uses wxPython in its example):

http://wiki.wxpython.org/LongRunningTasks

Other places of interest include:

http://aspn.activestate.com/ASPN/Coo.../491281http://...

If I were doing something like this, I would have the process write
it''s output to a file and periodically check to see if the file has
data.

Hopefully someone with more knowledge will come along soon.

Mike


Darn. Is threading the only way to do it? I was hoping not to have
to avoid that. Would have thought that there might be a way for
subprocess to handle this automatically.

Thanks for your help,
Brian

This is just the way I do it...as I said, there are probably some
other people in the group who will have other opinions. By the way,
your statement "I was hoping not to have to avoid that" means that you
hoped to use threading...which I think is contradictory to what you
meant.

Mike


这篇关于用子进程生成进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆