removeall() in list

Paul Rubin http
Fri Jan 11 18:26:42 EST 2008


castironpi at gmail.com writes:
> This function just wants X out of the list.  It doesn't matter if this
> happens before, during, or after something else; so long as it happens.

If you're asking in a general way how to do something like that,
there are several basic methods:

1. Put a single thread in charge of the list, and communicate with it
by message passing through Queues.  To get X out of the list, you'd
send the mutator thread a message asking for removal.  The mutator
thread would loop reading and processing messages from the queue,
blocking when no requests are pending.  This is sort of the preferred
Python style and is pretty simple to get correct, but if there are
many such objects you can end up with more threads than you really
want.

2. Associate a lock with the list.  Anything wanting to access the
list should acquire the lock, do its stuff, then release the lock.
This gets confusing after a while.

3. Various other schemes involving finer grained locks etc. that
get horrendously error prone (race conditions etc).

There is probably a good tutorial somewhere about programming with
threads.  It's sometimes a tricky subject, so it's worth taking
the time to study various approaches rather than re-inventing the wheeel.



More information about the Python-list mailing list