Asyncio subprocess.wait()无限期挂起

时间:2016-08-18 07:49:39

标签: python python-3.x subprocess python-asyncio

我使用的是从Python documentation派生的代码。但有时yield from proc.wait()只是无限期地挂起而不会产生任何结果。它有时会挂起,有时会起作用。我不知道问题究竟是什么!

它会生成输出或错误,但只是挂在wait()

import asyncio.subprocess
import sys

@asyncio.coroutine
def execute():
    code = 'uname -nmprs'

    # Create the subprocess, redirect the standard output into a pipe
    create = asyncio.create_subprocess_shell(code,
                                            stdout=asyncio.subprocess.PIPE)
    proc = yield from create

    # Read one line of output
    data = yield from proc.stdout.readline()
    line = data.decode('ascii').rstrip()

    # Wait for the subprocess exit
    yield from proc.wait()
    return line

loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
out = loop.run_until_complete(execute())
print("output: %s" % out)
loop.close()

0 个答案:

没有答案
相关问题