Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write to a Python subprocess's stdin without communicate()'s blocking behavior

How do I make this a non-blocking call? osd_cat accepts input only as a PIPE which need p.communicate() call making the process to block. Is there any other way to set stdin in Popen?

p = subprocess.Popen(('osd_cat',
                      '-d',
                      '{}'.format(interval)),
                     stdin=subprocess.PIPE)
p.communicate(message)
like image 410
Jean Avatar asked Oct 18 '25 06:10

Jean


1 Answers

The p.communicate method is a one-shot deal in terms of sending data to the process.

Instead, write directly to p.stdin. If you want to get output, you can read lines from p.stdout. Make sure you pass stdout=subprocess.PIPE to the constructor before attempting to read.

like image 166
Mad Physicist Avatar answered Oct 20 '25 20:10

Mad Physicist



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!