写入到Python子进程的stdin中,而没有communication()的阻塞行为 [英] Write to a Python subprocess's stdin without communicate()'s blocking behavior

查看:120
本文介绍了写入到Python子进程的stdin中,而没有communication()的阻塞行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将其设为无障碍通话? osd_cat仅接受输入作为PIPE,这需要调用p.communicate()来使进程阻塞.还有其他方法可以在Popen中设置stdin吗?

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)

推荐答案

p.communicate 方法是一次性的交易.

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

相反,直接写到 p.stdin .如果要获取输出,可以从 .尝试读取之前,请确保将stdout=subprocess.PIPE传递给构造函数.

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.

这篇关于写入到Python子进程的stdin中,而没有communication()的阻塞行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆