由于多处理需要放在windows中的__main__
中,我发现我的全局变量没有传递给我的函数。
from multiprocessing import Pool, Value
def Proses(lines):
global xyz
try:
print(lines)
print(xyz)
except Exception as e:
print(e)
def counter_handler(args):
global counter
counter = args
def Dosomething():
a = [i for i in range(10)]
return a
if __name__ == '__main__':
# i want to share xyz variable
xyz = Dosomething()
threads = []
data = ["bjir", "bjir", "bjir"]
counter = Value('i', 0)
with Pool(1) as pool:
p = Pool(1, initializer=counter_handler, initargs=(counter,))
i = p.map_async(Proses, data, chunksize=1)
i.wait()
我已经找了几个小时了,但还是没有线索,我想这可能是一个重复的问题,我知道,但我仍然找不到任何答案。有什么方法可以将xyz
变量传递给函数吗?
1条答案
按热度按时间i86rm4rw1#
正如您在示例中所写的,
Pool
有initializer=
和initargs=
参数,您也可以使用它们来初始化全局变量:图纸: