尝试在 python 中的文件对象上调用 readline() 但它暂停
我使用 readline() 函数从通过 subprocess 模块获取的文件对象中读取数据:proc = subprocess.Popen(cmd, bufsize=0, stdout=subprocess.PIPE)
。这允许我将 proc.stdout 作为类似文件的对象与 proc.stdout.readline() 一起使用。我的问题是,这会暂停等待输入,如果我进行 readline 调用时没有输入,我希望它超时并继续。我正在运行 Python 2.4,如何让 readline 方法停止暂停?谢谢。
I'm using the readline() function to read data from a file object obtained through the subprocess module: proc = subprocess.Popen(cmd, bufsize=0, stdout=subprocess.PIPE)
. This allows me to use proc.stdout
as a file-like object with proc.stdout.readline()
. My issue is that this pauses waiting for input and I'd like it to time out and move on if there isn't input there when I make the readline call. I'm running Python 2.4, how can I get the readline method to stop pausing? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 posix-y 平台(基本上是除 Windows 之外的任何流行平台)上, select 模块为此目的提供了正确的工具。不幸的是,在 Windows 上,
select
仅适用于套接字(不适用于subprocess.Popen
将使用的管道),因此情况不太清楚。您需要在 Windows 上运行吗...?如果没有,只需在
select.select
调用中使用子进程对象p
的p.stdout.fileno()
并设置短超时 - - 这真的很容易!编辑:这是一个简单的例子(当然假设需要导入):
请注意,没有办法“等待完整的行”:这会等待“任何输出”(然后阻塞,直到所有输出已准备好)。要阅读可用的内容,请使用 fcntl 来设置文件描述符上的 os.O_NODELAY (什么
fileno( )
返回) 在开始循环之前。On a posix-y platform (basically any popular platform except Windows), the select module offers the right tools for this purpose. Unfortunately, on Windows,
select
only works on sockets (not on pipes, which is whatsubprocess.Popen
will be using), so the situation is not quite as clear there. Do you need to run on Windows...?If not, just use the
p.stdout.fileno()
of your subprocess objectp
in aselect.select
call with a short timeout -- it's really easy!Edit: here's a simple example (assuming the needed imports of course):
Note there is no way to "wait for a complete line": this waits for "any output at all" (then blocks until all the output is ready). To read just what's available, use fcntl to set os.O_NODELAY on the file descriptor (what
fileno()
returns) before you start looping.