A non-blocking read on a subprocess.PIPE in Python

后端 未结 29 2670
醉酒成梦
醉酒成梦 2020-11-21 04:49

I\'m using the subprocess module to start a subprocess and connect to its output stream (standard output). I want to be able to execute non-blocking reads on its standard ou

29条回答
  •  借酒劲吻你
    2020-11-21 04:57

    Here is a module that supports non-blocking reads and background writes in python:

    https://pypi.python.org/pypi/python-nonblock

    Provides a function,

    nonblock_read which will read data from the stream, if available, otherwise return an empty string (or None if the stream is closed on the other side and all possible data has been read)

    You may also consider the python-subprocess2 module,

    https://pypi.python.org/pypi/python-subprocess2

    which adds to the subprocess module. So on the object returned from "subprocess.Popen" is added an additional method, runInBackground. This starts a thread and returns an object which will automatically be populated as stuff is written to stdout/stderr, without blocking your main thread.

    Enjoy!

提交回复
热议问题