Search code examples
pythonpython-3.xpython-multiprocessing

How to use os environment variable in main class, which set through multiprocessing sub methods? multiprocessing scenario


I am working on application where multiprocessing is used.

# Main class
# some code...

def executor():
  os.environ['test_count'] = "setting this os. I want to use this os variable in main class"

def run():
  # Some code ..........
  process = Process(target=executor, args=(...))  # executer method gets call from here


  # Some code ..........
  # want to use 'test_count' here somewhere

in executor method I am setting one os variable. I want to use that variable in main class.

Any help is appreciated.


Solution

  • You can use process shared queue from multiprocessing module. The code may look something like this:

    from multiprocessing import Process, Queue
    
    def executor(queue):
        queue.put({"key": "value"})
    
    def run(queue):
        process = Process(target=executor, args=(queue,))
        process.start()
        process.join()
        print(queue.get())
    
    if __name__ == '__main__':
        run(Queue())
    

    Output:

    >>> {'key': 'value'}
    

    There are also proxy objects for sharing data between multiple processes.