简体   繁体   中英

Why is the output of my subprocess not being printed?

Here is my best attempt at asynchronously reading stdin/stdout from a subprocess and printing it from Python:

import asyncio
import subprocess

from asyncio.subprocess import STDOUT, PIPE, DEVNULL


async def start_stream():
    return await asyncio.create_subprocess_shell(
        'watch ls /proc',
        stdout=PIPE,
        stderr=PIPE,
        limit=1024
    )


async def spawn():
    ev_proc = await start_stream()
    while True:
        stdout, stderr = await ev_proc.communicate()
        print(stdout, stderr)


if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(spawn())

Why is the print function not outputting anything?

Your watch process never terminates and communicate() waits for the process to terminate, therefore stdout never arrives in your script.
https://docs.python.org/3/library/asyncio-subprocess.html

coroutine communicate(input=None)

Interact with process: Send data to stdin. Read data from stdout and stderr, until end-of-file is reached. Wait for process to terminate.

Try the following code which was inspired by https://stackoverflow.com/a/24435988/2776376 . It uses pipe_data_received and then len > 16 is simply to prevent printing empty lines.

SubprocessProtocol.pipe_data_received(fd, data)

Called when the child process writes data into its stdout or stderr pipe. fd is the integer file descriptor of the pipe. data is a non-empty bytes object containing the data.

import asyncio

class SubprocessProtocol(asyncio.SubprocessProtocol):
    def pipe_data_received(self, fd, data):
        if fd == 1:
            text = data.decode()
            if len(text.strip()) > 16:
                print(text.strip())

    def process_exited(self):
        loop.stop()

loop = asyncio.get_event_loop()


ls = loop.run_until_complete(loop.subprocess_exec(
    SubprocessProtocol, 'watch', 'ls', '/proc'))
loop.run_forever()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM