Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to stop a thread?

0 views
Skip to first unread message

M

unread,
Oct 10, 2002, 3:17:47 PM10/10/02
to
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.

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.

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 Liechti

unread,
Oct 10, 2002, 4:48:06 PM10/10/02
to
marv...@hotmail.com (M) wrote in
news:c0abefc3.02101...@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 <clie...@gmx.net>

sism...@hebmex.com

unread,
Oct 10, 2002, 4:22:48 PM10/10/02
to
> From: marv...@hotmail.com [mailto:marv...@hotmail.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.
>
> 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.

AH-HAH!!

Someone else with my same questions... :-)

>
> 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()
>

I did something almost-but-not-quite-completely-unlike your
approach. Mine's an HTTP server class with an inner loop
which listens for connections on the server socket,
but same as you, I didn't know how to wait for a
connection-yet-still-have-control.

It seems like select() is our friend here.

My loop loops a little like this:

def accept_request(self):
# Wait up to self._timeout seconds for incoming request.
l = select.select([self._socket], [], [], self._timeout)
# IF there's a request, accept connection. Else, exit.
if l:
csock, caddr = self._socket.accept()
ident = self.request_ident(caddr)
self.log("Got request from ", Client=caddr, ID=ident)
self.dispatch_request(csock, caddr, ident)

This little function __THEORETICALLY__ first checks if there's
incoming data on the socket for up to self._timeout seconds,
if there is, then it accepts the connection and processes
the request; else, it exits.

This helps a little, because I can tell the server between
calls to this function to exit if so-and-so conditions. Also,
having a non-zero timeout helps out all other threads.

Salutations all :-)

-gustavo


piter

unread,
Oct 11, 2002, 6:15:18 PM10/11/02
to
I did it otherwise:
I have couple of threads like:

class slave( SocketServer.BaseRequestHandler ):
def handle( self ):
threadId = thread.get_ident()
masterQueue.put( (threadId, self.request) )
while 1:
.
.
.

where masterQueue is a Queue instance that is read by master thread "master"
so it knows every connection and can close the socket when it wants...
s.close() or s.shutdown(2)

note that "master" thread gets both threadId and socket (self.request) the
thread is using


Uzytkownik "M" <marv...@hotmail.com> napisal w wiadomosci
news:c0abefc3.02101...@posting.google.com...

0 new messages