My python program uses httplib2.Http to make an http request. As soon as I need to generate a request, I create an httplib2.Http object, so my program will often create / destroy httplib2.Http objects.
I found that my program crashed easily due to reaching the maximum number of open files. When checking / proc // fd, there were too many open fds. The problem made me look at the httplib2 source code.
Then I found that the httplib2.Http._conn_request method had this code:
else: content = "" if method == "HEAD": conn.close() else: content = response.read() response = Response(response) if method != "HEAD": content = _decompressContent(response, content) break
This shows that the socket only closes when the http method is HEAD. Maybe httplib2 wanted to reuse the socket somehow. But the Http class does not have a close () method. This means that when I make an Http request, the socket will not close until my process completes.
Then I changed the code:
else: content = "" if method == "HEAD": conn.close() else: content = response.read() response = Response(response) if method != "HEAD": content = _decompressContent(response, content) conn.close()
After that, my program worked well.
But I'm still curious if this is really an httplib2 error, given that httplib2 is a very old and general library.
source share