我想使用 gevent-socketio 从工作线程发送消息并更新所有连接的客户端的作业状态。
我试过这个:
from flask import Flask, render_template
from flask.ext.socketio import SocketIO, send, emit
import threading
import time
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app)
@socketio.on('message')
def handle_message(message):
send(message, broadcast=True)
@app.route('/')
def index():
return render_template('index.html')
def ping_thread():
while True:
time.sleep(1)
print 'Pinging'
send('ping')
if __name__ == '__main__':
t = threading.Thread(target=ping_thread)
t.daemon = True
t.start()
socketio.run(app)
它给了我这个错误:
RuntimeError: working outside of request context
如何从没有@socketio.on()
装饰器的函数发送消息?我可以gevent
直接使用发送消息socketio
吗?