Wednesday, October 26, 2011

Python: How to “kill” an hanging HTTPConnection object

This may typically happen when you have an httplib.HTTPConnection object which is performing getresponse() in another thread and is, well, waiting...

Suppose you have the urgent need to terminate it. You don’t have a terminate() method. What you need is the sock attribute (undocumented), which gives you access to the corresponding socket.Socket object. On this object you can call shutdown().

So you go:

myHTTPConnection.sock.shutdown(socket.SHUT_RDWR)

as in this (production) example:

https://github.com/gderosa/xbmc-vera/blob/a61e05964eee3b2a1536ccf19b7916a3bacebd3b/resources/lib/gui/main.py#L54.

No comments:

Post a Comment