Bin Chen
Bin Chen

Reputation: 63309

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

I am using Popen to 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

Upvotes: 4

Views: 5265

Answers (2)

Cody Snider
Cody Snider

Reputation: 368

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

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

Upvotes: 2

Jeet
Jeet

Reputation: 39807

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.

Upvotes: 2

Related Questions