Python - 如何在 subprocess.Popen 中从 PIPE 进行非阻塞读取?

标签 python pipe subprocess

我发布了类似的question上周,这篇文章反射(reflect)了我的试验和我现在面临的问题。

通过Popen调用的程序是一个命令行程序。我使用一个线程从队列中读取一项并将其发送到标准输入并从标准输出获取响应。但是它挂起proc.stdout.read()中。上周五我确实看到它按照预期输出运行良好,然后当我今天做了一些更改时,它挂起了。我所做的更改是将 read() 替换为 readlines() 并使用循环来迭代结果。我知道 readlines() 可能会阻塞,但是当我上周五使用 read() 将代码反转到原来的位置时,它也会阻塞。我现在完全迷失了。有什么可能的原因吗?

下面是从队列中获取一个句子并将其提供给java程序以获得响应的代码:

''' below is the code for worker thread. '''
def readQueue(proc, queue):
    print 'enter queueThread.\n'
    global notEmpty
    notEmpty = True
    while notEmpty:
        try:
            sen = queue.get()
            proc.stdin.write(sen.strip())
            res = proc.stdout.read()
            print res.strip(), ' ', sen
            queue.task_done()
        except Empty:
            break
    print 'leave queueThread.'

下面的主线程是从文件中读取每一行并将其放入队列中,供工作线程逐项处理:

def testSubprocess():
    ee = open('sentences.txt', 'r')
    #ff = open('result.txt', 'w')   # print it to stdout first before really write to a file. 
    lines = ee.readlines()
    cmd = ['java', 
           '-cp', 'someUsefulTools.jar', 
           'className', 
           '-stdin',]   # take input from stdin
    proc = Popen(cmd, stdout=PIPE, stdin=PIPE, stderr=PIPE, bufsize=-1, universal_newlines=True)
    q = Queue()
    for sen in lines:
        q.put(sen.strip())
    readThread = Thread(target=readQueue, args=(proc, q))
    readThread.daemon = True
    readThread.start()
    print 'Main thread is waiting...\n'
    q.join()
    global notEmpty; notEmpty = False
    print 'Done!'

最佳答案

子进程中的管道是类似文件的对象。

如果您没有指定应该读取多少内容,这些对象上的方法 read() 会将所有内容读取到内存中,直到达到 EOF,请参阅文档:https://docs.python.org/2/library/stdtypes.html#file.read .

如果您不希望出现此行为,则必须设置要读取的大小。尝试将其设置为 1 字节?

readlines() 也是如此,请参阅文档:https://docs.python.org/2/library/stdtypes.html#file.readlines

关于Python - 如何在 subprocess.Popen 中从 PIPE 进行非阻塞读取?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29489983/

相关文章:

python - 将非 ascii 字符转换为 ascii 字符

python 为 windows 启用 gdb

linux - 如何使用管道命令显示文件中的属性?

python - 子进程 cp 返回错误 - bufsize 必须是整数

java - 使用 sh.Command 从 python 调用 Java 时执行格式错误

带有变量的 Python 子进程调用

python - 如何从其键是引用字典子集的嵌套字典创建平面字典?

python - 使用 Dask 加载多个 CSV 文件时混合列

java - Java 中的命令行管道输入

c - 二进制模式下的 Windows 管道