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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T11:49:37+00:00 2026-05-15T11:49:37+00:00

Considering the code below. I would like to run 3 experiments at a time.

  • 0

Considering the code below. I would like to run 3 experiments at a time. The experiments are independent, the only thing they share is the Model object which they only read.

As there are seemingly no hard things in threading this out, how can I best do this in Python? I would like to use a pool or so to make sure that only three experiments run at a time. Shall I use multi-processing? If yes, how is the shortest and most concise?

#!/usr/bin/env python2.6
import time

class Model:
    name = ""
    def __init__(self,name):
        self.name = name

class Experiment:
    id = 0
    model = None
    done = False

    def __init__(self,id,model):
        self.id = id
        self.model = model

    def run(self):
        for _ in range(0,60):
            print "Hey %s from experiment %d" % (self.model.name, id)
            time.sleep(1)
        self.done = True


if __name__ == "__main__":
    experiments = []
    model = Model("statictistical model")
    for i in range(0,5):
        experiments.append(Experiment(i, model))

    #How to run 3 experiments at the same time
  • 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-15T11:49:38+00:00Added an answer on May 15, 2026 at 11:49 am

    Check the docs, specifically:

    http://docs.python.org/library/multiprocessing.html#module-multiprocessing.pool

    There really are a lot of examples there that should get you on your way. For instance, I could come up with:

    #!/usr/bin/env python2.6
    import time
    import multiprocessing
    
    class Model:
        name = ""
        def __init__(self,name):
            self.name = name
    
    def run_experiment(id, model):
        print "Experiment %d is starting" % id
        for _ in range(0,60):
            print "Hey %s from experiment %d" % (model.name, id)
            time.sleep(1)
        print "Experiment %d is done" % id
        return "Result for %d" % id
    
    
    if __name__ == "__main__":
        model = Model("statictistical model")
        experiments = ((i, model) for i in range(0, 5))
        pool = multiprocessing.Pool(3)
    
        results = [pool.apply_async(run_experiment, experiment) for experiment in experiments]
        for result in results:
            r = result.get()
            # do something with r
            # or nothing, i suppose...
    

    Do also pay attention to what the docs say about using the multiprocessing module:

    Functionality within this package
    requires that the __main__ method be
    importable by the children. This is
    covered in Programming guidelines
    however it is worth pointing out here.
    This means that some examples, such as
    the multiprocessing.Pool examples
    will not work in the interactive
    interpreter

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

Sidebar

Related Questions

Considering the criteria listed below, which of Python, Groovy or Ruby would you use?
We use a simple object model for our low level networking code at work
Considering such code: class ToBeTested { public: void doForEach() { for (vector<Contained>::iterator it =
Considering this code, can I be absolutely sure that the finally block always executes,
I am considering purchasing a code signing certificate from VeriSign or Thawte to sign
I'm considering integrating some D3D code I have with WPF via the new D3DImage
I am considering porting a small portion of the code in a C# project
I have this small code library that I'm considering releasing into Open Source. I
Logging can get complicated, quickly. Considering that you have some code, how do you
Considering the following architecture: a base object 'Entity' a derived object 'Entry:Base' and a

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.