[英]Python multiprocessing unable to read from shared set
I have just started to use the multiprocessing
module for a process that reads from a shared python set()
.我刚刚开始将multiprocessing
模块用于从共享 python set()
读取的进程。 But from p.map
, it behaves as if the set is empty.但是从p.map
,它的行为就好像集合是空的。 However, it all works fine when I declare the set directly a = set(["portugal", "india"])
without going through the init
module.但是,当我直接声明集合a = set(["portugal", "india"])
而不通过init
模块时,一切正常。
What's the problem here?这里有什么问题? My actual processing is complex.我的实际处理很复杂。 How do I make sure that the code works like it would have worked with single processor?我如何确保代码像在单处理器上一样工作?
from multiprocessing import Pool
class ABC:
a = set()
def __init__(self):
ABC.a.add("portugal")
ABC.a.add("india")
def is_loc(text):
return text in ABC.a
def main():
ABC()
locs = ["portugal", "india", "om", "pitata"]
with Pool(4) as p:
print(p.map(ABC.is_loc, locs)) # [False, False, False, False]
res = list(map(ABC.is_loc, locs))
print(res) # [True, True, False, False]
if __name__ == '__main__':
main()
Not sure why but calling this gives correct results不知道为什么,但调用它会给出正确的结果
multiprocessing.set_start_method("fork")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.