运行烧瓶路径时无法收听socket.on

时间:2020-04-01 00:09:54

标签: multithreading flask socket.io routes request

我有以下情况:

  1. 首先运行并停留在while循环中的路由,该循环等待从客户端通过套接字发送的数据。

2。我有一个@ socket.on()来侦听从客户端发送的数据。

问题在于,首先调用路由时,它会阻止@ socket.on接收数据,这可能是因为主线程被阻止(?)..

while循环中运行的路由如下:

@app.route('/api/get-data', methods=['POST'])
def start_fab_loop():
   while(received_data is None){
       // keep looping
   }
// here I have more code.. I don't want to end the request until I receive the data from the socket emit

这部分侦听客户端发送的数据:

@socketio.on('join')
def on_join():
    send("data is send from here to the route!")

我试图将while循环放在单独的线程中,并使请求线程像这样加入它:

thread1 = threading.Thread(target=while_loop_inside_route)
thread1.start()
threading.Thread.join(thread1)

但是,它也不起作用。 *我正在使用flask-socketio并像这样运行应用程序:

app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
cors = CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'
app.config['transports'] = 'websocket'
socketio = SocketIO(app, cors_allowed_origins="*")
socketio.run(app)

0 个答案:

没有答案