multithreading - Python - Multiprocessing Pool Class: Can the Thread Function contain arguments? -
i want know if possible pass arguments threading pool funktion?
from multiprocessing.dummy import pool threadpool def mainfunc(): myarray = ["a", "b", "c"] pool = threadpool(8) pool.map(threadfunc, myarray) # how possible give >>threadfunc<< argument >>text1<< pool.close() pool.join() def threadfunc(text2): text1 = "hello " # should given argument of function print text1 + text2 mainfunc()
this simple example of want do.. how can give text1 second argument threadfunc function?
solved:
i solved problem simple global accessible variable ... solution problem had right ... greater idea use multiprocessing.process
instead ...
import sys multiprocessing.dummy import pool threadpool = sys.modules[__name__] def mainfunc(): myarray = ["a", "b", "c"] this.text1 = "hello " pool = threadpool(8) pool.map(threadfunc, myarray) # how possible give >>threadfunc<< argument >>text1<< pool.close() pool.join() def threadfunc(text2): print this.text1 + text2 mainfunc()
the official python docs says
a parallel equivalent of map() built-in function (it supports 1 iterable argument though). blocks until result ready.
therefore impossible add argument, can add text1 last element of
myarray
, pop out once use it.you can use
multiprocessing.process
other pool. in case can add argument easily.def f(name): print 'hello', name if __name__ == '__main__': p = process(target=f, args=('bob',)) p.start() p.join()
Comments
Post a Comment