As requested.

---------- Forwarded message ----------
From: Kent Johnson <[EMAIL PROTECTED]>
Date: Feb 20, 2006 2:39 PM
Subject: Re: [Tutor] Threading + socket server (blocking IO)
To:
Cc: [EMAIL PROTECTED]


Liam Clarke wrote:
> Hi,
>
>
> Just poking at threads, I'm contemplating doing something that I think
> may be a very dumb thing to do, or it may work fine.
>
> In the following code -
>
>
> import threading
> import Queue
> import reg
> import msvcrt
>
> class ParseThread(threading.Thread):
>
>     def __init__(self, Q, parser):
>         self.Q = Q
>         self.parser = parser
>         self.sendIt = False
>         threading.Thread.__init__(self)
>
>     def run(self):
>         while True:
>             if self.sendIt:
>                 parser.send_data()
>                 self.sendIt = False
>             try:
>                 data = self.Q.get(False)
>                 self.parser.check(data)
>             except Empty:
>                 continue
>
>
> if __name__ == "__main__":
>
>     Q = Queue.Queue()
>     parser = reg.Parser()
>
>     t1 = ParseThread(Q, parser)
>     t1.start()
>
>     while True:
>         if msvcrt.kbhit():
>             if msvcrt.getch()=="S":
>                 t1.sendIt = True
>
>
> It's the sendIt attribute in t1. Is setting an attribute of a running
> thread a very bad thing, or a just be careful thing? The only usages
> for the attribute are as above, if it's set to True. the thread calls
> a method of the parser object, and then resets it to False.
>
> I can see an interesting collision if an external source sets
> self.sendIt to True as the thread sets it to False.
>
> I intend to use this with a timer, so that very x minutes, the
> attribute is changed, which hopefully sidesteps that particular
> collision.
>
> But yes, very unfamiliar territory, and I'm worried about sowing the
> seeds of my own destruction by being too clever/dumb.

Setting an attribute in one thread and checking it in another is OK, I
think. But setting an atribute in two threads raises the possibility of
a race condition. There is definitely a chance of missing a setting of
sendIt to true:

ParseThread                    main thread

if self.sendIt: # if sendIt is already true
                                t1.sendIt = True # main thread sets it
true again
   parser.send_data()
   self.sendIt = False # set it false

If the test is much more frequent than the set, this is unlikely but it
is a weakness in the design.

Also you have a busy loop (an infinite loop that just keeps polling),
you should at least put a sleep in it, better is to use a get() with
timeout or a blocking get().

I don't understand what your run() loop is doing - it seems to read data
from the queue and throw it away, optionally calling parser.send_data().
Can you explain what you are trying to do? Presumably this is part of
your UDP server?

Kent

PS My mail server has been blacklisted, can you forward this to the
tutor list?
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to