multiprocessing and Locks

gvv gvverdad at gmail.com
Mon Apr 13 01:08:14 EDT 2009


On Apr 13, 3:30 am, Piet van Oostrum <p... at cs.uu.nl> wrote:
> >>>>> gvv <gvver... 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 <p... at cs.uu.nl>
> URL:http://pietvanoostrum.com[PGP 8DAE142BE17999C4]
> Private email: p... at vanoostrum.org

Hi Piet,

Thanks for your help. It worked.



More information about the Python-list mailing list