开发者

Python: How to be notified when the subprocess is ended opened by Popen

开发者 https://www.devze.com 2023-01-12 22:36 出处:网络
I am using Popen t开发者_如何学Pythono run a command but I don\'t know how I can write a callback that gets called once the command is finished. Any idea?

I am using Popen t开发者_如何学Pythono run a command but I don't know how I can write a callback that gets called once the command is finished. Any idea?

Thanks. Bin


You can call communicate():

 p = subprocess.Popen('find . -name "*.txt"', stdout=subprocess.PIPE, stderr=subprocess.PIPE)
 stdout, stderr = p.communicate()

You can also call wait(), but this might cause problems if the child process fills its output buffer.


You could use p.poll() method of the Popen object.

http://docs.python.org/library/subprocess.html#subprocess.Popen.poll

0

精彩评论

暂无评论...
验证码 换一张
取 消