trouble using signal

mnietz okumee at compuserve.de
Wed Apr 16 08:26:00 EDT 2003


Hi Folks,

I am trying to use signals for a timeout in a http-request as
following:


#------------------------------------------------------------------------------
def requestUrl(host, data):
    """starts a http-request for given data to the given url
       and returns status code. If request will not be answered in
specifiend
       time, status 404 will be returned
    """
    conn = httplib.HTTPConnection(host)
    conn.request("GET", data)
   
    status = 404
    content = None
    
    signal.signal(signal.SIGALRM, requestUrlTimoutHandler)
    signal.alarm(10)
    
    r = conn.getresponse()
    
    signal.alarm(0)
    
    status = r.status
       
    if status == 200:
       content = r.read()
    
    return (status, content)   

#------------------------------------------------------------------------------
def requestUrlTimoutHandler(signum, frame):
    print "the url request timed out, status stays = 404"



but that will bring up following Exception:
....
  File "/usr/local/lib/python2.2/httplib.py", line 760, in getresponse
    response.begin()
  File "/usr/local/lib/python2.2/httplib.py", line 269, in begin
    version, status, reason = self._read_status()
  File "/usr/local/lib/python2.2/httplib.py", line 231, in
_read_status
    line = self.fp.readline()
IOError: [Errno 4] Interrupted system call


Is the reason for this, that r.read is interrupted by the
timeout-handler? But timeout handling should work this way. Or should
I just catch the exception and ignore it?

Thanks for any advice,

Best Regards,
Matthias




More information about the Python-list mailing list