OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

My threads are not running when running a flask app

  • Thread starter Thread starter Werner van der Merwe
  • Start date Start date
W

Werner van der Merwe

Guest
Whenever I am trying to run a thread in my app.py containing my flask app, the thread does not run.

Code:
from flask import Flask, render_template
from threading import Thread

app = Flask(__name__)
run = True


@app.route('/')
def home():
    return render_template('index.html')


def output():
    while run:
        print('run')


if __name__ == '__main__':
    output_thread = Thread(target=output())
    output_thread.start()
    app.run()

The thread output_thread should display 'run' in the console, but nothing is happening.

I have made multiple attempts to rewrite the code and tried various configurations, of running the thread, but none seem to work, I have also tried letting the thread do something else like write in pyautogui, thinking that the problem might have been with the console, without any luck.
<p>Whenever I am trying to run a thread in my app.py containing my flask app, the thread does not run.</p>
<pre><code>from flask import Flask, render_template
from threading import Thread

app = Flask(__name__)
run = True


@app.route('/')
def home():
return render_template('index.html')


def output():
while run:
print('run')


if __name__ == '__main__':
output_thread = Thread(target=output())
output_thread.start()
app.run()

</code></pre>
<p>The thread output_thread should display 'run' in the console, but nothing is happening.</p>
<p>I have made multiple attempts to rewrite the code and tried various configurations, of running the thread, but none seem to work, I have also tried letting the thread do something else like write in pyautogui, thinking that the problem might have been with the console, without any luck.</p>
 
Top