Hi all,
This will probably be a long question/short answer, sorry, but I have
wandered net about the subject and really feel cannot find just enough
information.I want to ask my question by giving an example to
explicitly express my understanding which may be also wrong:
So, let's have a string in a module like:
st = 'IAmAstring'
My understanding: Python allocates a string object and binds the st to
that string's reference.
and let's have two threads simply doing following operations on the st
string:
Thread A:
st += 'ThreadAWasHere '
Thread B
st = 'ThreadBWasHere '
Let's think about the following situation:
Thread A reads the st value, and it is currently binded to an object
which is holding the string 'IAmAString'.
So let Thread B starts execution at that point. Note that Thread A
does not add the new string to object, it only reads st object. So,
then Thread B just writes to st, and all of a sudden st is binded to
something which is ThreadBWasHere. And then let's continue Thread A,
it already readed st's reference which is a reference to 'IamAString'.
So, finally if the execution flow is like above, we will have
'IAmAStringThre adAWasHere'. I am assuming this should not be the case.
We should have 'ThreadBWasHere ThreadAWasHere' in the final string,
because we have just consumed the code in ThreadB.
Also, the other question is the operation st = 'ThreadBWasHere ' is
atomic? I mean, if Python does not guarantee if an immutable object
assignment is atomic, then how can we say that the object is thread-
safe? So, if it is an atomic operation, which operators are atomic,
means only assignment'='? How about other operators like +=, or -
=..etc?
I am confused about which data structure to rely on thread-safety, or
operator in Python?
Regards, 2 2186
k3xji wrote:
Hi all,
This will probably be a long question/short answer, sorry, but I have
wandered net about the subject and really feel cannot find just enough
information.I want to ask my question by giving an example to
explicitly express my understanding which may be also wrong:
So, let's have a string in a module like:
st = 'IAmAstring'
When you make an assignment like that, python generates bytecode that
looks like this(straight from dis.dis):
1 0 LOAD_CONST 0 ('IAmAstring')
3 STORE_NAME 0 (a)
That's 1 instruction to load the constant and another one to bind it.
My understanding: Python allocates a string object and binds the st to
that string's reference.
and let's have two threads simply doing following operations on the st
string:
Thread A:
st += 'ThreadAWasHere '
Thread B
st = 'ThreadBWasHere '
Let's think about the following situation:
Thread A reads the st value, and it is currently binded to an object
which is holding the string 'IAmAString'.
So let Thread B starts execution at that point. Note that Thread A
does not add the new string to object, it only reads st object. So,
then Thread B just writes to st, and all of a sudden st is binded to
something which is ThreadBWasHere. And then let's continue Thread A,
it already readed st's reference which is a reference to 'IamAString'.
So, finally if the execution flow is like above, we will have
'IAmAStringThre adAWasHere'. I am assuming this should not be the case.
We should have 'ThreadBWasHere ThreadAWasHere' in the final string,
because we have just consumed the code in ThreadB.
Assuming that thread A and B contain nothing but that code, you will
either get 'ThreadBWasHere ThreadAWasHere' or just 'ThreadBWasHere ',
because by default the interpreter switches threads every 100 bytecode
instructions.
Also, the other question is the operation st = 'ThreadBWasHere ' is
atomic? I mean, if Python does not guarantee if an immutable object
assignment is atomic, then how can we say that the object is thread-
safe? So, if it is an atomic operation, which operators are atomic,
means only assignment'='? How about other operators like +=, or -
=..etc?
The augmented assignment operators first load the current value of the
variable like this:
a += 'asdf'
becomes
1 0 LOAD_NAME 0 (a)
3 LOAD_CONST 0 ('asdf')
6 INPLACE_ADD
7 STORE_NAME 0 (a)
>
I am confused about which data structure to rely on thread-safety, or
operator in Python?
All of the builtin functions(which are implemented in C, like len()) are
atomic(but assigning their output to a value may not be).
I hope this helps,
AlcariTheMad
En Sun, 26 Oct 2008 23:25:09 -0200, Alcari The Mad
<Al**********@g mail.comescribi ó:
>I am confused about which data structure to rely on thread-safety, or operator in Python?
All of the builtin functions(which are implemented in C, like len()) are
atomic(but assigning their output to a value may not be).
You can't count on the builtins being atomic. len(x) executes
type(x).__len__ if such method exists, which may execute arbitrary Python
code, even trigger the garbage collector and run absolutely unrelated
things.
See this effbot page for discussion [1] - but in general, since the
language reference doesn't specify whether an operation is atomic or not,
you should not count on it. Use a lock when required.
[1] http://effbot.org/pyfaq/what-kinds-o...hread-safe.htm
--
Gabriel Genellina This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Zalek Bloom |
last post by:
I am learning about difference between String and StringBuffer
classes. In a book it sayes that a String class is immutable, that
means that one an instance of String class is created, the string it
contais cannot be changed, only reference will change to point to a
different string. So results will be different in the following code...
|
by: Torsten Mohr |
last post by:
Hi,
reading the documentation (and also from a hint from this NG)
i know now that there are some types that are not mutable.
But why is it this way?
From an overhead point of view i think it is not optimal,
for example for a large string it could be much faster to
have it changed in place, not generating a new one for
|
by: Mapisto |
last post by:
Hi,
I've noticed that if I initialize list of integers in the next manner:
>>> my_list = * 30
It works just fine, even if I'll try to assign one element:
>>> id( my_list )
10900116
|
by: Andrew Quine |
last post by:
Hi
Just read this article http://www.artima.com/intv/choices.html.
Towards the end of the dicussions, when asked "Did you consider
including support for the concept of immutable directly in C# and the
CLR?" Anders' reply included this comment: "The concept of an
immutable object is very useful, but it's just up to the author to say
that...
|
by: Johnny Cash |
last post by:
Can anyone tell me how to make a Hashtable immutable? I see that
Hashtable has the "IsReadOnly" property, but I can't see how I am
supposed to actually make it immutable.
Thanks!
BCOT.
| |
by: Ben Finney |
last post by:
Howdy all,
I've recently packaged 'enum' in PyPI. In its description, I make the
claim that it creates "immutable" enumeration objects, and that the
enumeration values are "constant" values.
This raises questions.
Is there any difference between a Python immutable value, and a
constant? I suppose "constant" also implies that the *name*...
|
by: yccheok |
last post by:
I have an immutable object, where I do not provide implementation on
=operator. However, I am facing a problem when trying to use it with
stl map. stl map requires the object to have =operator being
overloading.
The following is the source of my immutable object:
Any suggestion on how to insert immutable object into the stl map is
very...
|
by: Ben Finney |
last post by:
Howdy all,
How can a (user-defined) class ensure that its instances are
immutable, like an int or a tuple, without inheriting from those
types?
What caveats should be observed in making immutable instances?
--
\ "Love is the triumph of imagination over intelligence." -- |
|
by: Sam Kong |
last post by:
Hi group,
I want to have some advice about immutable objects.
I made a constructor.
function Point(x, y) {
this.x = x;
this.y = y;
}
|
by: InDepth |
last post by:
Now that .NET is at it's fourth release (3.5 is coming soon), my very humble
question to the gurus is:
"What have we won with the decision to have string objects immutable? Or did
we won?"
Ok. It's a broad, and maybe a very silly question to ask, but still.
I mean, what good has it brought to us? What advantages immutable strings
have...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |