List iterator thread safety

Hendrik van Rooyen hendrik at microcorp.co.za
Fri Aug 28 03:54:51 EDT 2009


On Thursday 27 August 2009 16:50:16 Carl Banks wrote:
> On Aug 27, 7:25 am, Hendrik van Rooyen <hend... at microcorp.co.za>
> wrote:

> > Its not too bad - if you crook a bit - the trick is that you iterate over
> > the list backwards when you are removing stuff based on index, so that
> > the remainder does not get jumbled up by losing their positions, as
> > happens when you do it going forwards.
>
> That's only if you remove the "current item".  The OP has different
> threads accessing the list at the same time, so I have to assume that
> item being remove is not necessarily the current iteration.

Sorry - I did not pick that up - The threading screws the simple scheme up.
In such a situation I would have a thread to run the list - almost like a mini 
data base - and link the lot together using queues.  It is still a bugger, 
though, as one thread could try to kill something that is "checked out" to 
another one.  Then you have to take hard decisions, and a list is the wrong 
structure, because you have to remember state.  Better to use a dict, so you 
can at least mark a thing as "dead", and check for that before you update.

8<-------- example --------

> For the record, I use a more sophisticated system that explicitly
> resolves cause and effect in my games.  That's probably beyond the
> scope of this thread, though.

Yes - it is hairy - and there are probably as many different solutions as 
there are programmers, and then some.  :-)

- Hendrik



More information about the Python-list mailing list