【发布时间】:2015-04-07 11:00:49
【问题描述】:
我上周发布了类似的question,这篇帖子反映了我的试用和我现在面临的问题。
通过 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!'
【问题讨论】:
-
抱歉,如果我误解了您的代码并且这不起作用,但是我发现我可以阻止管道以这种方式阻塞。 (我对 python 中的线程没有很多经验)在定义“proc”后添加:“fcntl.fcntl(proc, fcntl.F_SETFL, os.O_NONBLOCK)”如果 proc 被定义为类型“管道” “这应该会阻止它。您可能需要导入 fcntl。
-
@Aphire 不确定我是否正确,似乎“fcntl”用于 linux/unix 环境,对吧? (我的工作环境是windows)
-
想象你给java程序发了一句话:你怎么知道什么时候停止读取响应:是always 10字节还是always i> 一行,还是要一直读到 EOF,即每个进程只能回答一个单个问题?
标签: python pipe subprocess