Portafreak
Portafreak

Reputation: 43

How to run Flask SocketIO in a thread?

I'm trying to run Flask SocketIO in thread using Python 3 and I cannot get it to work.

My code will not continue in my while loop.

How can I run it in thread?

import threading
from flask import Flask, render_template, request, redirect, url_for
from flask_socketio import SocketIO

app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'


#turn the flask app into a socketio app
socketio = SocketIO(app)


@app.route("/")
def index():
   return render_template('index.html', **templateData)

if __name__ == "__main__":
   threading.Thread(target=socketio.run(app),kwargs=dict(debug=False, use_reloader=False,host='0.0.0.0', port=5000)).start()
   sleep(2)

   while True:
       try:
           Print("Hello I'm in a while loop")

       except KeyboardInterrupt:
           sys.exit()

Upvotes: 1

Views: 209

Answers (1)

nick
nick

Reputation: 463

you should pass socketio.run as target, and app as argument

threading.Thread(
    target=socketio.run,
    args=(app,),
    kwargs=dict(debug=False, use_reloader=False,host='0.0.0.0', port=5000)
).start()

also seems like you forget to import sleep from time

from time import sleep

and also please notice that templateData is not defined in code

Upvotes: 1

Related Questions