4

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か?

4

1 に答える 1