Python-socketio: Python-SocketIO server won't emit from the main section of the program

Created on 21 Sep 2016  路  2Comments  路  Source: miguelgrinberg/python-socketio

I have a pyhton-socketio server that servers on port 8000 and handles connections perfectly. I want to emit the following emit message every 2 seconds. When emitting the client doesn't receive a message. If I emit from where I point out in the comment it works perfect.

How can I edit this to be able to emit from inside the while loop successfully?

The python server code:

import socketio
import eventlet
import threading
import time
from flask import Flask, render_template

sio = socketio.Server()
app = Flask(__name__)


@app.route('/')
def index():
    return render_template('index.html')


# as a decorator:
@sio.on('connect')
def connect_handler(sid, environ):
    print('IP->' + environ['REMOTE_ADDR'])
    # If I emit here it works e.g. sio.emit('status-update', {'core0_in': 8, 'core1_in': 12,'cpu_usage_in': 5, 'users': 7})


@sio.on('disconnect')
def disconnect(sid):
    print('disconnect ', sid)


class Server(threading.Thread):
    def __init__(self, thread_id):
        threading.Thread.__init__(self)
        self.threadID = thread_id

    def run(self):
        print("Starting " + self.name)
        serve()


def serve():
    if __name__ == '__main__':
        global app
        # wrap Flask application with socketio's middleware
        app = socketio.Middleware(sio, app)

        # deploy as an eventlet WSGI server
        eventlet.wsgi.server(eventlet.listen(('', 8000)), app)


server_thread = Server("Server-thread")
server_thread.start()
while True:
    print("Emitting...")
    sio.emit('status-update', {'core0_in': 8, 'core1_in': 12,'cpu_usage_in': 5, 'users': 7})  # when emitting here the client doesn't receive anything
    time.sleep(2)
question

Most helpful comment

I'm not 100% sure, but it is quite possible that the problem is that you are mixing eventlet with regular Python threads. This isn't a good idea, the green threads that eventlet uses do not work well with regular threads. Instead, you should use green threads for all your threading needs.

One option is to monkey patch the Python standard library, so that threading, sockets, etc. are replaced with eventlet friendly versions. You can do this at the very top of your script:

import eventlet
eventlet.monkey_patch()

All 2 comments

I'm not 100% sure, but it is quite possible that the problem is that you are mixing eventlet with regular Python threads. This isn't a good idea, the green threads that eventlet uses do not work well with regular threads. Instead, you should use green threads for all your threading needs.

One option is to monkey patch the Python standard library, so that threading, sockets, etc. are replaced with eventlet friendly versions. You can do this at the very top of your script:

import eventlet
eventlet.monkey_patch()

Yes this solves my issue. Thank you!

Was this page helpful?
0 / 5 - 0 ratings