473,396 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 3995
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: Antoon Pardon | last post by:
I have had a look at the signal module and the example and came to the conclusion that the example wont work if you try to do this in a thread. So is there a chance similar code will work in a...
0
by: Morten Gulbrandsen | last post by:
Hello Programmers, Thank you for the kind advice, I have another small problem with MySQL I'd like to investigate, All I get is this:=20 ERROR 1005 at line 47:=20 Can't create table...
5
by: clusardi2k | last post by:
Hello, I have a assignment just thrown onto my desk. What is the easiest way to solve it? Below is a brief description of the task. There are multible programs which use the same library...
2
by: Luk Vloemans | last post by:
Hey, Having a few problems using threads in C#. In the msdn documentation, mutual exclusion is only discussed when dealing with threads that all execute the same method. What's the actual...
9
by: bonk | last post by:
Does anyone have a simple example on how to prohibit that any thread other than the current thread modifies a certain object (a collection) while we are in a certain section of the code? In other...
5
by: iLL | last post by:
So why is it that we need to use the Invoke method when updating the GUI? The following is the way we are suppose to update GUI components: delegate void textIt(object o); public partial...
167
by: darren | last post by:
Hi I have to write a multi-threaded program. I decided to take an OO approach to it. I had the idea to wrap up all of the thread functions in a mix-in class called Threadable. Then when an...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.