Flask-SocketIO Redis订阅 [英] Flask-SocketIO redis subscribe
本文介绍了Flask-SocketIO Redis订阅的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 https://github.com/miguelgrinberg/Flask-SocketIO 实现WebSocket服务器.
I'm using https://github.com/miguelgrinberg/Flask-SocketIO to implement a WebSocket server.
我需要接收来自其他进程的消息(仅订阅),并向特定房间中的客户端发送消息.
I need to receive a message from another process (only subscribe) and emit for clients in a specific room.
但是,当我尝试发送消息时,出现此错误:
But, when i try to send the message, i got this error:
无法将消息发送到家庭会议室:在请求上下文之外工作.
Could not send message to home room: Working outside of request context.
这是我的代码:
from flask import Flask, request
from flask_socketio import SocketIO, join_room, leave_room, send, rooms
import json
import eventlet
import logging
import redis
import threading
FORMAT = '%(asctime)-15s - %(message)s'
logging.basicConfig(format=FORMAT)
log = logging.getLogger(__name__)
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app, async_mode='eventlet')
.
.
.
def _send_task_message():
try:
send(json.dumps({"type":"UPDATE_TASK"}), room='home')
except Exception as e:
log.error('Could not send message to home room: %s' % str(e))
class Listener(threading.Thread):
def __init__(self, r, channels):
threading.Thread.__init__(self)
self.daemon = True
self.redis = r
self.pubsub = self.redis.pubsub()
self.pubsub.psubscribe(channels)
def work(self, item):
if isinstance(item['data'], bytes):
try:
msg = item['data'].decode('utf-8')
decode_msg = json.loads(msg)
if decode_msg['type'] == 'UPDATE_TASK':
_send_task_message()
except ValueError as e:
log.error("Error decoding msg to microservice: %s", str(e))
def run(self):
for item in self.pubsub.listen():
self.work(item)
if __name__ == '__main__':
r = redis.Redis()
client = Listener(r, ['/bobguarana/socketio'])
client.start()
socketio.run(debug=True, app=app, port=8080)
推荐答案
我解决了将应用程序作为参数传递给类的问题,并按照错误说明的建议使用了它的上下文,但是名称空间也是必需的:
I Solved passing the app as an argument to the class and using it context as suggested by the error description, but the namespace is necessary too:
class Listener(threading.Thread):
def __init__(self, r, channels, app):
threading.Thread.__init__(self)
self.daemon = True
self.redis = r
self.pubsub = self.redis.pubsub()
self.pubsub.psubscribe(channels)
self.app = app
def work(self, item):
with app.app_context():
if isinstance(item['data'], bytes):
try:
msg = item['data'].decode('utf-8')
decode_msg = json.loads(msg)
if decode_msg['type'] == 'UPDATE_TASK':
send(json.dumps({"type":"UPDATE_TASK"}), room='home', namespace='/')
#_send_task_message()
except ValueError as e:
log.error("Error decoding msg to microservice: %s", str(e))
def run(self):
for item in self.pubsub.listen():
self.work(item)
if __name__ == '__main__':
r = redis.Redis()
client = Listener(r, ['/bobguarana/socketio'], app)
client.start()
socketio.run(debug=True, app=app, port=8080)
这篇关于Flask-SocketIO Redis订阅的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文