简体   繁体   中英

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() . But from p.map , it behaves as if the set is empty. However, it all works fine when I declare the set directly a = set(["portugal", "india"]) without going through the init module.

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM