dictionary of pickle error when get it in multiprocessing and has name error

Peter Otten __peter__ at web.de
Mon Feb 27 03:28:45 EST 2017


Ho Yeung Lee wrote:

> before cloususerlogin
> Unexpected error: <type 'exceptions.NameError'>
> after map pool
> 
> 
> ...
> passwordlist = pickle.load( open( str(currentworkingdirectory) +
> "\\decryptedsecret.p", "rb" ) )

According to
https://docs.python.org/dev/library/multiprocessing.html#programming-guidelines

you cannot use global resources on Windows:

"""
Explicitly pass resources to child processes

On Unix using the fork start method, a child process can make use of a 
shared resource created in a parent process using a global resource. 
However, it is better to pass the object as an argument to the constructor 
for the child process.

Apart from making the code (potentially) compatible with Windows and the 
other start methods this also ensures that as long as the child process is 
still alive the object will not be garbage collected in the parent process. 
This might be important if some resource is freed when the object is garbage 
collected in the parent process.

So for instance

from multiprocessing import Process, Lock

def f():
    ... do something using "lock" ...

if __name__ == '__main__':
    lock = Lock()
    for i in range(10):
        Process(target=f).start()
should be rewritten as

from multiprocessing import Process, Lock

def f(l):
    ... do something using "l" ...

if __name__ == '__main__':
    lock = Lock()
    for i in range(10):
        Process(target=f, args=(lock,)).start()
"""





More information about the Python-list mailing list