By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,403 Members | 1,089 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,403 IT Pros & Developers. It's quick & easy.

Is python memory shared between theads?

P: n/a
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.

Dec 1 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On 2006-12-01, Wesley Henwood <we***********@hotmail.comwrote:
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.

--
Grant Edwards grante Yow! If I felt any more
at SOPHISTICATED I would DIE
visi.com of EMBARRASSMENT!
Dec 1 '06 #2

P: n/a

Wesley Henwood wrote:
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.

Dec 1 '06 #3

P: n/a
Wesley Henwood wrote:
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
Dec 1 '06 #4

P: n/a
Wesley Henwood wrote:
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.

Dec 1 '06 #5

P: n/a

John Henry wrote:
Wesley Henwood wrote:
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.
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

Dec 1 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.