[Baypiggies] multithreading question

Alex Martelli aleax at google.com
Wed Sep 17 22:07:37 CEST 2008


On Wed, Sep 17, 2008 at 12:06 PM, Shannon -jj Behrens <jjinux at gmail.com> wrote:
> On Wed, Sep 17, 2008 at 12:01 PM, Adam Hupp <adam at hupp.org> wrote:
>> On Wed, Sep 17, 2008 at 11:40 AM, Shannon -jj Behrens <jjinux at gmail.com> wrote:
>>>
>>> I'm using the queue module to coordinate a bunch of threads.  Those
>>> threads need to log a line to STDOUT.  The line is short.  Can I count
>>> on the GIL to make sure that writing a single line to STDOUT is
>>> atomic, or do I need to be more paranoid?
>>
>> Isn't the GIL dropped during any IO?  In that case I don't think you
>> can rely on it.
>
> Python gives up the GIL.  The C library goes to write the string.  It
> writes out only four bytes.  A context switch occurs.  Some other
> thread does the same thing.  Kaboom.
>
> Seems plausible.  Is it really as bad as that?  Ugh.

Yep.  If you need real reliability, devote a separate thread to the
sole task of writing to stdout, listening on a Queue for such write
requests.  Or, you could use the logging module.


Alex


More information about the Baypiggies mailing list