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 7664573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:20:28+00:00 2026-05-31T14:20:28+00:00

I understand using subprocess is the preferred way of calling external command. But what

  • 0

I understand using subprocess is the preferred way of calling external command.

But what if I want to run several commands in parall, but limit the number of processes being spawned? What bothers me is that I can’t block the subprocesses. For example, if I call

subprocess.Popen(cmd, stderr=outputfile, stdout=outputfile)

Then the process will continue, without waiting for cmd to finish. Therefore, I can’t wrap it up in a worker of multiprocessing library.

For example, if I do:

def worker(cmd): 
    subprocess.Popen(cmd, stderr=outputfile, stdout=outputfile);

pool = Pool( processes = 10 );
results =[pool.apply_async(worker, [cmd]) for cmd in cmd_list];
ans = [res.get() for res in results];

then each worker will finish and return after spawning a subprocess. So I can’t really limit the number of processes generated by subprocess by using Pool.

What’s the proper way of limiting the number of subprocesses?

  • 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-31T14:20:29+00:00Added an answer on May 31, 2026 at 2:20 pm

    You can use subprocess.call if you want to wait for the command to complete. See pydoc subprocess for more information.

    You could also call the Popen.wait method in your worker:

    def worker(cmd): 
        p = subprocess.Popen(cmd, stderr=outputfile, stdout=outputfile);
        p.wait()
    

    Because there seems to be some confusion about this answer, here’s a complete example:

    import concurrent.futures
    import multiprocessing
    import random
    import subprocess
    
    
    def worker(workerid):
        print(f"start {workerid}")
        p = subprocess.Popen(["sleep", f"{random.randint(1,30)}"])
        p.wait()
        print(f"stop {workerid}")
        return workerid
    
    
    def main():
        tasks = []
        with concurrent.futures.ThreadPoolExecutor(max_workers=20) as pool:
            for i in range(20):
                tasks.append(pool.submit(worker, i))
    
            print("waiting for tasks...", flush=True)
            for task in concurrent.futures.as_completed(tasks):
                print(f"completed {task.result()}", flush=True)
            print("done.")
    
    
    if __name__ == "__main__":
        main()
    

    If you run the above code, you will see that all of the worker processes start in parallel and that we are able to gather values as they are completed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I understand the pros of using namespaces within the actual application layers but when
I don't understand what this code is doing, I'm wanting to run a command
I can understand using properties for messages and international settings but I feel there
I'm learning Squeryl and trying to understand the 'using' syntax but can't find documentation
There are several tabs in GUI runner of NUnit: I understand that using Console.WriteLine
I understand using 0 and 1 for true and false evaluation, but can someone
I understand that using BitmapFactory can convert a File to a Bitmap, but is
I understand that using the === compares type, so running the following code results
I understand that using the facebook API I need an api key to connect,
I don't understand how using a 'challenge token' would add any sort of prevention:

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.