[Tutor] Threading + socket server (blocking IO)
Liam Clarke
ml.cyresse at gmail.com
Sun Feb 19 13:20:17 CET 2006
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.
Regards,
Liam Clarke
On 2/19/06, Liam Clarke <ml.cyresse at gmail.com> wrote:
> Thanks Kent, I'll try swapping it around and see how it goes.
>
> As for the setDaemon, my apologies. There's a
>
> while True:
> if msvcrt.kbhit():
> break
>
> loop afterwards, so at a keypress it exits, hence the daemon stuff.
>
>
> On 2/19/06, Kent Johnson <kent37 at tds.net> wrote:
> > Liam Clarke wrote:
> > > Hi,
> > >
> > > Just coming back to the server end of things. Kent, could I please ask
> > > you to confirm that I'm not doing anything abnormal with the
> > > ThreadingMixIn? The code is here at rafb:
> > > http://www.rafb.net/paste/results/915JVm90.html
> > >
> > > Basically, when I test it using a script to generate the datagrams via
> > > localhost, the server misses anywhere between 200 - 400 datagrams out
> > > of 1041.
> >
> > I don't have time for a detailed response but it looks OK except
> > ThreadingMixin needs to be the first base class because it overrides a
> > method of UDPServer. It turns out there is a class
> > SocketServer.ThreadingUDPServer that does this.
> >
> > Also I'm surprised the program stays running - ISTM with the server
> > thread marked daemon and the main thread exiting the program should exit...
> >
> > Kent
> >
> >
>
More information about the Tutor
mailing list