fork, threads and proper closing

OdarR Olivier.Darge at gmail.com
Mon Jun 29 13:32:43 EDT 2009


On 29 juin, 14:44, Francesco Bochicchio <bieff... at gmail.com> wrote:
> On 29 Giu, 07:10, OdarR <Olivier.Da... at gmail.com> wrote:
>
>
>
> > On 28 juin, 23:26, Tomasz Pajor <ni... at puffy.pl> wrote:
>
> > > Hello,
>
> > > Configuration is as follows.
>
> > > I have a starter process which creates 3 sub processes (forks) and each
> > > of this processes creates a number of threads.
> > > Threads in that processes have semaphore so on KeyboardInterrupt without
> > > sending a sigterm to the subprocess i'm not able to close threads.
> > > Is there any work around? Can I somehow run join for the thread on
> > > keyboard interrupt?
>
> > When creating a thread you can add a Queue parameter to communicate
> > with threads:http://docs.python.org/library/queue.html
> > easy and reliable.
>
> > give them a "poison pill" in the queue: a recognizable object placed
> > on the queue that means "when you get this, stop."
>
> This is the way I usually go, but it has one important limitation: if
> the thread is waiting
> for a blocking I/O operation to complete, like reading from a socket
> with no data or waiting

add a small wait (time.sleep()), and also, I/O function in Python can
often releas the GIL...

> for a locked resource (i.e. semaphore) to be unlocked, it will not
> service the queue and will
> not read the 'quit command' (the poison pill), and therefore will not
> quit until the blocking
> I/O terminates (and it could be never).
>
> ASAIK, there is no way - in python - to solve this.

no easy way, yes...
but I think I gave a good advice to our friend Tomasz :)

Olivier



More information about the Python-list mailing list