theads & global namespaces

rdev at my-deja.com rdev at my-deja.com
Wed Jun 30 15:18:38 EDT 1999


Two suggestions:
- If you're running on Solaris, the thread won't get to run
unless you give up some time.  So try putting a time.sleep(.001)
into your while loop.  On NT, this is not necessary (don't know
why.)
- Your thread will only increment once and then exit, so you'll
never see x>1.  If you want to keep the thread running, put it in
a while loop.  Make sure you give up the time slice there as well.

Roger Dev
dev at opticominc.com

In article <377A5C88.32A1FAB6 at notes.cba.ufl.edu>,
  andersdt at notes.cba.ufl.edu wrote:
> 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
>
>


Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.




More information about the Python-list mailing list