相关疑难解决方法(0)

python中subprocess.PIPE上的非阻塞读取

我正在使用子进程模块启动子进程并连接到它的输出流(stdout).我希望能够在其标准输出上执行非阻塞读取.有没有办法让.readline非阻塞或在我调用之前检查流上是否有数据.readline?我希望这是可移植的,或至少在Windows和Linux下工作.

这是我现在如何做到的(.readline如果没有数据可用,则阻止它):

p = subprocess.Popen('myprogram.exe', stdout = subprocess.PIPE)
output_str = p.stdout.readline()
Run Code Online (Sandbox Code Playgroud)

python io subprocess nonblocking

477
推荐指数
15
解决办法
21万
查看次数

python非块读取文件

我想读取非块模式的文件.所以我在下面做了

import fcntl
import os

fd = open("./filename", "r")
flag = fcntl.fcntl(fd.fileno(), fcntl.F_GETFD)
fcntl.fcntl(fd, fcntl.F_SETFD, flag | os.O_NONBLOCK)
flag = fcntl.fcntl(fd, fcntl.F_GETFD)
if flag & os.O_NONBLOCK:
    print "O_NONBLOCK!!"
Run Code Online (Sandbox Code Playgroud)

但价值flag仍然代表0.为什么......?我想我应该根据改变os.O_NONBLOCK

当然,如果我调用fd.read(),它会在read()处被阻塞.

python nonblocking python-2.7

6
推荐指数
1
解决办法
1万
查看次数

标签 统计

nonblocking ×2

python ×2

io ×1

python-2.7 ×1

subprocess ×1