确定os.system()是否完成 [英] determine if os.system() is done

查看:444
本文介绍了确定os.system()是否完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我正在调用两个系统进程,一个用于解压缩,一个用于

a ?? taila ??得到最后一行。如何确定第一个

流程何时完成?


示例:


subprocess.Popen([ r" / sw / bin / gzip"," -d"," access_log.4.gz"]);


last_line = subprocess.Popen([r" / usr / bin / tail"," -n 1"," access_log.4"],

stdout = subprocess.PIPE).communicate()[0]


当然,我可以尝试类似os.system的解决方法(gzip -d

thiss.gz&&& tail thiss),但我想知道是否有'非黑客的方式来确定系统进程何时完成。


Xah
xa*@xahlee.org

a ?? http://xahlee.org/

suppose i''m calling two system processes, one to unzip, and one to
a??taila?? to get the last line. How can i determine when the first
process is done?

Example:

subprocess.Popen([r"/sw/bin/gzip","-d","access_log.4.gz"]);

last_line=subprocess.Popen([r"/usr/bin/tail","-n 1","access_log.4"],
stdout=subprocess.PIPE).communicate()[0]

of course, i can try workarounds something like os.system("gzip -d
thiss.gz && tail thiss"), but i wish to know if there''s non-hack way to
determine when a system process is done.

Xah
xa*@xahlee.org
a?? http://xahlee.org/

推荐答案

Xah Lee写道:
Xah Lee wrote:
当然,我可以尝试类似os.system的解决方法(gzip -d
thiss.gz& ;& tail thiss),但我想知道是否有非黑客的方法来确定系统进程何时完成。
of course, i can try workarounds something like os.system("gzip -d
thiss.gz && tail thiss"), but i wish to know if there''s non-hack way to
determine when a system process is done.




好吧,如果你使用popen的功能一家人,你从输出的子流程中得到某种

的返回值。管。你应该能够通过检查(解析)这个

输出管来确定你的子进程是否终止。


如果你使用os.system(),你应该得到一个可用于此任务的返回值(通常为None

或错误代码)。


在这两种情况下,您可能必须使用循环(sleep(x))等待代码中返回

值(并检查它)。


有关详细信息,请查看os.process模块​​的文档。 (也许

更新的子流程模块是更好的选择...)


HTH


-----------------------------------

Alessandro Bottoni



Well, if you use a function of the "popen" family, you get some kind of
return value from the subprocess on your "output" pipe. You should be able
to determine if your subprocess has terminated by examining (parsing) this
output pipe.

If you use os.system(), you should get a single return value (usually "None"
or a error code) that you can use for this task.

In both cases, you may have to use a loop (sleep(x)) to wait for the return
value (and check it) from within your code.

Have a look at the docu of the os.process module for details. (Maybe the
newer "subprocess" module is a better choice...)

HTH

-----------------------------------
Alessandro Bottoni


" Xah Lee" < xa*@xahlee.org>写道:
"Xah Lee" <xa*@xahlee.org> writes:
假设我正在调用两个系统进程,一个用于解压缩,一个用于尾部以获取最后一行。如何确定第一个
过程何时完成?
示例:
subprocess.Popen([r" / sw / bin / gzip"," -d"," access_log.4.gz"]);
last_line = subprocess.Popen([r" / usr / bin / tail"," -n 1"," access_log.4"],
stdout = subprocess.PIPE).communicate() [0]
当然,我可以尝试类似os.system的解决方法(gzip -d
thiss.gz&&& tail thiss),但我想知道是否''用于确定系统进程何时完成的非黑客方式。
suppose i''m calling two system processes, one to unzip, and one to
tail to get the last line. How can i determine when the first
process is done? Example: subprocess.Popen([r"/sw/bin/gzip","-d","access_log.4.gz"]); last_line=subprocess.Popen([r"/usr/bin/tail","-n 1","access_log.4"],
stdout=subprocess.PIPE).communicate()[0] of course, i can try workarounds something like os.system("gzip -d
thiss.gz && tail thiss"), but i wish to know if there''s non-hack way to
determine when a system process is done.




您是否尝试过阅读子进程模块的手册?如果你看看你可以用Popen对象做什么,你可以*找到你的问题的答案。实际上,只要了解一下

的沟通()方法的确切语义,就可以解决你的问题。

-

Thomas Bellman,瑞典Link?ping大学Lysator计算机俱乐部

我拒绝与一场斗智斗勇! bellman @ lysator.liu.se

非武装人员。 !做爱 - Nicht Wahr!



Have you tried reading the manual for the subprocess module? You
just *might* find the answer to your question if you look at what
you can do with Popen objects. Actually, just learning about the
exact semantics of the communicate() method might be enought to
solve your problem.
--
Thomas Bellman, Lysator Computer Club, Link?ping University, Sweden
"I refuse to have a battle of wits with an ! bellman @ lysator.liu.se
unarmed person." ! Make Love -- Nicht Wahr!


Thomas Bellman写道:
Thomas Bellman wrote:
你有没有试过阅读手册subprocess module?
Have you tried reading the manual for the subprocess module?




han har f?rs?kt,men hans tourette tog?verhanden:

http://mail.python.org/pipermail/pyt...er/ 297642.html


< / F>



han har f?rs?kt, men hans tourette tog ?verhanden:

http://mail.python.org/pipermail/pyt...er/297642.html

</F>


这篇关于确定os.system()是否完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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