My Object pool isnt working

Jason M Jurkowski ;002;icsg6; jmj8237 at cs.rit.edu
Fri Apr 4 18:06:39 EST 2003


On Fri, 4 Apr 2003, Michael Chermside wrote:

> > 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.
>
and the default number of bytecodes is 10.  see sys.setcheckinterval(n).
> 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)
> instead.
well a simple 'bad' scenario would be the following.

thread 1
for i in len(list):
	#do something with list[i]
thread 2
del list[x:]

if thread 1 has not finished iterating and comes upon the elements between
x and the end of the list a IndexError exception will be raised.
>
> -- Michael Chermside
>
>
>




More information about the Python-list mailing list