Search code examples
pythonmultiprocessingpython-multiprocessing

Python Multiproccessing Pool - Sharing one variable per process?


I have been trying to find a simple example where I share one constant variable per process launched in my process pool. Most examples show you how to share variables across processes, which is not what I want.

import multiprocessing
import time

data = (
    {"var":1, "shared": None}, {"var":2, "shared": None}, {"var":3, "shared": None}, {"var":4, "shared": None}
)

def mp_worker(input):
    print input
    # print " Processs %s\tWaiting %s seconds" % (inputs, the_time)
    # time.sleep(int(the_time))
    # print " Process %s\tDONE" % inputs

def mp_handler():
    p = multiprocessing.Pool(2)
    p.map(mp_worker, data)

if __name__ == '__main__':
    mp_handler()

For example, if I run this code, I would like to have my "shared" component intialized once for each process.

I would like to do something like this (This doesnt work):

from multiprocessing import Pool, Process

class Worker(Process):
    def __init__(self):
        print 'Worker started'
        # do some initialization here
        super(Worker, self).__init__()

    def compute(self, data):
        print 'Computing things!'
        return data * data


if __name__ == '__main__':
    # This works fine
    worker = Worker()
    #print worker.compute(3)

    # workers get initialized fine
    pool = Pool(processes = 4,
                initializer = Worker)
    data = range(10)
    # How to use my worker pool?
    # result = pool.map(Worker.compute, data)

    result = pool.map(Worker.compute, data)

Solution

  • def init(args, num_gpu):
        pid = int(str(multiprocessing.current_process()).split(" ")[0].split("-")[-1].split(",")[0]) - 1
        gpu_id = pid % num_gpu
        global testModule
        testModule = TestModuleShared(args, gpu_id)
    
    def worker(datum):
        pid = int(str(multiprocessing.current_process()).split(" ")[0].split("-")[-1].split(",")[0]) - 1
        params = datum["params"]
        # print str(datum["fc"]) + " " + str(pid) 
        # print testModule.openpose
    
        # Reset State
        testModule.run()
    
    p = multiprocessing.Pool(per_gpu_threads*num_gpu, initializer=init, initargs=(params["test_module_param"],num_gpu,))
    

    It turns out you can just use the global variable keyword, along with an initializer callback to initialize it.