Клиент Python google api случайным образом создает исключения

#python #python-3.x #google-api-python-client

Вопрос:

Я работаю с электронными таблицами Google, используя googleapiclient их, и постоянно вижу исключения в случайные моменты для операций чтения и записи (я не вижу никакого отношения к конкретным). Недавнее исключение:

 Traceback (most recent call last):
  File "/usr/lib/python3.9/http/client.py", line 519, in _read_next_chunk_size
    return int(line, 16)
ValueError: invalid literal for int() with base 16: b''

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/apps/myapp/myapp/logger/logger.py", line 228, in wrapper
    result = func(*args, **kwargs)
  File "/apps/myapp/myapp/services/gsuite/spreadsheet.py", line 80, in update
    get_spreadsheets_resource()
  File "/apps/myapp/.venv/lib/python3.9/site-packages/googleapiclient/_helpers.py", line 134, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/apps/myapp/.venv/lib/python3.9/site-packages/googleapiclient/http.py", line 920, in execute
    resp, content = _retry_request(
  File "/apps/myapp/.venv/lib/python3.9/site-packages/googleapiclient/http.py", line 191, in _retry_request
    resp, content = http.request(uri, method, *args, **kwargs)
  File "/apps/myapp/.venv/lib/python3.9/site-packages/google_auth_httplib2.py", line 218, in request
    response, content = self.http.request(
  File "/apps/myapp/.venv/lib/python3.9/site-packages/httplib2/__init__.py", line 1708, in request
    (response, content) = self._request(
  File "/apps/myapp/.venv/lib/python3.9/site-packages/httplib2/__init__.py", line 1424, in _request
    (response, content) = self._conn_request(conn, request_uri, method, body, headers)
  File "/apps/myapp/.venv/lib/python3.9/site-packages/httplib2/__init__.py", line 1405, in _conn_request
    content = response.read()
  File "/usr/lib/python3.9/http/client.py", line 462, in read
    return self._readall_chunked()
  File "/usr/lib/python3.9/http/client.py", line 569, in _readall_chunked
    chunk_left = self._get_chunk_left()
  File "/usr/lib/python3.9/http/client.py", line 552, in _get_chunk_left
    chunk_left = self._read_next_chunk_size()
  File "/usr/lib/python3.9/http/client.py", line 523, in _read_next_chunk_size
    self._close_conn()
  File "/usr/lib/python3.9/http/client.py", line 409, in _close_conn
    fp.close()
AttributeError: 'NoneType' object has no attribute 'close'
 

Используемый код:

 import socket

from googleapiclient.discovery import Resource, build

.... more imports ....

socket.setdefaulttimeout(300)
service = build("sheets", "v4", credentials=get_credentials(SPREADSHEET_SCOPES, credentials_type="service_account"))

def get_spreadsheets_resource() -> Resource:
    resource = service.spreadsheets()

    if not resource:
        raise RetryError("get_spreadsheets_resource:: service.spreadsheets() result is empty")

    return resource


def append(values: List[List[str]], sheet_name: str, spreadsheet_id=config.GOOGLE_SPREADSHEET_ID):
    return (
        get_spreadsheets_resource()
        .values()
        .append(
            spreadsheetId=spreadsheet_id,
            range=f"{sheet_name}!A1:A2",
            valueInputOption="USER_ENTERED",
            body={"range": f"{sheet_name}!A1:A2", "majorDimension": "ROWS", "values": values},
        )
        .execute()
    )
 

Иногда это ошибка SSL, иногда тайм-аут сокета (он улучшается после того, как я добавил явный тайм-аут сокета с большой задержкой, но все еще существует), иногда проблема с закрытием обработчика. Но все это определенно связано с вызовами клиентов Google api.