【问题标题】:Python - How to do non-blocking read from PIPE in subprocess.Popen?Python - 如何在 subprocess.Popen 中从 PIPE 进行非阻塞读取?
【发布时间】: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


【解决方案1】:

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

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

如果你不想要这种行为,你必须设置你想要阅读的大小。尝试将其设置为 1 个字节?

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

【讨论】:

  • 已尝试设置读取大小。问题依旧。差点想放弃转pexpect
  • 某些程序在通过pexpect 执行时可能会表现不同,因为程序可以检测它们是否在终端(TTY 设备)中运行。不知道Java程序是否可以做到这一点。例如,如果我在我的系统上运行ps auxps 检测到终端只有 80 个字符宽,并相应地缩短行。如果我们将输出通过管道传输到文件中(例如使用子进程),它将不知道终端的宽度是多少,因此不会缩短行。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-09-27
相关资源
最近更新 更多