stupid thread question

David Bolen db3l at fitlinxx.com
Wed Jun 14 22:07:13 EDT 2000


Michael Vanier <mvanier at endor.bbb.caltech.edu> writes:

> def callback(event):
>     global thread_on
>     key = event.char
> 
>     if key == "s":
>         if not thread_on:
>             stop_flag = 0
              ^^^^^^^^^^^^^
>             thread_on = 1
>             thread.start_new_thread(run, ())
>         else:
>             print "halting thread"
>             stop_flag = 1
              ^^^^^^^^^^^^^
>             thread_on = 0
>     elif key == "q":
>         raise SystemExit

You haven't stop_flag as global ("global stop_flag"), so the
highlighted operations have created a local (to the callback function)
variable, and thus are not changing the global variable, which the
thread function is correctly seeing.

This is definitely a somewhat subtle issue with Python scoping -
scopes are used dynamically but determined statically.  See near the
end of section 9.2 of the tutorial for some info.  See also sections
4.36 and 4.57 of the FAQ.

--
-- David
-- 
/-----------------------------------------------------------------------\
 \               David Bolen            \   E-mail: db3l at fitlinxx.com  /
  |             FitLinxx, Inc.            \  Phone: (203) 708-5192    |
 /  860 Canal Street, Stamford, CT  06902   \  Fax: (203) 316-5150     \
\-----------------------------------------------------------------------/



More information about the Python-list mailing list