472,110 Members | 2,074 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,110 software developers and data experts.

threads, mutual exclusion, and lists

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

Aug 15 '07 #1
6 3931
On Aug 15, 1:36 pm, Scott <smba...@gmail.comwrote:
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.

Aug 15 '07 #2
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
Aug 15 '07 #3
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
Aug 15 '07 #4
>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
Aug 16 '07 #5
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
Aug 16 '07 #6
>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
Aug 16 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

23 posts views Thread by Antoon Pardon | last post: by
reply views Thread by Morten Gulbrandsen | last post: by
2 posts views Thread by Luk Vloemans | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.