On Jan 12, 3:51 am, Fredrik Lundh <[EMAIL PROTECTED]> wrote: > [EMAIL PROTECTED] wrote: > > I'm writing an NxN observer pattern, mostly for my own personal > > exploration. Two threads -might- be calling 'Disconnect' at the same > > time, and I can't even guarantee that the function runs properly. > > > for emelem in [ e for e in emlist if e.func is func ]: > > try: > > emlist.remove( emelem ) > > except ValueError: > > pass > > so use a lock. it's a whopping two lines of code: > > creation: > > lock = threading.Lock() > > usage: > > with lock: > for emelem in ... > ... > > more here: > > http://effbot.org/zone/thread-synchronization.htm > > and btw, looping over a list to figure out what you want to remove from > that list is a bit pointless. better just create a new list: > > with lock: > # get rid of all func instances > emlist = [e for e in emlist if e.func is not func] > > an alternative approach would be to replace emlist with a dictionary, > keyed on func objects. that'll let you remove all items associated with > a given function with a single atomic operation: > > del emdict[func] > > </F>
-> so use a lock. it's a whopping two lines of code: Yes. 1) I'm wondering what the specifics are on [Rubin]: >>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.<< I considered these suggestions early on. They apply often but I ruled them out for reasons: 2) List is referenced by others; concurrent modifications may be going on; can not replace it. Can I make asynchronous modifications and merge the changes, SCM-style? 3) Dictionary returns non-static order; order is important. Create a int-func tuple and sort dictionary results? Perhaps. That's sounding pretty good. -- http://mail.python.org/mailman/listinfo/python-list