Alessandro
Alessandro

Reputation: 53

Start infinite python script in new thread within Flask

I'd like to start my never ending Python script from within Flask request:

def start_process():
    exec(open("./process/main.py").read(), globals())
    print("Started")
    return

and with the request:

@app.route("/start")
   def start():
   from threading import Thread
   thread = Thread(target=start_process, args=())
   thread.setDaemon(True)
   thread.start()
   return redirect(url_for('main'))

The main.py process is a little test server that waits for some messages, but it simply hangs the entire flask script (in fact, through gunicorn, if I send CTRL-C, I can see the output of the subprocess).

How can I make the main.py script start separately?

Upvotes: 1

Views: 884

Answers (1)

Dave W. Smith
Dave W. Smith

Reputation: 24966

I've not successfully started long-running threads from within Flask, but have gone the other way: starting Flask in a thread and providing it a way to communicate with other threads.

The trick is to something along the lines of

def webserver(coordinator):
    app.config['COORDINATOR'] = coordinator
    app.run(use_reloader=False)
    # use_reloader=False is needed to keep Flask happy in a thread

def main():
    coordinator = Coordinator()
    ui = threading.Thread(target=webserver, args=(coordinator,))
    ui.start()
    # start other threads, passing them coordinator
    # start long-running tasks in main thread, passing it coordinator

Where Coordinator uses threading facilities (e.g., Lock, Queue) to protect access to shared data or resources. You can fairly easily set up the coordinator to support have worker threads block until signaled (from a handler), then start long-running tasks.

Upvotes: 1

Related Questions