【发布时间】:2016-07-15 20:06:37
【问题描述】:
我想知道是否可以将参数传递到线程池功能中?
from multiprocessing.dummy import Pool as ThreadPool
def mainFunc():
myArray = ["A", "B", "C"]
pool = ThreadPool(8)
pool.map(threadFunc, myArray) # how is it possible to give >>threadFunc<< another argument >>text1<<
pool.close()
pool.join()
def threadFunc(text2):
text1 = "Hello " # this should be given as a argument of the function
print text1 + text2
mainFunc()
这是我想做的一个简单示例。如何将 text1 作为第二个参数提供给 threadFunc 函数?
已解决:
我用一个简单的全局可访问变量解决了这个问题......但这只是我现在遇到的问题的一个解决方案......改用multiprocessing.Process是一个更好的主意......
import sys
from multiprocessing.dummy import Pool as ThreadPool
this = sys.modules[__name__]
def mainFunc():
myArray = ["A", "B", "C"]
this.text1 = "Hello "
pool = ThreadPool(8)
pool.map(threadFunc, myArray) # how is it possible to give >>threadFunc<< another argument >>text1<<
pool.close()
pool.join()
def threadFunc(text2):
print this.text1 + text2
mainFunc()
【问题讨论】:
标签: python multithreading multiprocessing pool