Python subprocess PIPE blocking

夙愿已清 提交于 2019-12-12 02:54:29

问题


The subprocess will output several characters, and I want to send some response via stdin. The length of the output is predictable, so I wrote code like this:

p = Popen("myproc", shell = True, stdin = PIPE, stdout = PIPE)
p.stdout.read(1)

However, p.stdout.read(1) doesn't return even if the process had already output more than a character. How can I make it stop blocking after reading a byte sequence of the expected length?


回答1:


myproc may use a block-buffering mode when the standard output is redirected i.e., your parent Python won't see anything until the corresponding buffer in the child is flushed. See several ways to workaround it in this answer (and follow the corresponding links there).



来源:https://stackoverflow.com/questions/35573778/python-subprocess-pipe-blocking

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