Python - 多处理池 Class:线程函数可以包含参数吗?
Python - Multiprocessing Pool Class: Can the Thread Function contain arguments?
我想知道是否可以将参数传递到线程池函数中?
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 文档说
A parallel equivalent of the map() built-in function (it supports only one iterable argument though). It blocks until the result is ready.
因此无法添加另一个参数,但您始终可以将 text1 添加为 myArray
的最后一个元素,并在使用后将其弹出。
您还可以使用multiprocessing.Process
而不是游泳池。在这种情况下,您可以轻松添加另一个参数。
def f(name):
print 'hello', name
if __name__ == '__main__':
p = Process(target=f, args=('bob',))
p.start()
p.join()
您可以在元组或列表中传递任意数量的参数:
from multiprocessing.dummy import Pool as ThreadPool
def mainFunc():
myArray = ["A", "B", "C"]
text1 = "Hello "
pool = ThreadPool(8)
# combine text1 with each element in myArray
args_generator = ((text1, x) for x in myArray)
pool.map(threadFunc, args_generator)
pool.close()
pool.join()
def threadFunc(args):
# unpack arguments
text1, text2 = args
print text1 + text2
我想知道是否可以将参数传递到线程池函数中?
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 文档说
A parallel equivalent of the map() built-in function (it supports only one iterable argument though). It blocks until the result is ready.
因此无法添加另一个参数,但您始终可以将 text1 添加为
myArray
的最后一个元素,并在使用后将其弹出。您还可以使用
multiprocessing.Process
而不是游泳池。在这种情况下,您可以轻松添加另一个参数。def f(name): print 'hello', name if __name__ == '__main__': p = Process(target=f, args=('bob',)) p.start() p.join()
您可以在元组或列表中传递任意数量的参数:
from multiprocessing.dummy import Pool as ThreadPool
def mainFunc():
myArray = ["A", "B", "C"]
text1 = "Hello "
pool = ThreadPool(8)
# combine text1 with each element in myArray
args_generator = ((text1, x) for x in myArray)
pool.map(threadFunc, args_generator)
pool.close()
pool.join()
def threadFunc(args):
# unpack arguments
text1, text2 = args
print text1 + text2