gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 17:48:05 26 4
gpt4 key购买 nike

我发布了类似的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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com