Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

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

like image 986
Bin Chen Avatar asked Sep 05 '25 03:09

Bin Chen


1 Answers

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.

like image 200
Jeet Avatar answered Sep 07 '25 15:09

Jeet