[英]Execute two functions concurrently using asyncio
我现在有一个设置,其中我打开一个子,我要读这两个stdout
和stderr
在同一时间,所以调用子后,我催生一个新的线程stdout
,只是手柄stderr
在主线程。
# imports
from subprocess import Popen, PIPE
from threading import Thread
def handle_stdout(stdout):
# ... do something with stdout,
# not relevant to the question
pass
def my_fn():
proc = Popen([...], stdout=PIPE, stderr=PIPE)
Thread(target=lambda: handle_stdout(proc.stdout)).start()
# ... handle stderr
print(proc.stderr.read())
proc.wait()
proc.kill()
my_fn()
有没有办法使用 asyncio 实现同样的目标?
代码的无线asyncio
版本可能如下所示:
import asyncio
import asyncio.subprocess
async def handle_stdout(stdout):
while True:
line = await stdout.readline() # Possibly adding .decode() to get str
if not line:
break
# In 3.8 four lines above can be replaced with just:
# while line := await stdout.readline(): # Yay walrus operator!
# ... do stuff with line ...
async def my_fn():
# Note: No list wrapping on command line arguments; all positional arguments are part of the command
proc = await asyncio.create_subprocess_exec(..., stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE)
stdout_task = asyncio.create_task(handle_stdout(proc.stdout))
# ... handle stderr
print(await proc.stderr.read())
await stdout_task
await proc.wait()
if __name__ == '__main__':
asyncio.run(my_fn())
API 略有不同,异步函数实际上是在您从它们中创建任务时调用的(线程必须执行未调用的函数),并且您需要小心地await
所有异步操作,但这并没有什么不同。 主要问题是async
的病毒性质; 由于您只能在async
函数中await
,因此很难从非异步代码调用异步代码(反之亦然,只要非异步代码不会因任何原因阻塞)。 它使异步代码库在很大程度上与非async
内容不兼容,并使零碎的转换几乎不可能,但对于全新的代码,它工作正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.