Is python memory shared between theads?

W

Wesley Henwood

So I declare a variable named A in thread1, in script1.py. I assign
the value of 2.5 to A. I then run script2.py in thread2. Script2.py
assigns the value of 5.5 to a variable named A. Now, when thread1
resums execution, I see that A = 5.5, rather than 2.5 as I expected.

Is this normal behavior? Based on the little documentation I have been
able to find on this topic, it is normal behavior. The only way to use
same-named variables in scripts is to have them run in a different
process, rather than different threads.
 
G

Grant Edwards

So I declare a variable named A in thread1, in script1.py. I assign
the value of 2.5 to A. I then run script2.py in thread2. Script2.py
assigns the value of 5.5 to a variable named A. Now, when thread1
resums execution, I see that A = 5.5, rather than 2.5 as I expected.

Is this normal behavior?

Yes. Threads share global namespace. From a CPU point of
view, they share a single address space.
Based on the little documentation I have been able to find on
this topic, it is normal behavior. The only way to use
same-named variables in scripts is to have them run in a
different process, rather than different threads.

Or make them local variables.
 
J

John Henry

Wesley said:
So I declare a variable named A in thread1, in script1.py. I assign
the value of 2.5 to A. I then run script2.py in thread2. Script2.py
assigns the value of 5.5 to a variable named A. Now, when thread1
resums execution, I see that A = 5.5, rather than 2.5 as I expected.

Is this normal behavior? Based on the little documentation I have been
able to find on this topic, it is normal behavior. The only way to use
same-named variables in scripts is to have them run in a different
process, rather than different threads.

Yes and No.

local variables are local to each threads. Global variables are
global to the threads.
 
D

Daniel Dittmar

Wesley said:
So I declare a variable named A in thread1, in script1.py. I assign
the value of 2.5 to A. I then run script2.py in thread2. Script2.py
assigns the value of 5.5 to a variable named A. Now, when thread1
resums execution, I see that A = 5.5, rather than 2.5 as I expected.

Is this normal behavior?

Not if this is all you are doing. A variable A in script1.py and a
variable A in script2.py are completely different, even when running in
the same thread.

But if you're running script1.py and script2.py by calling execfile or
exec and you pass the same dictionary as the globals argument to
execfile, then the two scripts would share the global namespace.
Variables of the same name would really be the same variable.

Daniel
 
S

sjdevnull

Wesley said:
So I declare a variable named A in thread1, in script1.py. I assign
the value of 2.5 to A. I then run script2.py in thread2. Script2.py
assigns the value of 5.5 to a variable named A. Now, when thread1
resums execution, I see that A = 5.5, rather than 2.5 as I expected.

Is this normal behavior?

Yes. In fact, sharing memory is the whole reason threads exist. Use
processes in the (more common) case where you don't want memory
sharing, use threads when you do.
 
K

Klaas

John said:
Wesley Henwood wrote:

Yes and No.

local variables are local to each threads. Global variables are
global to the threads.

That is somewhat misleading. _All_ variables accessible from two
threads are shared. This includes globals, but also object attributes
and even local variables (you could create a closure to share a local
among threads).

The only reason locals appear "thread-local" is that locals are
"invokation-local" in that they are different bindings every time a
function is executed, and generally a single invokation of a function
is confined to a single thread.

Another way to share local variables is to create a generator, and call
..next() in two different threads... the local variables are
simulatneously modifiable by both threads.

FWIW, there is also threading.local().

-MIke
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,783
Latest member
RickeyDort

Latest Threads

Top