Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > threads, mutual exclusion, and lists

Reply
Thread Tools

threads, mutual exclusion, and lists

 
 
Scott
Guest
Posts: n/a
 
      08-15-2007
I have two threads that share a python list. One thread adds to the
list with append(), the other thread removes items with pop().

My question is -- are python list operations atomic? If they are not,
then I assume I need to put some mutual exclusion around the append()
and pop() calls ?

Thanks,
Scott

 
Reply With Quote
 
 
 
 
danmcleran@yahoo.com
Guest
Posts: n/a
 
      08-15-2007
On Aug 15, 1:36 pm, Scott <(E-Mail Removed)> wrote:
> I have two threads that share a python list. One thread adds to the
> list with append(), the other thread removes items with pop().
>
> My question is -- are python list operations atomic? If they are not,
> then I assume I need to put some mutual exclusion around the append()
> and pop() calls ?
>
> Thanks,
> Scott


You might want to consider using a Queue instead. It is designed to be
thread-safe.

 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=
Guest
Posts: n/a
 
      08-15-2007
> I have two threads that share a python list. One thread adds to the
> list with append(), the other thread removes items with pop().
>
> My question is -- are python list operations atomic?


Yes, they are in the current implementation of CPython (all versions).
Notice that not *all* operations are atomic (e.g. .sort() is not),
but both append and pop happen to be atomic (which is primarily because
they don't need to call back to user-defined functions, unlike sort).

It's not a language property, though; things may be different in Jython
or IronPython.

Regards,
Martin
 
Reply With Quote
 
Matt McCredie
Guest
Posts: n/a
 
      08-15-2007
> My question is -- are python list operations atomic? If they are not,
> then I assume I need to put some mutual exclusion around the append()
> and pop() calls ?


They are not, but there is one included in the standard library:
http://docs.python.org/dev/lib/module-Queue.html

Matt
 
Reply With Quote
 
=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=
Guest
Posts: n/a
 
      08-16-2007
>> My question is -- are python list operations atomic? If they are not,
>> then I assume I need to put some mutual exclusion around the append()
>> and pop() calls ?

>
> They are not, but there is one included in the standard library:
> http://docs.python.org/dev/lib/module-Queue.html


Why do you think they are not?

Regards,
Martin
 
Reply With Quote
 
Matt McCredie
Guest
Posts: n/a
 
      08-16-2007
> Why do you think they are not?

Because they aren't. You even mentioned that a few operations that
aren't atomic. If operations are atomic it isn't necessarily because
of the design of the list, but the design of CPython. More
specifically the GIL. I don't mean to imply that you can't get a
multi-threaded app to communicate using lists, but the Queue is
explicitly built for it and better suited.

Matt
 
Reply With Quote
 
=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=
Guest
Posts: n/a
 
      08-16-2007
>> Why do you think they are not?
>
> Because they aren't. You even mentioned that a few operations that
> aren't atomic.


OTOH, the OP specifically asked for .append() and .pop(), which are
atomic.

Regards,
Martin
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ISIS and EIGRP mutual redistribution --Help! phillip Cisco 2 09-19-2008 08:27 PM
List of lists of lists of lists... =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==?= Python 5 05-15-2006 11:47 AM
.Net client and SSL mutual authentication : 403 Forbidden, client certificate not sent Mfenetre ASP .Net Security 11 10-12-2005 03:02 PM
Problems mutual redistributing of ISIS and EIGRP phillip Cisco 5 12-07-2004 07:49 PM
Mutual EJB possible? Vjeran Marcinko Java 3 10-17-2003 09:21 PM



Advertisments