Search code examples
pythonsetpython-multiprocessing

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

Solution

  • Not sure why but calling this gives correct results

    multiprocessing.set_start_method("fork")