views:

160

answers:

6

Suppose I have the following in Python

# A loop
for i in range(10000):
    Do Task A

# B loop
for i in range(10000):
    Do Task B

How do I run these loops simultaneously in Python?

+10  A: 

Why do you want to run the two processes at the same time? Is it because you think they will go faster (there is a good chance that they wont). Why not run the tasks in the same loop, e.g.

for i in range(10000):
    doTaskA()
    doTaskB()

The obvious answer to your question is to use threads - see the python threading module. However threading is a big subject and has many pitfalls, so read up on it before you go down that route.

Alternatively you could run the tasks in separate proccesses, using the python multiprocessing module. If both tasks are CPU intensive this will make better use of multiple cores on your computer.

There are other options such as coroutines, stackless tasklets, greenlets, CSP etc, but Without knowing more about Task A and Task B and why they need to be run at the same time it is impossible to give a more specific answer.

Dave Kirby
Just a little warning about the threading module. python has something it calls the Global Interpreter Lock (GIL). This locks out certain (large) areas of python from running at the same time even in separate threads. Multiprocessing doesn't have this issue - though it has its own bunch of pitfalls.
Michael Anderson
if speed is the issue, using some variant of `map`, a list comprehension or a generator expression is the way to go if it's feasible.
aaronasterling
+1  A: 

How about: A loop for i in range(10000): Do Task A, Do Task B ? Without more information i dont have a better answer.

PeterK
+1  A: 

You could use threading or multiprocessing.

Matt Curtis
A: 
from threading import Thread
def loopA():
    for i in range(10000):
        #Do task A
def loopA():
    for i in range(10000):
        #Do task B
threadA = Thread(target = loopA)
threadB = Thread(target = loobB)
threadA.run()
threadB.run()
# Do work indepedent of loopA and loopB 
threadA.join()
threadB.join()
Odomontois
+1  A: 

If you want concurrency, here's a very simple example:

from multiprocessing import Process

def loop_a():
    while 1:
        print("a")

def loop_b():
    while 1:
        print("b")

if __name__ == '__main__':
    Process(target=loop_a).start()
    Process(target=loop_b).start()

This is just the most basic example I could think of. Be sure to read http://docs.python.org/library/multiprocessing.html to understand what's happening.

If you want to send data back to the program, I'd recommend using a Queue (which in my experience is easiest to use).

via Michael Anderson:
This method appears to be preferred to using the threading module. See http://en.wikipedia.org/wiki/Global_Interpreter_Lock

stefano palazzo
I'm four minutes behind Odomontois.
stefano palazzo
A: 

Another thing to keep in mind is that if you are looping over big ranges in python it is probably better to be using xrange so that the list isn't created in memory first

Hugoagogo