通过Popen调用的程序是一个命令行程序。我使用一个线程从队列中读取一个项目并将其发送到stdin,并从stdout获取响应。但它在proc.stdout.read()中挂起。我确实看到它在上周五的预期输出中正常工作,然后当我今天做了一些更改时,它会挂起。我所做的更改将使用readlines()取代read()并使用循环来迭代结果。我知道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('