How to get a raised exception from other thread

Antoon Pardon apardon at forel.vub.ac.be
Wed Oct 19 03:11:15 EDT 2005


Op 2005-10-18, dcrespo schreef <dcrespo at gmail.com>:
>> Before, after, or during the .start() call, or somewhere else?
>
> I'd like to catch *just after* the .start() call.
>
>> I'm quite sure the problem you are trying to solve can be solved, but
>> you are still describing part of the solution you believe you need,
>> rather than explaining why you want to do this (which may let us show
>> you other, simpler or cleaner ways to accomplish your goals).
>
> The thing is that I have in ProgramB a class derived from
> threading.Thread, that runs a TCPServer, listening to a port on local
> ip address.
>
> As you surely know, you have to specify certain parameters when
> instantiating the tcpserver:
>
> SocketServer.TCPServer(server_address, RequestHandlerClass)
> where server_address is a tuple (ip,port)
> ip has to be one of three modes of values (If I remember well):
>
> 1. A null string ''
>    Listens on all IP local address
>
> 2. A string containing the local IP address where you want it to listen
>    Listens only in the specified local IP address
>
> 3. A string containing "localhost".
>    Listens only for connections from "localhost".
>
> Here comes the problem:
> When you specify the case number 2, the IP must be valid for the
> computer where the program runs, otherwise, it raises an exception
> saying that "Can't assign requested address".
> The TCPServer class, defined in a module, is ran (instantiatedly) from
> the main program through a started thread. The thread also is in the
> same module as TCPServer class.
> It looks like (it's much more code than this. If you want the whole
> code, tell me):
>
> MainProgram.py
> ...
> SrvrTCP = module.ThreadedTCPServer(ip,port)
> SrvrTCP.start()
> #Here, I want to know if the TCPServer started well.
> ...
>
>
> module.py
> ...
> class ThreadedTCPServer(threading.Thread):
>
>     def __init__(self, ip,port):
>         threading.Thread.__init__(self)
>         self.ip= ip
>         self.port= port
>
>     def run(self):
>         TCPServer((self.ip,self.port)) #Here, if the self.ip is
> invalid, it raises an exception.

Just a suggestion, but since it is the main thread you want notified,
you could use signals here. Something like the following:

MainProgram.py

class TCPProblem(Exception):
  pass

def signalcallback(signum, frame):
  raise TCPProblem

signal.signal(signal.SIGHUP, signalcallback)

SrvrTCP = module.ThreadedTCPServer(ip,port)
SrvrTCP.start()
...


module.py

mainpid = os.getpid()

class ThreadedTCPServer(threading.Thread):

    def __init__(self, ip,port):
        threading.Thread.__init__(self)
        self.ip= ip
        self.port= port

    def run(self):
        try:
          TCPServer((self.ip,self.port)) #Here, if the self.ip is invalid, it raises an exception.
	except ...:
	  os.kill(mainpid, signal.SIGHUP)
	  
-- 
Antoon Pardon




More information about the Python-list mailing list