-1

Currently i'm trying to use proper threading to execute a bunch of scripts.

They are sorted like that: Main Thread (Runs the Flask app) -Analysis Thread (Runs the analysis script which invokes all needed scripts) -3 different functions executed as thread (Divided in 3 parts so the analysis runs quicker)

My problem is i have a global variable with the analysis thread to be able to determine after the call wether the thread is running or not. The first time it does start and running just fine. Then you can call that endpoint as often as you like it wont do anything because i return a 423 to state that the thread (the analysis) is still running. After all scripts are finished, the if clause with analysis_thread.isAlive() returns false as it should and tries to start the analysis again with analysis_thread.start() but that doesn't work, it throws an exception saying the thread is already active and can't be started twice.

Is there a way to achieve that the script can be started and while it is running it returns another code but when it is finished i can start it again ?

Thanks for reading and for all your help

Christoph

  • if the thread is finished you can re-run the thread object, create a new thread and run it. analyisis_thread = Thread() .. analyisis_thread.start() – darc Aug 15 '18 at 10:58
  • ahh oke well then my problem is that i made the Thread object global and the object is instantiated only once ? – Christoph Pelzer Aug 15 '18 at 11:03

1 Answers1

0

The now hopefully working solution is to never stop the thread and just let it wait.

in the analysis script i have a global variable which indicates the status it is set to False by default.

inside the function it runs two whiles:

while True:
    while not thread_status:
        time.sleep(30)
    execution of the other scripts.
    thread_status = False # to ensure the execution runs just once.

I then just set the flag to True from the Controller class so it starts executing