
Threading.Thread(target=function, args=(arg1, arg2)). You make one in Python byĬalling the Thread constructor with a call something like this: Shares the same memory space as the spawning thread. Luckily, Python has a way of doing two things atĪ thread, if you didn’t know, is a lightweight separate thread of execution that Simultaneously and putting a line from A or B to the mother process’s What we need is a way of doing two things at once: reading from A and B Interesting output is coming from A it makes no odds, it just has to wait. Just wait as long as it takes for some output to turn up. Stdout of B and no output is produced from B, then our mother process will However, there’s a snag, and that snag is that the read() method of Python’sįile object is blocking: if our mother process is waiting to read from the Subprocess and pass through to the mother process’s stdout, prepended with anĪ: if it came from procedure A and a B: if it came from procedure B. Our basic strategy is going to be read a line from a To have to do something clever… Non-blocking Outputs: The Power of Threads Interleaved and two know what input to send to which child process we’re going

In order to avoid the two outputs becoming Their input and outputs since we have two inputs and outputs and only one mother This is necessary because we want to have control over Note that both our subprocesses are opened with their stdins and stdoutsįrom internal pipes. PIPE) while True: # check if either sub-process has finished proc_a. # two_subprocesses.py import subprocess # start both `proc_a.py` and `proc_b.py` proc_a = subprocess. Prompt, reading a line of input, echoing that back and prompting for more input: Going to spawn will be kept very simple, doing nothing more than printing a The Two Sub-ProcessesĪs an illustration of what can be achieved, the two sub-processes that we are Which output comes from which process we’ll prepend output from process A withĪn A: and from process B with a B. Of the processes will be printed on stdout, but to enable us to differentiate These two pipes A and B will read their respective inputs. What we aim to end up with is a program that starts up two sub-processes, let’sĬall them A and B, and connects to two named pipes in the file system. The server, to provide a set of pre-scripted commands to each to get them in aĬertain state, and then a way of providing my own custom commands, to do some

Basically, I wanted a program to start up the client and In this section we’ll do the same, but this time for two sub-processes.Ī use for this, and the original reason I first developed this, was for testingĪ client and server. We explored start a subprocess and controlling its input and output via
