what i want to do is be able to call a function with multiple threads and get their results.
i have the following code:
(it is an example, the actual code doesn’t simply convert str to int)
from threading import Thread
import time
import Queue
#an example - actual code connects to a server
def str_to_int(arg, queue):
result = 0
result = int(arg)
#sleep to check that they happen at once.
time.sleep(10)
queue.put(result)
def combine():
q1 = Queue.Queue()
q2 = Queue.Queue()
q3 = Queue.Queue()
t1 = Thread(target = str_to_int, args=("111", q1))
t2 = Thread(target = str_to_int, args=("222", q2))
t3 = Thread(target = str_to_int, args=("333", q3))
t1.start()
t2.start()
t3.start()
t1.join()
t2.join()
t3.join()
return (q1.get(),q2.get(),q3.get())
print combine()
this code works. and i get the expected results:
>>>
(111, 222, 333)
however, there must be a better way to do this.
i plan on having many more threads than 3, but even if i was only staying with 3 – it seems very ugly.
EDIT: i need to be able to know which result came from which thread (ie: from which parameters/arguments that i gave the function)
Here are some advices:
Queue‘s are thread-safe, so use 1 queue to pass results.So here’s what your code might look like: