How to stop a thread?
Chris Liechti
cliechti at gmx.net
Thu Oct 10 16:48:06 EDT 2002
marvelan at hotmail.com (M) wrote in
news:c0abefc3.0210101117.4db044fe at posting.google.com:
> How can I stop a thread that is waiting to handle a TCP
> socket?
>
> As there is no kill thread function in Python my first idea
> was to do a handle_request until I set a global magical_flag.
that's basicaly a good plan. maybe you want the terminate flag per thread.
that way you can shutdown single connections without killing the entire
app.
> But then I realised that this works only when I have a steady
> flow of connections comming in. Otherwise it will just hang
> (as it should) in handle_request and thus the thread would
> never stop.
you could make the socket non-blocking, but then it may use lots of cpu
power just to poll if there is data.
the ususal way to go is select.select() with that function you're able to
wait for data while having a timeout which allows to poll the terminate
flag.
if you don't want to do this "by hand" i sugest a look at TimeoutSocket
(search google)
chris
> So, how can I stop the thread in the example below? I would
> like to stop it when user presses the "stop" button in some
> way.
>
>
> class Foo(Thread):
>
> def run(self):
> self.server = SocketServer.ThreadingTCPServer(addr, h)
>
> while not magical_flag:
> self.server.handle_request()
>
--
Chris <cliechti at gmx.net>
More information about the Python-list
mailing list