Multiprocessing does not see global variables?
Question: I have run into a strange behaviour at multiprocessing.

When i try to use a global variable in a function which is called from
multiprocessing it does not see a global variable.

Example:

    
    
    import multiprocessing
    
    def func(useless_variable):
        print(variable)
    
    useless_list = [1,2,3,4,5,6]
    p = multiprocessing.Pool(processes=multiprocessing.cpu_count())
    variable = "asd"
    
    func(useless_list)
    
    for x in p.imap_unordered(func, useless_list):
        pass
    

Output:

    
    
    asd
    multiprocessing.pool.RemoteTraceback: 
    """
    Traceback (most recent call last):
      File "/usr/lib/python3.4/multiprocessing/pool.py", line 119, in worker
        result = (True, func(*args, **kwds))
      File "pywork/asd.py", line 4, in func
        print(variable)
    NameError: name 'variable' is not defined
    """
    
    The above exception was the direct cause of the following exception:
    
    Traceback (most recent call last):
      File "pywork/asd.py", line 11, in <module>
        for x in p.imap_unordered(func, useless_list):
      File "/usr/lib/python3.4/multiprocessing/pool.py", line 689, in next
        raise value
    NameError: name 'variable' is not defined
    

As you see the first time i just simply call `func` it print `asd` as
expected. However when i call the very same function with multiprocessing it
says the variable `variable` does not exists, even after i clearly printed it
just before.

Does multiprocessing ignore global variables? How can i work this around?


Answer: When you spam a process all context is copyed, you need to get use of
[`managers`](https://docs.python.org/2/library/multiprocessing.html#managers)
for exachanging objects between them, check the [official
documentations](https://docs.python.org/2/library/multiprocessing.html#exchanging-
objects-between-processes), for managing state check
[this](https://docs.python.org/2/library/multiprocessing.html#sharing-state-
between-processes).

