Python Forum
printing interleaved lines from many processes
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
printing interleaved lines from many processes
#1
a new program (no code, yet) will launch many child processes that all will be printing long lines of info to stdout that is passed along from the parent (not piped to the parent). it is essential that each line stay whole and that the interleaving between lines never breaks a line in the middle no matter how stdout gets redirected by the shell when program gets run. lines will not be longer than 1000 characters but they could have Unicode characters in UTF-8 form some day in the future. this will only ever be run on Linux, BSD, Mac, or Unix, never on Windows, DOS, or OS/2. what is the best way, in Python, to ensure that this works?

edit:

to be clear, the parent is passing along stdout that it originally got when it began.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
Either flush stdout or open stdout without buffering.
Reply
#3
To ensure that the long lines of info printed by child processes are not broken in the middle when redirected by the shell, you can utilize the subprocess module in Python. Specifically, you can use subprocess.Popen to launch the child processes and handle their output.

Here's a basic approach:

Use subprocess.Popen to launch the child processes.
Redirect the stdout of each child process to a pipe.
Read from the pipe in the parent process and print the output.
Here's a simplified example:

python
Copy code
import subprocess

# Launch child process with stdout redirected to a pipe
child_process = subprocess.Popen(['your_child_process_command'], stdout=subprocess.PIPE)

# Read output from the child process line by line
for line in child_process.stdout:
# Print the line without breaking in the middle
print(line.decode('utf-8').rstrip()) # Decode from bytes to string and remove trailing newline
This approach ensures that each line of output from the child process is printed in its entirety, without being broken in the middle, even when redirected by the shell. It also handles Unicode characters encoded in UTF-8 format properly.

You can extend this basic approach to handle multiple child processes by launching them sequentially or in parallel, depending on your requirements. Additionally, you may need to handle error handling and other considerations depending on the specific behavior of your child processes and the environment they are running in.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  launch processes from threads Skaperen 9 761 Feb-21-2024, 01:16 AM
Last Post: Skaperen
  order to call Popen for 2 piped processes Skaperen 0 1,161 Oct-22-2020, 11:31 PM
Last Post: Skaperen
  capture stdout from child processes Skaperen 0 3,295 Oct-30-2019, 12:11 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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