python - 使用 google-api-python-client 拉超时

标签 python python-2.7 ssl httplib2 google-cloud-pubsub

我正在尝试使用“returnImmediately”= False 设置用户定义的消息拉取超时:

    PUBSUB_SCOPES = ['https://www.googleapis.com/auth/pubsub']

    credentials = oauth2client.GoogleCredentials.get_application_default()
    if credentials.create_scoped_required():
        credentials = credentials.create_scoped(PUBSUB_SCOPES)

    http = httplib2.Http(timeout=timeout)
    credentials.authorize(http)

    return discovery.build('pubsub', 'v1', http=http)

当超时小于 90 秒时,我会收到以下错误:

resp = client.projects().subscriptions().pull(subscription=subscription, body=body).execute()
  File "venv\lib\site-packages\oauth2client\util.py", line 137, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "venv\lib\site-packages\googleapiclient\http.py", line 755, in execute
    method=str(self.method), body=self.body, headers=self.headers)
  File "venv\lib\site-packages\googleapiclient\http.py", line 93, in _retry_request
    resp, content = http.request(uri, method, *args, **kwargs)
  File "venv\lib\site-packages\oauth2client\client.py", line 622, in new_request
    redirections, connection_type)
  File "venv\lib\site-packages\httplib2\__init__.py", line 1609, in request
    (response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
  File "venv\lib\site-packages\httplib2\__init__.py", line 1351, in _request
    (response, content) = self._conn_request(conn, request_uri, method, body, headers)
  File "venv\lib\site-packages\httplib2\__init__.py", line 1307, in _conn_request
    response = conn.getresponse()
  File "C:\python27\Lib\httplib.py", line 1074, in getresponse
    response.begin()
  File "C:\python27\Lib\httplib.py", line 415, in begin
    version, status, reason = self._read_status()
  File "C:\python27\Lib\httplib.py", line 371, in _read_status
    line = self.fp.readline(_MAXLINE + 1)
  File "C:\python27\Lib\socket.py", line 476, in readline
    data = self._sock.recv(self._rbufsize)
  File "C:\python27\Lib\ssl.py", line 714, in recv
    return self.read(buflen)
  File "C:\python27\Lib\ssl.py", line 608, in read
    v = self._sslobj.read(len or 1024)
SSLError: ('The read operation timed out',)

谢谢。

最佳答案

不幸的是,这些客户端库不支持将超时值转发到服务器;然而,我们有 just announced the gRPC client libraries , 正确地将截止日期传递给服务器。

作为当前库的解决方法,请使用 returnImmediately=true,或将截止日期设置为高于 90 秒,正如您的问题所暗示的那样。

关于python - 使用 google-api-python-client 拉超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36140229/

相关文章:

python - Sklearn.LogisticRegression 是否添加一列 1?

python - 从哪里可以获得 python27.dll 和 wxmsw dll

python - Python 2.7 中 "Absolute Import"的正确方法

bash - 从 bash 脚本中确定证书仍然有效的天数

.net - 站点使用 SSL,但是 JSON 查询呢?

node.js - Haraka 未授权我的身份验证

python - 使用 scipy.io.wavfile 读取 wav 文件然后写入不会重现原始文件

python - Google App Engine 远程 Api 导入错误

bash - 在 shell 脚本中处理 python 返回的退出代码

python - Django 元类访问外部属性