How to restart tweepy script in case of error?

后端 未结 5 489
甜味超标
甜味超标 2020-12-13 16:37

I have a python script that continuously stores tweets related to tracked keywords to a file. However, the script tends to crash repeatedly due to an error appended below.

5条回答
  •  一整个雨季
    2020-12-13 17:08

    Figured out how to incorporate the while/try loop by writing a new function for the stream:

    def start_stream():
        while True:
            try:
                sapi = tweepy.streaming.Stream(auth, CustomStreamListener(api))
                sapi.filter(track=["Samsung", "s4", "s5", "note" "3", "HTC", "Sony", "Xperia", "Blackberry", "q5", "q10", "z10", "Nokia", "Lumia", "Nexus", "LG", "Huawei", "Motorola"])
            except: 
                continue
    
    start_stream()
    

    I tested the auto restart by manually interrupting the program with CMD + C. Nonetheless, happy to hear of better ways to test such functionality.

提交回复
热议问题