Search code examples
pythonpython-3.xpython-multiprocessingglobal

Python global variable on __main__


since multiprocessing need to be put inside __main__ in windows, i found out that my global variable is not passed to my function.

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

been searching for hours but still no clue and i think maybe this is a duplcate quetions and i know it but i still can't find any answers. Is there any way to pass my xyz variable to my function?


Solution

  • As you have written in your example, Pool has initializer= and initargs= parameters which you can use to initialize the global variables as well:

    from multiprocessing import Pool, Value
    
    
    def Proses(lines):
        global xyz
    
        print(lines)
        print(xyz)
    
    
    def Dosomething():
        a = [i for i in range(10)]
        return a
    
    
    def init_pool(a, cnt):
        global xyz, counter
        xyz = a
        counter = cnt
    
    
    if __name__ == "__main__":
        # i want to share xyz variable
        xyz = Dosomething()
    
        data = ["bjir", "bjir", "bjir"]
        counter = Value("i", 0)
    
        with Pool(1, initializer=init_pool, initargs=(xyz, counter)) as pool:
            i = pool.map_async(Proses, data, chunksize=1)
            i.wait()
    

    Prints:

    bjir
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    bjir
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    bjir
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]