1

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?

1 Answer 1

1

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.

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

1 Comment

It does the reverse of what I want to do, but it's always a sort of solution.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.