theads & global namespaces

Taylor Anderson Taylor.Anderson at p98.f112.n480.z2.fidonet.org
Wed Jun 30 12:06:01 CEST 1999


From: Taylor Anderson <andersdt at notes.cba.ufl.edu>

Greetings all,

I am trying to share globals between threads.  Is is possible for me to
share globals between threads that call functions from different
modules?  Thus far I have had little success.  I ask this question
because  I would prefer not to put all my code in one module.

For example, I have 2 .py files: a.py and b.py.  b.py contains the
function ('foo()') to be executed  under the new thread, as well as the
global variable 'x'.  a.py contains the call to
start_new_thread(foo,()).  a.py also tries to use the global variable x.

=========================================
b.py

x = 0

def foo():
   global x
   x  = x + 1

=========================================
a.py

from b.py import *

if __name__ == '__main__':

    start_new_thread(foo, ())
    while 1:
        if x != 0:
            print x

=========================================

So far, my code in a.py does not detect any change in x.  Any
suggestions for a workaround?

Many thanks,

Taylor




More information about the Python-list mailing list