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

  • Home
  • SEARCH
  • 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 6468191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T05:52:01+00:00 2026-05-25T05:52:01+00:00

I have a python program, at a point it calls an external program (foo).

  • 0

I have a python program, at a point it calls an external program (foo). This external program needs to be run several times. The exact number of times (num_pros) is variable and depends on the input.
Because this external program is by far the most time consuming part of my Python program I would like to take advantage of multiple cores processors to run several instances of the external program at the same time.

I came with the following solution that take into account that num_pros is unknown a priori and that the solution should be adaptable to any number of cores.

cores=2
proc_list=[]
for i in range(0,num_pros):
    proc=Popen(['foo'], stdin=PIPE)
    proc_list.append(proc)
    if i%cores == cores-1: 
        for process in proc_list:
            process.wait()

I have two questions:

There is a better (more efficient or pythonic) solution?

This code reduce the execution time only when the cores are real. Is this a hardware issue? Or something that could be fixed using python?

To clarify the second question let me provide an example.
In my notebook (running linux) the comnand ‘cat /proc/cpuinfo | grep processor | wc -l’ indicates the existence of 4 processor if I use cores=2 in my code I get the results in half the time (as expected), but when using cores=3 or cores=4 I get the same performance that when using cores=2. I have an Intel core I3 (2 cores and 4 threads) hence I guess that the problem is that only 2 cores are real (I test the code in other computer/processor I get the same result only real cores seems to be useful).

  • 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-25T05:52:01+00:00Added an answer on May 25, 2026 at 5:52 am

    I think multiprocessing is more intended for the case where the work you want to farm out is in python, not a totally different process. It’s all about using fork and passing stuff from python process to python process, so I don’t think it will work for you.

    In your current implementation, once the max number of subprocesses is spawned, your code is blocking the spawning of new subprocesses until all the current batch of processes complete because Popen.wait() blocks until that specific subprocess completes.

    I think what you want is os.wait(). I’ve done something very similar by keeping a mapping of my subprocess.Popen instances mapped by pid. Just spin up your max number of subprocesses and then let os.wait() tell you when one of them finishes. os.wait() will give you the pid of whatever Popen instances completes next and you can use that to do any remaining cleanup for that subprocess. Then you let your code spin up the next subprocess.

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

Sidebar

Related Questions

I have a program which during it's run sometimes needs to call python in
I have a python program that reads floating point values using the following regular
I have a simple python program that I'd like to daemonize. Since the point
I have a python program that opens several urls in seperate tabs in a
I have a Python program (with Django - does this matter?) that I want
I have a python program/file that I want to run repeatedly and calculate the
I have a python program running very well. It connects to several websites and
I have a Python program that uses the threading module. Once every second, my
I have a python program, which runs on the CPython implementation, and inside it
I have a python program that is going to eat a lot of memory,

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.