Search code examples
pythonconcurrencytwistedreactor

Python - simplest way possible to run functions in parallel


I'm struggling a bit with something that should actually be quite simple to do. I have a function that does some long computation, to simplify it loks like this:

import time    
def compute_stuff(number):
   time.sleep(10)
   return [number*2, number*4]

I want to run TWO instances of this function in parallel and collect their results in one array. I've read a bit about Twisted reactor and it seems to provide async queries, but is running everything in one thread so when I do e.g.:

from twisted.internet import reactor
import time

def compute_stuff(number):
   time.sleep(10)
   return [number*2, number*4]

reactor.callWhenRunning(compute_stuff, 1)
reactor.callWhenRunning(compute_stuff, 4)
reactor.callWhenRunning(compute_stuff, 2)
reactor.run()

It waits for the first call to complete before executing the next one. Is there a way to make it parallel? Is Twisted even the way to go?


Solution

  • You can try the threading module

    import threading
    import time    
    
    def compute_stuff(number):
       print str(number) + "start"
       time.sleep(10)
       print str(number) + "end"
       return [number*2, number*4]
    
    
    threads = []
    for i in range(5):
        t = threading.Thread(target=compute_stuff, args=(i,))
        threads.append(t)
        t.start()