[Tutor] Exception thrown by socket.recv() and whacking threads

Jeroen Vogelpoel n.a.vogelpoel at chello.nl
Wed Jul 7 16:26:39 CEST 2004


Good day,

I'm currently busy with a script that involves some simple 
multithreading and sockets, however, I've run into some minor problems 
regarding exception handling and shutting down/killing threads. Here is 
a simple snippet of code with some of the relevant code:

===
def run( self ):
     if self.boolConnected == False:
         return 0
     while True:
         try:
             strReceived = self.socket.recv( 1024 )
         except Exception:
             exit()
         else:
             self.intTotalBytesRecv += len( strReceived )
             self.strBuffer += strReceived
             for strMessage in self._parseNewlines():
                 self.funcOutput( strMessage )
===

socket.recv() throwns an Exception "error" when the socket is being 
disconnected if it's still trying to read from the socket. Is there not 
a nice and clean way to catch and handle "error" exceptions, other then 
catching "Exception"? Besides, I'd expect an "IOError" when reading from 
a disconnected socket, not some uncatchable semi-exception 
thingamajig... What gives?

Also, I wonder wether it's possible to shut down/kill threads ( 
threading.Thread to be precise ) outside of the main run() method. That 
way, the entire deal with aforementioned exceptions can be ignore as I 
can just kill the thread, then disconnect the socket.

Cheers,
  - Jeroen Vogelpoel


More information about the Tutor mailing list