导入子流程,时间 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, waitfor it to finish, and then look at its output. Now I want to spawnthe process separately, later check to see if it''s finished, and if itis look at its output. I may want to send a signal at some point tokill the process. This seems straightforward, but it doesn''t seem tobe working.Here''s my test case:import subprocess, timecmd = "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):breakelse:print thread.returncodeprint "returncode = ", thread.returncodefor line in thread.stdout:print "stdout:\t",lineThis will just print the returncode of None forever until I Ctrl-C it.Of course, the program works fine if I call thread.communicate(), butsince 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, waitfor it to finish, and then look at its output. Now I want to spawnthe process separately, later check to see if it''s finished, and if itis look at its output. I may want to send a signal at some point tokill the process. This seems straightforward, but it doesn''t seem tobe working.Here''s my test case:import subprocess, timecmd = "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.returncodeprint "returncode = ", thread.returncodefor line in thread.stdout: print "stdout:\t",lineThis will just print the returncode of None forever until I Ctrl-C it.Of course, the program works fine if I call thread.communicate(), butsince 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 willpost and have other views though. I have had some success usingPython''s threading module though. There''s a pretty good walkthroughhere (it uses wxPython in its example): http://wiki.wxpython.org/LongRunningTasksOther 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.htmlIf I were doing something like this, I would have the process writeit''s output to a file and periodically check to see if the file hasdata.Hopefully someone with more knowledge will come along soon.MikeI''ve read that this sort of thing can be a pain. I''m sure someone willpost and have other views though. I have had some success usingPython''s threading module though. There''s a pretty good walkthroughhere (it uses wxPython in its example): http://wiki.wxpython.org/LongRunningTasksOther places of interest include: http://aspn.activestate.com/ASPN/Coo...ncurrency.htmlIf I were doing something like this, I would have the process writeit''s output to a file and periodically check to see if the file hasdata.Hopefully someone with more knowledge will come along soon.MikeDarn. Is threading the only way to do it? I was hoping not to haveto avoid that. Would have thought that there might be a way forsubprocess to handle this automatically.Thanks for your help,BrianOn 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. MikeDarn. Is threading the only way to do it? I was hoping not to haveto avoid that. Would have thought that there might be a way forsubprocess to handle this automatically.Thanks for your help,BrianThis is just the way I do it...as I said, there are probably someother 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 youhoped to use threading...which I think is contradictory to what youmeant.Mike 这篇关于用子进程生成进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!