472,784 Members | 966 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Immutable object thread-safety

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
'IAmAStringThreadAWasHere'. I am assuming this should not be the case.
We should have 'ThreadBWasHereThreadAWasHere' 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,
Oct 26 '08 #1
2 2138
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
'IAmAStringThreadAWasHere'. I am assuming this should not be the case.
We should have 'ThreadBWasHereThreadAWasHere' 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 'ThreadBWasHereThreadAWasHere' 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
Oct 27 '08 #2
En Sun, 26 Oct 2008 23:25:09 -0200, Alcari The Mad
<Al**********@gmail.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

Oct 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
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...
7
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...
6
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
48
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#...
4
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.
22
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. ...
2
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...
90
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...
3
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; }
16
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?" ...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.