如果发生错误,请重新运行Python脚本

时间:2017-10-10 22:21:39

标签: python

我正在使用他们的Streaming API抓取Twitter数据,有时候,我会得到

  File "//swigel_local/Shared/Resources/Tools/Twitter/Streaming/StreamingTwitter.py", line 68, in <module>
    stream.filter(track=TRACK_TERMS)

  File "C:\Users\SwigelUser\Anaconda3\lib\site-packages\tweepy\streaming.py", line 445, in filter
    self._start(async)

  File "C:\Users\SwigelUser\Anaconda3\lib\site-packages\tweepy\streaming.py", line 361, in _start
    self._run()

  File "C:\Users\SwigelUser\Anaconda3\lib\site-packages\tweepy\streaming.py", line 294, in _run
    raise exception

  File "C:\Users\SwigelUser\Anaconda3\lib\site-packages\tweepy\streaming.py", line 263, in _run
    self._read_loop(resp)

  File "C:\Users\SwigelUser\Anaconda3\lib\site-packages\tweepy\streaming.py", line 313, in _read_loop
    line = buf.read_line().strip()

AttributeError: 'NoneType' object has no attribute 'strip'

这将停止我的计划。有没有办法在发生此错误时自动重新运行程序?

我的代码是:

class StreamListener(tweepy.StreamListener):

    def on_status(self, status):
        if status.retweeted:
            return

        text = status.text
        created = status.created_at
        id_str = status.id_str
        retweets = status.retweet_count
        blob = TextBlob(text)
        sent = blob.sentiment

        table = db[settings.TABLE_NAME]
        try:
            table.insert(dict(
                text = text,
                created = created,
                id_str = id_str,
                retweet_count = retweets,
                polarity = sent.polarity,
                subjectivity = sent.subjectivity,
            ))
        except ProgrammingError as err:
            print(err)


    def on_error(self, status_code):
        if status_code == 420:
            #returning False in on_data disconnects the stream
            return False
stream.filter(track=TRACK_TERMS)

我不知道错误发生的原因。但我认为这与我最近做出的改变有关。

之前,我在MySQL中的表有utf8排序规则,但这会引起警告,然后我阅读文章,并将我的表格改为utf8mb4 unicode。 警告消失了,程序运行正常。但有时,程序运行时会出现上述错误。

def _read_loop(self, resp):
    charset = resp.headers.get('content-type', default='')
    enc_search = re.search('charset=(?P<enc>\S*)', charset)
    if enc_search is not None:
        encoding = enc_search.group('enc')
    else:
        encoding = 'utf-8'

    buf = ReadBuffer(resp.raw, self.chunk_size, encoding=encoding)

    while self.running and not resp.raw.closed:
        length = 0
        while not resp.raw.closed:
            line = buf.read_line().strip()
            if not line:
                self.listener.keep_alive()  # keep-alive new lines are expected
            elif line.isdigit():
                length = int(line)
                break
            else:
                raise TweepError('Expecting length, unexpected value found')

1 个答案:

答案 0 :(得分:0)

  

有没有办法在发生此错误时自动重新运行程序?

您可以编写一个监视系统的外部应用程序,检测应用程序的崩溃并重新启动它。

但是我不推荐它!

正确的解决方案是:

  • 找出问题的原因并加以解决!
  • 使用try .. except包装有问题的代码段,捕获此类错误(例如AttributeError)并相应地处理它们