programmerskillz
programmerskillz

Reputation: 178

How to thread a flask app and function with a while loop to run simultaneously?

I have a flask app which I want to use with a while loop running in the background. My first thought was to use threading to run them at the same time. The problem I stumbled upon is that only the first thread works (whether the flask app or the function with while loop comes first). My code is:

from multiprocessing import Process

if __name__ == '__main__':
    Process(target = app.run(host='0.0.0.0', port=8080)).start()
    Process(target = statupdate()).start()

Note: I also used the library "thread" and making the flask app its own function.

Is there some way to fix this error or run the two simultaneously?

Upvotes: 1

Views: 2199

Answers (1)

Bharel
Bharel

Reputation: 26900

You have a couple of issues:

  • You are not threading in this case, you are multi-processing. It's something completely different but will still achieve what you seek.
  • You are running the functions instead of giving them as a target.

In order to make this work, don't run the function:

Process(target=app.run, kwargs=dict(host='0.0.0.0', port=8080)).start()
Process(target=statupdate).start()

Upvotes: 2

Related Questions