blocks - send input to python subprocess pipeline

前端 未结 11 1343
轻奢々
轻奢々 2021-01-30 09:22

I\'m testing subprocesses pipelines with python. I\'m aware that I can do what the programs below do in python directly, but that\'s not the point. I just want to test the pipel

11条回答
  •  星月不相逢
    2021-01-30 09:52

    There are three main tricks to making pipes work as expected

    1. Make sure each end of the pipe is used in a different thread/process (some of the examples near the top suffer from this problem).

    2. explicitly close the unused end of the pipe in each process

    3. deal with buffering by either disabling it (Python -u option), using pty's, or simply filling up the buffer with something that won't affect the data, ( maybe '\n', but whatever fits).

    The examples in the Python "pipeline" module (I'm the author) fit your scenario exactly, and make the low-level steps fairly clear.

    http://pypi.python.org/pypi/pipeline/

    More recently, I used the subprocess module as part of a producer-processor-consumer-controller pattern:

    http://www.darkarchive.org/w/Pub/PythonInteract

    This example deals with buffered stdin without resorting to using a pty, and also illustrates which pipe ends should be closed where. I prefer processes to threading, but the principle is the same. Additionally, it illustrates synchronizing Queues to which feed the producer and collect output from the consumer, and how to shut them down cleanly (look out for the sentinels inserted into the queues). This pattern allows new input to be generated based on recent output, allowing for recursive discovery and processing.

提交回复
热议问题