I'm playing around with getting zeromq
pub/sub messages sent up to a browser with a websocket. The following "works", in that messages do get sent up through a websocket. However, trying to reload the page just hangs, as I'm guessing the while True
loop is blocking. I thought that the gevent.sleep()
call would allow a context switch, but apparently not. Any idea how to get all these parts working together?
import zmq
import json
import gevent
from flask_sockets import Sockets
from flask import Flask, render_template
import logging
from gevent import monkey
monkey.patch_all()
app = Flask(__name__)
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
sockets = Sockets(app)
context = zmq.Context()
ZMQ_LISTENING_PORT = 6557
@app.route('/')
def index():
return render_template('index.html')
@sockets.route('/zeromq')
def send_data(ws):
logger.info('Got a websocket connection, sending up data from zmq')
socket = context.socket(zmq.SUB)
socket.connect('tcp://localhost:{PORT}'.format(PORT=ZMQ_LISTENING_PORT))
socket.setsockopt(zmq.SUBSCRIBE, "")
gevent.sleep()
while True:
data = socket.recv_json()
logger.info(data)
ws.send(json.dumps(data))
gevent.sleep()
if __name__ == '__main__':
from gevent import pywsgi
from geventwebsocket.handler import WebSocketHandler
server = pywsgi.WSGIServer(('', 25000), app, handler_class=WebSocketHandler)
server.serve_forever()
Nevermind, just realized need to do
import zmq.green as zmq
in order to use with gevent compatibility. Here's a link to pyZeormq docs.