Is HTTPSConnection thread-safe?

Joe Wong joewong at mango.cc
Tue Aug 24 01:48:49 EDT 2004


No, I have HTTPSConnection object in each thread. It seems that the exception happens when all threads try to execute .request() method for the very first time at the sametime...

- Joe

> Hi,
> I have a small program using HTTPSConnection to issue a POST on the
> server. When multiple threads of the same program is running, I got
> the error:
> Exception in thread Thread-3:
> Traceback (most recent call last):
> File "C:\PYTHON23\lib\threading.py", line 436, in __bootstrap
> self.run()
> File "C:\PYTHON23\lib\threading.py", line 416, in run
> self.__target(*self.__args, **self.__kwargs)
> File "mcstest.py", line 27, in thread_func
> conn.request("POST", CGI_PATH, params, headers)
> File "c:\python23\lib\httplib.py", line 718, in request
> self._send_request(method, url, body, headers)
> File "c:\python23\lib\httplib.py", line 739, in _send_request
> self.endheaders()
> File "c:\python23\lib\httplib.py", line 712, in endheaders
> self._send_output()
> File "c:\python23\lib\httplib.py", line 597, in _send_output
> self.send(msg)
> File "c:\python23\lib\httplib.py", line 564, in send
> self.connect()
> File "c:\python23\lib\httplib.py", line 985, in connect
> ssl = socket.ssl(sock, self.key_file, self.cert_file)
> File "C:\Python23\lib\socket.py", line 73, in ssl
> return _realssl(sock, keyfile, certfile)
> sslerror: SSL_CTX_new error
> in random order. Is there any fix / patches for this?
> Regards,
> - Joe

Are you passing the same HTTPSConnection object to a bunch of threads?
With just a cursory glance at the httplib module, it doesn't look like
it is thread safe (meaning I saw no thread imports, no references to
locks, etc.). If, for some reason, you feel it necessary to pass the
same HTTPSConnection object to multiple threads, you will probably want
to write a thread-safe wrapper for HTTPSConnection. I can't think why
you would want to pass the same conn object to multiple threads rather
than creating a new one per thread - unless you want to have one and
only one persistent connection among all threads (using keepalives, I
guess).

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20040824/e88873e6/attachment.html>


More information about the Python-list mailing list