Search code examples
pythonmultithreadingmultiprocessingpython-multiprocessing

How to get multiprocessing Array in python process


I'd like to access the "gs" variable here in each process. How do I reference the initargs from the process?

# file exec.py
import time
import os
import random


def f(letter):
    sleep_time = random.randint(3, 20)
    pid = os.getppid()
    g_id = # gs.pop() (how to access gs?)
    print('-------\nsleep: {}\npid: {}\nletter: {}'.format(sleep_time, pid, letter))
    time.sleep(sleep_time)
    print('===========\n{} ended\n==========='.format(letter))

    # gs.add(g_id) (return g_id so it can be used by others)
    return letter

This is the main file:

from multiprocessing import Pool, TimeoutError, Array

# file main.py
if __name__ == '__main__':
    gs = Array('B', [66, 67, 68, 69], lock=False)
    pool = Pool(processes=4, initargs=(gs, ))

    hopts = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']

    pool.map(f, hopts)

Solution

  • The key thing you are missing is that you need to use the initializer argument of the pool. This answer is a good example of how to accomplish that and why you need to do it that way.

    Here is an example of applying that kind of a workflow to your problem:

    from multiprocessing import Pool, Array
    import time
    import os
    import random
    
    def f(letter):
        sleep_time = random.randint(3, 20)
        pid = os.getppid()
        g_id = gs[0]
        print('-------\nsleep: {}\npid: {}\nletter: {}'.format(sleep_time, pid, letter))
        time.sleep(sleep_time)
        print('===========\n{} ended\n==========='.format(letter))
    
        # gs.add(g_id) (return g_id so it can be used by others)
        return letter
    
    def init(local_gs):
        global gs
        gs = local_gs
    
    if __name__ == '__main__':
        gs = Array('B', [66, 67, 68, 69], lock=False)
        pool = Pool(initializer=init, initargs=(gs,))
    
        hopts = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
        pool.map(f, hopts)
    

    (I'm not sure what your end goal is exactly so I attempted to modify as little as possible.)