2 years ago

#59820

test-img

Ditto Rahmat

gspread returning error requests.exceptions.ConnectionError: ('Connection aborted.', TimeoutError('The write operation timed out'))

I found out about gspread and it is just what I need for my code. I tried testing it on 100+ rows and it was inserting fine, but after I testing it on 100K+ rows, it returning the following error.

Traceback (most recent call last):
  File "C:\Python310\lib\site-packages\urllib3\connectionpool.py", line 703, in urlopen
    httplib_response = self._make_request(
  File "C:\Python310\lib\site-packages\urllib3\connectionpool.py", line 398, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "C:\Python310\lib\site-packages\urllib3\connection.py", line 239, in request
    super(HTTPConnection, self).request(method, url, body=body, headers=headers)
  File "C:\Python310\lib\http\client.py", line 1282, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "C:\Python310\lib\http\client.py", line 1328, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "C:\Python310\lib\http\client.py", line 1277, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "C:\Python310\lib\http\client.py", line 1076, in _send_output
    self.send(chunk)
  File "C:\Python310\lib\http\client.py", line 998, in send
    self.sock.sendall(data)
  File "C:\Python310\lib\ssl.py", line 1236, in sendall
    v = self.send(byte_view[count:])
  File "C:\Python310\lib\ssl.py", line 1205, in send
    return self._sslobj.write(data)
TimeoutError: The write operation timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\lenovo\AppData\Roaming\Python\Python310\site-packages\requests\adapters.py", line 440, in send
    resp = conn.urlopen(
  File "C:\Python310\lib\site-packages\urllib3\connectionpool.py", line 785, in urlopen
    retries = retries.increment(
  File "C:\Python310\lib\site-packages\urllib3\util\retry.py", line 550, in increment
    raise six.reraise(type(error), error, _stacktrace)
  File "C:\Python310\lib\site-packages\urllib3\packages\six.py", line 769, in reraise
    raise value.with_traceback(tb)
  File "C:\Python310\lib\site-packages\urllib3\connectionpool.py", line 703, in urlopen
    httplib_response = self._make_request(
  File "C:\Python310\lib\site-packages\urllib3\connectionpool.py", line 398, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "C:\Python310\lib\site-packages\urllib3\connection.py", line 239, in request
    super(HTTPConnection, self).request(method, url, body=body, headers=headers)
  File "C:\Python310\lib\http\client.py", line 1282, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "C:\Python310\lib\http\client.py", line 1328, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "C:\Python310\lib\http\client.py", line 1277, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "C:\Python310\lib\http\client.py", line 1076, in _send_output
    self.send(chunk)
  File "C:\Python310\lib\http\client.py", line 998, in send
    self.sock.sendall(data)
  File "C:\Python310\lib\ssl.py", line 1236, in sendall
    v = self.send(byte_view[count:])
  File "C:\Python310\lib\ssl.py", line 1205, in send
    return self._sslobj.write(data)
urllib3.exceptions.ProtocolError: ('Connection aborted.', TimeoutError('The write operation timed out'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\upwork\realtorcom\db.py", line 124, in <module>
    worksheet.update([df.columns.values.tolist()] + df.values.tolist())
  File "C:\Users\lenovo\AppData\Roaming\Python\Python310\site-packages\gspread\utils.py", line 600, in wrapper
    return f(*args, **kwargs)
  File "C:\Users\lenovo\AppData\Roaming\Python\Python310\site-packages\gspread\worksheet.py", line 746, in update
    response = self.spreadsheet.values_update(
  File "C:\Users\lenovo\AppData\Roaming\Python\Python310\site-packages\gspread\spreadsheet.py", line 215, in values_update
    r = self.client.request("put", url, params=params, json=body)
  File "C:\Users\lenovo\AppData\Roaming\Python\Python310\site-packages\gspread\client.py", line 59, in request
    response = getattr(self.session, method)(
  File "C:\Users\lenovo\AppData\Roaming\Python\Python310\site-packages\requests\sessions.py", line 589, in put
    return self.request('PUT', url, data=data, **kwargs)
  File "C:\Users\lenovo\AppData\Roaming\Python\Python310\site-packages\google\auth\transport\requests.py", line 480, in request
    response = super(AuthorizedSession, self).request(
  File "C:\Users\lenovo\AppData\Roaming\Python\Python310\site-packages\requests\sessions.py", line 529, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Users\lenovo\AppData\Roaming\Python\Python310\site-packages\requests\sessions.py", line 645, in send
    r = adapter.send(request, **kwargs)
  File "C:\Users\lenovo\AppData\Roaming\Python\Python310\site-packages\requests\adapters.py", line 501, in send
    raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', TimeoutError('The write operation timed out'))

Here's a snippet of my code

df = pd.read_sql_table('Table',database_connection)
df = df.fillna('')
gc = gspread.service_account(filename='client_secret.json')
sh = gc.open("Data")
worksheet = sh.sheet1
worksheet.update([df.columns.values.tolist()] + df.values.tolist())

Question Do I need to set some sort of timeout in gspread ? How do I set it ? I already checked the docs and couldn't find anything

https://docs.gspread.org/en/latest/search.html?q=timeout&check_keywords=yes&area=default#

python

google-sheets-api

gspread

0 Answers

Your Answer

Accepted video resources