Hi, I’m using the v2 streaming API like this:
request = requests.get(’?expansions=author_id&tweet.fields=context_annotations,created_at,entities,id,text&user.fields=id,name,username,public_metrics’, headers=headers, stream=True)
and then
for data in request.iter_lines():
Now, it sometimes runs alright for a while, but like today, it’s been breaking after every couple dozen of tweets with a ECONNRESET, which I think triggers the ChunkedEncodingError.
The main problem is that it seems to take Twitter a long time to admin the failed connection, and I keep getting ‘TooManyConnections’ responses when I try to reconnect I guess untill the rate limit window resets.
The output of my program then looks something like below.
Am I doing something wrong?
Start reading
.....................................................................Exception in thread Thread-352:
Traceback (most recent call last):
File "<MYDIR>/lib/python3.8/site-packages/urllib3/contrib/pyopenssl.py", line 313, in recv_into
return self.connection.recv_into(*args, **kwargs)
File "<MYDIR>/lib/python3.8/site-packages/OpenSSL/SSL.py", line 1734, in recv_into
self._raise_ssl_error(self._ssl, result)
File "<MYDIR>/lib/python3.8/site-packages/OpenSSL/SSL.py", line 1558, in _raise_ssl_error
raise SysCallError(errno, errorcode.get(errno))
OpenSSL.SSL.SysCallError: (104, 'ECONNRESET')
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<MYDIR>/lib/python3.8/site-packages/urllib3/response.py", line 425, in _error_catcher
yield
File "<MYDIR>/lib/python3.8/site-packages/urllib3/response.py", line 752, in read_chunked
self._update_chunk_length()
File "<MYDIR>/lib/python3.8/site-packages/urllib3/response.py", line 682, in _update_chunk_length
line = self._fp.fp.readline()
File "/usr/lib/python3.8/socket.py", line 669, in readinto
return self._sock.recv_into(b)
File "<MYDIR>/lib/python3.8/site-packages/urllib3/contrib/pyopenssl.py", line 318, in recv_into
raise SocketError(str(e))
OSError: (104, 'ECONNRESET')
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<MYDIR>/lib/python3.8/site-packages/requests/models.py", line 750, in generate
for chunk in self.raw.stream(chunk_size, decode_content=True):
File "<MYDIR>/lib/python3.8/site-packages/urllib3/response.py", line 560, in stream
for line in self.read_chunked(amt, decode_content=decode_content):
File "<MYDIR>/lib/python3.8/site-packages/urllib3/response.py", line 781, in read_chunked
self._original_response.close()
File "/usr/lib/python3.8/contextlib.py", line 131, in __exit__
self.gen.throw(type, value, traceback)
File "<MYDIR>/lib/python3.8/site-packages/urllib3/response.py", line 443, in _error_catcher
raise ProtocolError("Connection broken: %r" % e, e)
urllib3.exceptions.ProtocolError: ('Connection broken: OSError("(104, \'ECONNRESET\')")', OSError("(104, 'ECONNRESET')"))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/lib/python3.8/threading.py", line 932, in _bootstrap_inner
self.run()
File "/usr/lib/python3.8/threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "<MYDIR>/project/api/management/commands/run_stream.py", line 129, in handle_twitter_stream
for data in request.iter_lines():
File "<MYDIR>/lib/python3.8/site-packages/requests/models.py", line 794, in iter_lines
for chunk in self.iter_content(chunk_size=chunk_size, decode_unicode=decode_unicode):
File "<MYDIR>/lib/python3.8/site-packages/requests/models.py", line 753, in generate
raise ChunkedEncodingError(e)
requests.exceptions.ChunkedEncodingError: ('Connection broken: OSError("(104, \'ECONNRESET\')")', OSError("(104, 'ECONNRESET')"))
Late heartbeat. Resetting stream.
Sleeping for 30 seconds before trying again
New stream
Start reading
..................................................................................................................................................................Exception in thread Thread-423:
Traceback (most recent call last):
File "<MYDIR>/lib/python3.8/site-packages/urllib3/contrib/pyopenssl.py", line 313, in recv_into
return self.connection.recv_into(*args, **kwargs)
File "<MYDIR>/lib/python3.8/site-packages/OpenSSL/SSL.py", line 1734, in recv_into
self._raise_ssl_error(self._ssl, result)
File "<MYDIR>/lib/python3.8/site-packages/OpenSSL/SSL.py", line 1558, in _raise_ssl_error
raise SysCallError(errno, errorcode.get(errno))
OpenSSL.SSL.SysCallError: (104, 'ECONNRESET')
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<MYDIR>/lib/python3.8/site-packages/urllib3/response.py", line 425, in _error_catcher
yield
File "<MYDIR>/lib/python3.8/site-packages/urllib3/response.py", line 752, in read_chunked
self._update_chunk_length()
File "<MYDIR>/lib/python3.8/site-packages/urllib3/response.py", line 682, in _update_chunk_length
line = self._fp.fp.readline()
File "/usr/lib/python3.8/socket.py", line 669, in readinto
return self._sock.recv_into(b)
File "<MYDIR>/lib/python3.8/site-packages/urllib3/contrib/pyopenssl.py", line 318, in recv_into
raise SocketError(str(e))
OSError: (104, 'ECONNRESET')
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<MYDIR>/lib/python3.8/site-packages/requests/models.py", line 750, in generate
for chunk in self.raw.stream(chunk_size, decode_content=True):
File "<MYDIR>/lib/python3.8/site-packages/urllib3/response.py", line 560, in stream
for line in self.read_chunked(amt, decode_content=decode_content):
File "<MYDIR>/lib/python3.8/site-packages/urllib3/response.py", line 781, in read_chunked
self._original_response.close()
File "/usr/lib/python3.8/contextlib.py", line 131, in __exit__
self.gen.throw(type, value, traceback)
File "<MYDIR>/lib/python3.8/site-packages/urllib3/response.py", line 443, in _error_catcher
raise ProtocolError("Connection broken: %r" % e, e)
urllib3.exceptions.ProtocolError: ('Connection broken: OSError("(104, \'ECONNRESET\')")', OSError("(104, 'ECONNRESET')"))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/lib/python3.8/threading.py", line 932, in _bootstrap_inner
self.run()
File "/usr/lib/python3.8/threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "<MYDIR>/project/api/management/commands/run_stream.py", line 129, in handle_twitter_stream
for data in request.iter_lines():
File "<MYDIR>/lib/python3.8/site-packages/requests/models.py", line 794, in iter_lines
for chunk in self.iter_content(chunk_size=chunk_size, decode_unicode=decode_unicode):
File "<MYDIR>/lib/python3.8/site-packages/requests/models.py", line 753, in generate
raise ChunkedEncodingError(e)
requests.exceptions.ChunkedEncodingError: ('Connection broken: OSError("(104, \'ECONNRESET\')")', OSError("(104, 'ECONNRESET')"))
Late heartbeat. Resetting stream.
Sleeping for 30 seconds before trying again
New stream
Start reading
{'title': 'ConnectionException', 'detail': 'This stream is currently at the maximum allowed connection limit.', 'connection_issue': 'TooManyConnections', 'type': 'https://api.twitter.com/2/problems/streaming-connection'}
Sleeping for 30 seconds before trying again
New stream
Start reading
{'title': 'ConnectionException', 'detail': 'This stream is currently at the maximum allowed connection limit.', 'connection_issue': 'TooManyConnections', 'type': 'https://api.twitter.com/2/problems/streaming-connection'}
Sleeping for 30 seconds before trying again
New stream
Start reading
{'title': 'ConnectionException', 'detail': 'This stream is currently at the maximum allowed connection limit.', 'connection_issue': 'TooManyConnections', 'type': 'https://api.twitter.com/2/problems/streaming-connection'}
Sleeping for 30 seconds before trying again
New stream
Start reading