我使用的是从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()