由于 API 限制,我的 Python 代码在从 Twitter 检索信息时不断崩溃。每次崩溃时,我如何才能自动再次运行该进程?
Traceback (most recent call last):
File "/usr/local/anaconda3/lib/python3.5/http/client.py", line 578, in _readinto_chunked
n = self._safe_readinto(mvb)
File "/usr/local/anaconda3/lib/python3.5/http/client.py", line 625, in _safe_readinto
raise IncompleteRead(bytes(mvb[0:total_bytes]), len(b))
http.client.IncompleteRead: IncompleteRead(0 bytes read, 2880 more expected)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/anaconda3/lib/python3.5/site-packages/urllib3/response.py", line 331, in _error_catcher
yield
File "/usr/local/anaconda3/lib/python3.5/site-packages/urllib3/response.py", line 413, in read
data = self._fp.read(amt)
File "/usr/local/anaconda3/lib/python3.5/http/client.py", line 448, in read
n = self.readinto(b)
File "/usr/local/anaconda3/lib/python3.5/http/client.py", line 478, in readinto
return self._readinto_chunked(b)
File "/usr/local/anaconda3/lib/python3.5/http/client.py", line 589, in _readinto_chunked
raise IncompleteRead(bytes(b[0:total_bytes]))
http.client.IncompleteRead: IncompleteRead(5530 bytes read)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "mona_counthash_stream.py", line 248, in <module>
streamer.filter(languages=["en"], track=unique_hashtags[:367])
File "/usr/local/anaconda3/lib/python3.5/site-packages/tweepy/streaming.py", line 450, in filter
self._start(async)
File "/usr/local/anaconda3/lib/python3.5/site-packages/tweepy/streaming.py", line 364, in _start
self._run()
File "/usr/local/anaconda3/lib/python3.5/site-packages/tweepy/streaming.py", line 297, in _run
six.reraise(*exc_info)
File "/usr/local/anaconda3/lib/python3.5/site-packages/six.py", line 693, in reraise
raise value
File "/usr/local/anaconda3/lib/python3.5/site-packages/tweepy/streaming.py", line 266, in _run
self._read_loop(resp)
File "/usr/local/anaconda3/lib/python3.5/site-packages/tweepy/streaming.py", line 325, in _read_loop
next_status_obj = buf.read_len(length)
File "/usr/local/anaconda3/lib/python3.5/site-packages/tweepy/streaming.py", line 164, in read_len
self._buffer += self._stream.read(read_len)
File "/usr/local/anaconda3/lib/python3.5/site-packages/urllib3/response.py", line 430, in read
raise IncompleteRead(self._fp_bytes_read, self.length_remaining)
File "/usr/local/anaconda3/lib/python3.5/contextlib.py", line 77, in __exit__
self.gen.throw(type, value, traceback)
File "/usr/local/anaconda3/lib/python3.5/site-packages/urllib3/response.py", line 349, in _error_catcher
raise ProtocolError('Connection broken: %r' % e, e)
urllib3.exceptions.ProtocolError: ('Connection broken: IncompleteRead(5530 bytes read)', IncompleteRead(5530 bytes read))
pass
尽管我在我的代码中使用了以下代码,但还是发生了这种情况:
from urllib3.exceptions import ProtocolError
import http
except tweepy.TweepError as e:
print("Tweepy Error is: ", e.reason)
pass
except http.client.IncompleteRead as e:
print("http exception is: ", e)
pass
except UnicodeEncodeError as e:
print("Unicode error is: ", e)
pass
#except urllib3.exceptions.ProtocolError as e:
except ProtocolError as e:
print("url exception is:", e)
time.sleep(15*60)
pass
except UnicodeDecodeError as e:
pass