multiprocessing and Locks

Piet van Oostrum piet at cs.uu.nl
Sun Apr 12 13:30:56 EDT 2009


>>>>> gvv <gvverdad at gmail.com> (G) wrote:

>G> Hi All,
>G> I am trying to understand multiprocessing, but I am getting a Runtime
>G> error on the
>G> code below. What am I missing or doing wrong?
>G> Error is:
>G> RuntimeError: Lock objects should only be shared between processes
>G> through inheritance
[code deleted]

I guess you can't share locks (and probably other objects) between
processes from a Pool. Maybe because there is no direct parent-child
relation or so (there is a separate thread involved). There is nothing
in the doc that explicitely forbids it AFAICT but it says that you have
to be careful with sharing. But it could be a bug.

You can do it with a manager, however, but this involves an additional
process under the hood.

if __name__ == "__main__":
    manager = multiprocessing.Manager()
    lock = manager.Lock()
    pool = multiprocessing.Pool(processes=5)
    for i in xrange(100):
        pool.apply_async(func=RunFunc, args=(i,lock))
    pool.close()
    pool.join()

-- 
Piet van Oostrum <piet at cs.uu.nl>
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: piet at vanoostrum.org



More information about the Python-list mailing list