How to stop a thread?

piter tojopiter at yahoo.com
Sat Oct 12 00:15:18 CEST 2002


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" <marvelan at hotmail.com> napisal w wiadomosci
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.
>
> 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()





More information about the Python-list mailing list