0

I've been spending some time in searching for the correct code but I got no luck.

I have below python script with background thread and a main prog.. I want to restart the script once the background thread encounters a condition. Hope you can help me with this. TIA

With this script, both background_stuff and "main" are running at the same time.

def background_stuff():
    while True:
        if condition == 'True':
            ### restart the script
t = Thread(target=background_stuff)
t.start()
### main
if __name__ == '__main__':
    while True:
          #do something
iGreenius
  • 33
  • 1
  • 5
  • Do you need to restart the entire script or can you just run the main-method? – Metareven Aug 05 '15 at 12:57
  • 1
    I think we need more information to help you here. Is the main thread still running when you need to restart? If so, you should probably run the main program in its own thread and then read http://stackoverflow.com/questions/323972/is-there-any-way-to-kill-a-thread-in-python for how to terminate it and re-run the main method in another thread once the condition is reached. – Metareven Aug 05 '15 at 13:12
  • @Metareven, I need to restart the whole script. It doesn't matter what the main script is currently doing. I tried that "run the main-method" and the background_stuff job stopped since the main() is also a non-terminating loop. Please see edited script. Thanks for looking into this. – iGreenius Aug 06 '15 at 03:02

1 Answers1

2

Can you restructure your script like so?

def main():
    t = Thread(target=background_stuff)
    t.start()
    while True:
        #do something

def background_stuff():
    while True:
        if condition == 'True':
            ### restart the script
#t = Thread(target=background_stuff)
#t.start()
### main
if __name__ == '__main__':
    main()

Calling main now should create a new thread that will run the background stuff and the script basically restarts. The only issue is that state is not reset. If you need to reset state then you could actually run your script from scratch from within your script using:

os.system("yourScript.py -yourargs")

I don't think this is best practice though...

Regardless of which solution you end up using, you still need to terminate the old main loop. This could either be done by setting some flag when you "restart your script" and then have the main loop periodically check for that flag and do a controlled shut down once the flag is set, or you can run the main loop in another thread which you can access globally or from the other thread and then terminate it. Just remember that forcibly shutting down a thread is often not adviced.

Metareven
  • 822
  • 2
  • 7
  • 26
  • Thanks for the idea Metareven. I actually got what I want with this - "This could either be done by setting some flag when you "restart your script" and then have the main loop periodically check for that flag and do a controlled shut down once the flag is set" – iGreenius Aug 06 '15 at 12:32