My Object pool isnt working

Michael Chermside mcherm at
Fri Apr 4 22:24:56 CEST 2003

> Whew! I always thought that I must use locks to access any object in 
> Python before I modify it from a thread.
> Is it possible to append a list or a dictionary from a thread without 
> locking it? 

Yep. If you write two threads that both run around adding to a list
then the list will wind up with all items, but their order isn't
known (partial ordering of each thread's items IS preserved).

> What does thread-safe mean here?

Basically, it means that each individual bytecode of python (not each
statement) is operated in series, not parallel. Erm... to put it
better, even if you have multiple processors, your threads act as if
they were running on just _one_ cpu and timeslicing, never interrupting
the execution of a single bytecode. Except for threads that are
blocked waiting on I/O, or which are performing C code that is
"independent" of python (doesn't need to hold the GIL). This effect
is achieved because there's a single (global) interpreter lock (GIL)
which must be held for a thread to [start? complete?] a bytecode.

Somebody please correct me if I've gotten some details wrong in the
above explanation.

> For example, what would happen if I walk through a list in a for loop 
> while another thread modifies the list?

Don't do that... you might accidentally trigger the time machine and
risk altering the future of the universe. No, seriously, Python won't
crash, but your program might not do what you expect... it's not usually
recomended. Someone with deeper knowledge can probably explain EXACTLY
what will happen, but I suggest using a queue (see the Queue module) 

-- Michael Chermside

More information about the Python-list mailing list