Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Threads and variable assignment

Reply
Thread Tools

Threads and variable assignment

 
 
Gregory Bond
Guest
Posts: n/a
 
      04-12-2005
I've had a solid hunt through the (2.3) documentation but it seems
silent on this issue.

I have an problem that would naturally run as 2 threads: One monitors a
bunch of asyncrhonous external state and decides if things are "good" or
"bad". The second thread processes data, and the processing depends on
the "good" or "bad" state at the time the data is processed.

Sort of like this:

Thread 1:

global isgood
while 1:
wait_for_state_change()
if new_state_is_good():
isgood = 1
else:
isgood = 0

Thread 2:

s = socket(....)
s.connect(...)
f = s.makefile()
while 1:
l = f.readline()
if isgood:
print >> goodfile, l
else:
print >> badfile, l

What guarantees (if any!) does Python make about the thread safety of
this construct? Is it possible for thread 2 to get an undefined
variable if it somehow catches the microsecond when isgood is being
updated by thread 1?
 
Reply With Quote
 
 
 
 
elbertlev@hotmail.com
Guest
Posts: n/a
 
      04-12-2005
Theoretically you have to use lock, while accesing the isgood instance,
but... practically noting bad can happen IN THIS PARTICULAR CASE, if
you don't. Python uses Global Interpreter Lock. In other words only
one thread is running at any particular moment. Thread scheduling is
preemptive, but "atomic" actions are not interrupted. Bollean asigment
is an atomic action.

WELCOME TO THE POWER OF MULTITHREADING

 
Reply With Quote
 
 
 
 
David M. Cooke
Guest
Posts: n/a
 
      04-12-2005
Gregory Bond <> writes:

> I've had a solid hunt through the (2.3) documentation but it seems
> silent on this issue.
>
> I have an problem that would naturally run as 2 threads: One monitors
> a bunch of asyncrhonous external state and decides if things are
> "good" or "bad". The second thread processes data, and the processing
> depends on the "good" or "bad" state at the time the data is processed.
>
> Sort of like this:
>
> Thread 1:
>
> global isgood
> while 1:
> wait_for_state_change()
> if new_state_is_good():
> isgood = 1
> else:
> isgood = 0
>
> Thread 2:
>
> s = socket(....)
> s.connect(...)
> f = s.makefile()
> while 1:
> l = f.readline()
> if isgood:
> print >> goodfile, l
> else:
> print >> badfile, l


You said that the processing depends on the good or bad state at the
time the data is processed: I don't know how finely-grained your state
changes will be in thread 1, but it doesn't seem that thread 2 would
notice at the right time. If the socket blocks reading a line, the
state could change i

> What guarantees (if any!) does Python make about the thread safety of
> this construct? Is it possible for thread 2 to get an undefined
> variable if it somehow catches the microsecond when isgood is being
> updated by thread 1?


It won't be undefined, but it's possible that (in thread 1)
between the "if new_state_is_good()" and the setting of isgood that
thread 2 will execute, so if new_state_is_good() was false, then it
could still write the line to the goodfile.

It really depends on how often you have state changes, how often you
get (full) lines on your socket, and how much you care that the
correct line be logged to the right file.

If you needed this to be robust, I'd either:

- Try to rewrite wait_for_status_change()/new_state_is_good() to be
asynchronous, particularly if wait_for_status_change() is blocking
on some file or socket object. This way you could hook it into
asynchat/asyncore or Twisted without any threads.

- Or, if you need to use threads, use a Queue.Queue object where
timestamps w/ state changes are pushed on in thread 1, and popped
off and analysed before logging in thread 2. (Or something; this
just popped in my head.)

--
|>|\/|<
/--------------------------------------------------------------------------\
|David M. Cooke
|cookedm(at)physics(dot)mcmaster(dot)ca
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Assignment operator self-assignment check Chris C++ 34 09-26-2006 04:26 AM
Implicit iterator variable $_ changing to ### upon variable assignment? Derek Basch Perl Misc 8 08-12-2006 06:30 PM
waiting Threads and processor assignment martin Java 0 08-07-2006 01:00 PM
Augument assignment versus regular assignment nagy Python 36 07-20-2006 07:24 PM
[new to threads] threads with UI and loop Une bévue Ruby 0 06-14-2006 10:22 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57