Python Forum
2 or more processes on the write end of the same pipe
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
2 or more processes on the write end of the same pipe
#4
Here is a solution using socket pairs instead of pipes. The advantage is that there are less blocking issues. Avoiding blocking with pipes may involve using threads and queues to read in the pipe, so I like the option to set sockets unblocking.

The below example shows a program main.py that starts two subprocesses a.py and b.py, both sending their output to the same socket.

# main.py
import subprocess as sp
import socket
import time

def main():
    print('Starting')
    r, w = socket.socketpair()
    r.settimeout(0.2)
    r.shutdown(socket.SHUT_WR)
    w.shutdown(socket.SHUT_RD)
    a = sp.Popen(['python3', 'a.py'], stdout=w)
    b = sp.Popen(['python3', 'b.py'], stdout=w)
    procs = [a, b]
    while procs:
        try:
            x = r.recv(1024)
            print('Read:', repr(x))
        except socket.timeout:
            print('Timed out!')
            procs = [p for p in procs if p.poll() is None]
    r.close()
    w.close()
    print('Bye')

if __name__ == '__main__':
    main()
# a.py
from time import sleep
import sys

for i in range(5):
    print(i)
    sys.stdout.flush()
    sleep(0.5)
# b.py
from time import sleep
import sys

for i in 'abcdef':
    print(i)
    sys.stdout.flush()
    sleep(0.3)
Output:
λ python3 main.py Starting Read: b'a\n' Read: b'0\n' Timed out! Read: b'b\n' Timed out! Read: b'1\n' Read: b'c\n' Timed out! Read: b'd\n' Read: b'2\n' Timed out! Read: b'e\n' Timed out! Read: b'3\nf\n' Timed out! Timed out! Read: b'4\n' Timed out! Timed out! Timed out! Bye
You could do something similar by using r, w = os.pipe() instead of r, w = socket.socketpair() but then you have to manage the blocking problem when you try to read in r.
Reply


Messages In This Thread
RE: 2 or more processes on the write end of the same pipe - by Gribouillis - Sep-27-2020, 07:09 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Media Pipe Python Interfacing with MATLAB cmcreecc 1 166 May-30-2024, 07:23 AM
Last Post: TrentErnser
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,750 Nov-09-2023, 10:56 AM
Last Post: mg24
  Convert Excel file into csv with Pipe symbol.. mg24 4 1,451 Oct-18-2022, 02:59 PM
Last Post: Larz60+
  processes shall be parallel flash77 4 1,208 Sep-20-2022, 11:46 AM
Last Post: DeaD_EyE
  Sharing imported modules with Sub Processes? Stubblemonster 2 1,585 May-02-2022, 06:42 AM
Last Post: Stubblemonster
  Killing processes via python Lavina 2 2,719 Aug-04-2021, 06:20 AM
Last Post: warnerarc
  BrokenPipeError: [Errno 32] Broken pipe throwaway34 6 9,883 May-06-2021, 05:39 AM
Last Post: throwaway34
  sharing variables between two processes Kiyoshi767 1 1,945 Nov-07-2020, 04:00 AM
Last Post: ndc85430
  Errors using --processes parameter sonhospa 3 2,525 Jul-01-2020, 02:24 PM
Last Post: sonhospa
  multiprocessing Pipe.poll very slow seandepagnier 0 2,420 Mar-09-2020, 03:10 AM
Last Post: seandepagnier

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020