Python线程停留在_ssl.sslwrap

时间:2016-01-06 22:31:13

标签: python multithreading python-2.7 python-requests

我有一个多线程Python(2.7.6)应用程序,有时在调用requests.post时遇到问题(该库的版本为2.5.1)。这是我得到的堆栈跟踪(通过调用sys._current_frames()找到):

"/usr/local/lib/python2.7/dist-packages/requests/api.py", line 99 in post
  return request('post', url, data=data, json=json, **kwargs)
"/usr/local/lib/python2.7/dist-packages/requests/api.py", line 49 in request
  response = session.request(method=method, url=url, **kwargs)
"/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 461 in request
  resp = self.send(prep, **send_kwargs)
"/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 573 in send
  r = adapter.send(request, **kwargs)
"/usr/local/lib/python2.7/dist-packages/requests/adapters.py", line 370 in send
  timeout=timeout
"/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/connectionpool.py", line 518 in urlopen
  body=body, headers=headers)
"/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/connectionpool.py", line 322 in _make_request
  self._validate_conn(conn)
"/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/connectionpool.py", line 727 in _validate_conn
  conn.connect()
"/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/connection.py", line 238 in connect
  ssl_version=resolved_ssl_version)
"/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/util/ssl_.py", line 254 in ssl_wrap_socket
  return context.wrap_socket(sock)
"/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/util/ssl_.py", line 80 in wrap_socket
  return wrap_socket(socket, ciphers=self.ciphers, **kwargs)
"/usr/lib/python2.7/ssl.py", line 487 in wrap_socket
  ciphers=ciphers)
"/usr/lib/python2.7/ssl.py", line 241 in __init__
  ciphers)

检查ssl.py的第241行,我发现它是此次通话的最后一行:

self._sslobj = _ssl.sslwrap(self._sock, server_side,
                            keyfile, certfile,
                            cert_reqs, ssl_version, ca_certs,
                            ciphers)

当线程卡在这里时,它们似乎永久卡住了。在我不得不重新启动应用程序之前,我能够看到24个线程在这里停留了大约两天。我使用requests相当多,而绝大多数时间没有问题,但由于某种原因,24个线程都被卡在几分钟的空间内。有谁知道这里发生了什么?

注意:我不能一直重现它,只是让它运行并等待一批线程最终卡住。

0 个答案:

没有答案