removeall() in list

castironpi at gmail.com castironpi at gmail.com
Sat Jan 12 12:47:37 EST 2008


On Jan 12, 11:22 am, Rhamphoryncus <rha... at gmail.com> wrote:
> On Jan 12, 1:37 am, castiro... at gmail.com wrote:
>
>
>
> > On Jan 11, 8:04 pm, Paul Rubin <http://phr...@NOSPAM.invalid> wrote:
>
> > > castiro... at gmail.com writes:
> > > > Could you:
>
> > > > lockerA= Locker( listA, listB )
> > > > lockerA.op( listB.reverse )
> > > > lockerA.op( listA.pop )
>
> > > > Where lockerA ops acquire the locks on all its threads?
>
> > > I don't understand that question.  The main thing to understand is
> > > that multi-threaded programming is complicated (especially if you're
> > > after high performance), and very difficult to get right without
> > > knowing exactly what you're doing.  The generally preferred approach
> > > in Python is to keep things simple at some performance cost.
>
> > > Your Locker approach above looks sort of reasonable if you can be
> > > absolutely sure that nothing else can mess with listA or listB
> > > concurrently with those locker operations.  Normally you would put
> > > listA and listB into a single object along with a lock, then do
> > > operations on that object.
>
> > > You might check the Python Cookbook for some specific recipes and
> > > sample code for this stuff.  If you've used Java, Python's general
> > > threading mechanisms are similar, but they are in the library rather
> > > than built into the language (i.e. there is no "synchronized"
> > > keyword, you have to do that locking explicitly).
>
> > > What is the actual application, if you don't mind saying?  Are you
> > > sure that you really need concurrency?
>
> > 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
>
> Is there a reason you're using a list, rather than a dict?  Note that
> each call to list.remove() is O(n), whereas deleting a key from a dict
> is O(1).

4.  List-only solution:
        Use a dictionary, map item to its index.
        To retrieve, sort on value, not key

Sure: you need to maintain order of the things you're guarding.  I
need this to happen first, then this second.  My application is event
callbacks, that you register in a certain order; think GUIs and the
Gamma et al. Chain of Responsibility pattern.  This is a circumstance;
in a lot of applications order doesn't matter and you can use a hash.

But even so, you still can't check membership during concurrent
operation.  In other words, to remove all elements E such that E.func
is FuncF, the simple
   for e in setofe[:]:
      if e.func is not FuncF: continue
      setofe.remove( e )
still doesn't work.



More information about the Python-list mailing list