threads and extension module initialization

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Fri Mar 28 12:00:18 EDT 2008


En Fri, 28 Mar 2008 12:25:55 -0300, <pianomaestro at gmail.com> escribió:

> On Mar 28, 11:14 am, "Gabriel Genellina" <gagsl-... at yahoo.com.ar>
> wrote:
>> En Fri, 28 Mar 2008 11:51:10 -0300, <pianomaes... at gmail.com> escribió:
>>
>> > How can this module access global state (not per-thread state) ?
>> > It needs to create a singleton.
>>
>> C global variables are global, not per-thread.
>
> Yes, but they get initialized once per-thread, therefore my singleton
> gets
> created multiple times.

Either don't call the initialization from every thread, or guard it with  
something like:

a_big_object my_object = NULL;
...

if (my_object!=NULL) {
    ...initialize object...
    my_object = ...;
}

That's a pretty standard idiom I think.

-- 
Gabriel Genellina




More information about the Python-list mailing list