Is python memory shared between theads?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Wesley Henwood

    #1

    Is python memory shared between theads?

    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.

  • Grant Edwards

    #2
    Re: Is python memory shared between theads?

    On 2006-12-01, Wesley Henwood <wesleyhenwood@ hotmail.comwrot e:
    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!

    Comment

    • John Henry

      #3
      Re: Is python memory shared between theads?


      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.

      Comment

      • Daniel Dittmar

        #4
        Re: Is python memory shared between theads?

        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

        Comment

        • sjdevnull@yahoo.com

          #5
          Re: Is python memory shared between theads?

          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.

          Comment

          • Klaas

            #6
            Re: Is python memory shared between theads?


            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

            Comment

            Working...