Reputation: 81
Given the following example, well it's a quite simple example according to the documentation. I've got an error using redis or any other provider as a message_queue. Browser just hangs, although, if I just set message_queue to None (uncomment the line). Everything works as expected. Also, I do have kombu, redis and eventlet package pip installed on the enviroment. Using flask-socketio==4.2.1
from flask import Flask, jsonify
from flask_socketio import SocketIO, emit
message_queue = "redis://127.0.0.1:6379"
# message_queue = None
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app,
async_mode='eventlet',
message_queue=message_queue,
manage_session=False,
logger=True,
engineio_logger=True)
@app.route('/')
def home():
return """
<html>
<body>
<script src="//cdnjs.cloudflare.com/ajax/libs/socket.io/2.2.0/socket.io.js" integrity="sha256-yr4fRk/GU1ehYJPAs8P4JlTgu0Hdsp4ZKrx8bDEDC3I=" crossorigin="anonymous"></script>
<script type="text/javascript" charset="utf-8">
var socket = io();
socket.on('connect', function() {
socket.emit('my event', {data: 'Im connected!'});
});
socket.on('my response', function(data) {
console.log(data);
});
</script>
ok
</body>
</html>
"""
@socketio.on('my event')
def test_message(message):
emit('my response', {'data': 'got it!'})
That's what is logged:
Server initialized for eventlet.
* Restarting with fsevents reloader
Server initialized for eventlet.
* Debugger is active!
* Debugger PIN: 609-071-851
(70237) wsgi starting up on http://127.0.0.1:5000
(70237) accepted ('127.0.0.1', 53303)
39982ab1b7f84dee8e8d34dee1141cec: Sending packet OPEN data {'sid': '39982ab1b7f84dee8e8d34dee1141cec', 'upgrades': ['websocket'], 'pingTimeout': 60000, 'pingInterval': 25000}
using rabbit-mq i've got thouse logs from rabbit:
rabbit_1 | 2020-02-18 20:47:16.535 [info] <0.18859.0> accepting AMQP connection <0.18859.0> (172.31.0.1:46412 -> 172.31.0.6:5672)
rabbit_1 | 2020-02-18 20:47:16.581 [info] <0.18859.0> connection <0.18859.0> (172.31.0.1:46412 -> 172.31.0.6:5672): user 'rabbitmq' authenticated and granted access to vhost '/'
=== updated
Patching eventlet in the beguining of the file, didn't changed the behavior.
import eventlet
eventlet.monkey_patch()
The entire example script is stated. If you manage to make it run properly, please make a comment. Thanks.
Upvotes: 0
Views: 1617
Reputation: 67479
My guess is that this is due to now having the standard library monkey patched to be compatible with eventlet. Add the following two lines at the two of your script:
import eventlet
eventlet.monkey_patch()
from flask import Flask, jsonify
from flask_socketio import SocketIO, emit
message_queue = "redis://127.0.0.1:6379"
# message_queue = None
# ...
For more information on monkey patching, see https://eventlet.net/doc/patching.html.
I've also needed to add the run()
call at the bottom, you omitted that in your listing:
socketio.run(app)
With those two changes, I can run the Python script and nothing hangs for me. I suspect you may have been using flask run
to run the server, but that is not the way to start the eventlet server, that only works for the Flask dev server. You have to use socketio.run(app)
to start the eventlet server properly.
Upvotes: 2