[英]How to run N threads of the same functions with different parameters in parallel? - python
From Concurrently run two functions that take parameters and return lists? 从Concurrent运行两个带参数和返回列表的函数? , i can run 2 functions in parallel by specifying 2 queues.
,我可以通过指定2个队列并行运行2个函数。
from threading import Thread
from Queue import Queue
def func1(x):
return [i*i for i in x]
nums1 = [1,2,3,4,5]; nums2 = [112,32,53,64,25]
def wrapper(func, arg, queue):
queue.put(func(arg))
q1, q2 = Queue(), Queue()
Thread(target=wrapper, args=(func1, nums1, q1)).start()
Thread(target=wrapper, args=(func1, nums2, q2)).start()
print q1.get(), q2.get()
How to run N threads of the same functions with different parameters in parallel? 如何并行运行具有不同参数的相同函数的N个线程?
Currently, I'm hard coding and doing: 目前,我很难编码和做:
nums1 = [1,2,3,4,5]; nums2 = [112,32,53,64,25]
nums3 = [11,522,33,467,85]; nums4 = [12,2,5,4,1125]
q1, q2, q3, q4 = Queue(), Queue(), Queue(), Queue()
Thread(target=wrapper, args=(func1, nums1, q1)).start()
Thread(target=wrapper, args=(func1, nums2, q2)).start()
Thread(target=wrapper, args=(func1, nums3, q3)).start()
Thread(target=wrapper, args=(func1, nums4, q4)).start()
print q1.get(), q2.get(), q3.get()
Queues are threadsafe. 队列是线程安全的。 So you should be able to get away with two queues, to be shared between all your threads:
所以你应该能够逃脱两个队列,在所有线程之间共享:
from threading import Thread
from multiprocessing import Queue
def func1(x):
return [i*i for i in x]
nums = [1,2,3,4,5,112,32,53,64,25]
def wrapper(func, qIn, qOut):
for arg in iter(qIn.get, None):
qOut.put(func(arg))
qIn, qOut = Queue(), Queue()
chunksize = len(nums)/numThreads
for i in xrange(numThreads):
qIn.put(nums[i*chunksize : (i+1)*chunksize])
numThreads = 2 # or N
for _ in xrange(numThreads):
qIn.put(None)
for _ in xrange(numThreads):
Thread(target=wrapper, args=(func1, qIn, qOut)).start()
for _ in xrange(numThreads):
print qOut.get()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.