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

走远了吗. 提交于 2020-04-16 07:12:39

问题


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)

回答1:


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.



来源:https://stackoverflow.com/questions/56776870/write-to-a-python-subprocesss-stdin-without-communicates-blocking-behavior

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!