Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7644785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:46:24+00:00 2026-05-31T09:46:24+00:00

I want to run many processes in parallel with ability to take stdout in

  • 0

I want to run many processes in parallel with ability to take stdout in any time. How should I do it? Do I need to run thread for each subprocess.Popen() call, a what?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T09:46:26+00:00Added an answer on May 31, 2026 at 9:46 am

    You can do it in a single thread.

    Suppose you have a script that prints lines at random times:

    #!/usr/bin/env python
    #file: child.py
    import os
    import random
    import sys
    import time
    
    for i in range(10):
        print("%2d %s %s" % (int(sys.argv[1]), os.getpid(), i))
        sys.stdout.flush()
        time.sleep(random.random())
    

    And you’d like to collect the output as soon as it becomes available, you could use select on POSIX systems as @zigg suggested:

    #!/usr/bin/env python
    from __future__ import print_function
    from select     import select
    from subprocess import Popen, PIPE
    
    # start several subprocesses
    processes = [Popen(['./child.py', str(i)], stdout=PIPE,
                       bufsize=1, close_fds=True,
                       universal_newlines=True)
                 for i in range(5)]
    
    # read output
    timeout = 0.1 # seconds
    while processes:
        # remove finished processes from the list (O(N**2))
        for p in processes[:]:
            if p.poll() is not None: # process ended
                print(p.stdout.read(), end='') # read the rest
                p.stdout.close()
                processes.remove(p)
    
        # wait until there is something to read
        rlist = select([p.stdout for p in processes], [],[], timeout)[0]
    
        # read a line from each process that has output ready
        for f in rlist:
            print(f.readline(), end='') #NOTE: it can block
    

    A more portable solution (that should work on Windows, Linux, OSX) can use reader threads for each process, see Non-blocking read on a subprocess.PIPE in python.

    Here’s os.pipe()-based solution that works on Unix and Windows:

    #!/usr/bin/env python
    from __future__ import print_function
    import io
    import os
    import sys
    from subprocess import Popen
    
    ON_POSIX = 'posix' in sys.builtin_module_names
    
    # create a pipe to get data
    input_fd, output_fd = os.pipe()
    
    # start several subprocesses
    processes = [Popen([sys.executable, 'child.py', str(i)], stdout=output_fd,
                       close_fds=ON_POSIX) # close input_fd in children
                 for i in range(5)]
    os.close(output_fd) # close unused end of the pipe
    
    # read output line by line as soon as it is available
    with io.open(input_fd, 'r', buffering=1) as file:
        for line in file:
            print(line, end='')
    #
    for p in processes:
        p.wait()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've run into a problem when using a one to many relationship. I want
I've seen many similar posts but I am still stumped. I want to run
How to start many processes(will run the same script) by Daemons gem, and how
I want to run dpkg ( or any other binary library files from cydia
I want run a script as follows: runner: ssh 'java program &' ssh 'java
Why this code don't work,when i want run this code vwd 2008 express show
I want to run a weekly batch process in an asp.net page. How can
I want to run a web application on php and mysql, using the CakePHP
I want to run javascript/Python/Ruby inside my application. I have an application that creates
I want to run a command as soon as a certain text appears in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.