我有以下情况:
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)