1

I have a script that collects data from the streaming API. I'm getting an error at random that I believe it's coming from twitter's end for whatever reason. It doesn't happen at specific time, I've been seen it as early as 10 minutes after running my script, and other times after 2 hours.

My question is how do I create another script (outside the running one) that can catch if it terminated with an error, then restart after a delay.

I did some searching and most were related to using bash on linux, I'm on windows. Other suggestions were to use Windows Task Scheduler but that can only be set for a known time.

I came across the following code:

import os, sys, time

def main():  
    print "AutoRes is starting"
    executable = sys.executable
    args = sys.argv[:]
    args.insert(0, sys.executable)

    time.sleep(1)
    print "Respawning"
    os.execvp(executable, args)

if __name__ == "__main__":  
    main()

If I'm not mistaken that runs inside the code correct? Issue with that is my script is currently collecting data and I can't terminate to edit.

5
  • 2
    Can you detect when your script goes wrong? At that point you can throw an exception. Commented Jun 27, 2015 at 5:15
  • ... and use forever ? stackoverflow.com/questions/19571282/… Commented Jun 27, 2015 at 5:17
  • I do have an exception but it doesn't capture that error, it captures errors once I receive data from the API and works for that. That's why I believe it might be a connection problem between me and twitter. Commented Jun 27, 2015 at 5:17
  • @Leb but in general, can you tell when you script goes wrong? At that point, call sys.exit(1) Commented Jun 27, 2015 at 5:19
  • @JuanPablo I need a delay, otherwise twitter won't appreciate it. Commented Jun 27, 2015 at 5:19

1 Answer 1

2

How about this?

from os import system
from time import sleep

while True: #manually terminate when you want to stop streaming
    system('python streamer.py')
    sleep(300) #sleep for 5 minutes

In the meanwhile, when something goes wrong in streamer.py , end it from there by invoking sys.exit(1)

Make sure this and streamer.py are in the same directory.

Sign up to request clarification or add additional context in comments.

11 Comments

@Leb this is a separate “controller” script.
Depending on what exactly streamer.py does, you could end up with multiple streamer.py instances. Also, you might have to wait 5 minutes before you deal with the errornous streamer.py
@moose how is that possible?
@moose isn’t a call to system blocking?
@RishavKundu I did a dummy run on another script and it worked. I can't use it on my main one because it'll create another connection. Once the current one terminate I'll run it and update.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.