Watch stdout and stderr of a subprocess simultaneously

前端 未结 2 1550
无人及你
无人及你 2020-12-19 03:29

How can I watch standard output and standard error of a long-running subprocess simultaneously, processing each line as soon as it is generated by the subprocess?

I

2条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-19 04:11

    It has occurred to me that there is actually a simpler solution to the problem, at least if the watching code is such that it doesn't need to be in a single coroutine invocation.

    What you can do is spawn two separate coroutines, one for stdout and one for stderr. Running them in parallel will give you the needed semantics, and you can use gather to await their completion:

    def watch(stream, prefix=''):
        async for line in stream:
            print(datetime.now(), prefix, line.decode().strip())
    
    async def run(cmd):
        p = await asyncio.create_subprocess_shell(cmd, stdout=PIPE, stderr=PIPE)
        await asyncio.gather(watch(p.stdout), watch(p.stderr, 'E:'))
    

提交回复
热议问题