Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception gevent.hub.LoopExit: LoopExit('This operation would block forever',)

I am always getting this error when running my Flask App with Websockets. I have tried to follow this guide - http://blog.miguelgrinberg.com/post/easy-websockets-with-flask-and-gevent

I have a flask app that provides the GUI interface for my network sniffer. The sniffer is inside a thread as shown below : ( l is the thread for my sniffer; isRunning is a boolean to check if the thread is already running)

try:
    if l.isRunning == False:  # if the thread has been shut down
        l.isRunning = True  # change it to true, so it could loop again
        running = True
        l.start()  # starts the forever loop / declared from the top to be a global variable
        print str(running)
    else:
        running = True
        print str(running)
        l.start()
except Exception, e:
    raise e
return flask.render_template('test.html', running=running)  #goes to the test.html page

The sniffer runs fine without the socketio and i am able to sniff the network while traversing my gui.However, when I included the socketio in the code, I first see the socketio working in my index page and i am able to receive the messages from the server to the page. I could also traverse fine to the other static paages in my GUI ;however, activating my threaded network sniffer would leave my browser hangup. I always get the Exception gevent.hub.LoopExit: LoopExit('This operation would block forever',) error and when I rerun my program, the console would say that the address is already in use. It seems to me that I may not be closing my sockets correctly as this is happening. I also think that some operation is blocking based from the error. The code in my python flask app is shown below

def background_thread():
    """Example of how to send server generated events to clients."""
    count = 0
    while True:
        time.sleep(10)
        count += 1
        socketio.emit('my response',{'data': 'Server generated event', 'count': count},namespace='/test')
if socketflag is None:
    thread = Thread(target=background_thread)
    thread.start()


@socketio.on('my event', namespace='/test')
def test_message(message):
    emit('my response', {'data': message['data']})

@socketio.on('my broadcast event', namespace='/test')
def test_message(message):
    emit('my response', {'data': message['data']}, broadcast=True)

@socketio.on('connect', namespace='/test')
def test_connect():
    emit('my response', {'data': 'Connected'})

@socketio.on('disconnect', namespace='/test')
def test_disconnect():
    print('Client disconnected')

Here is the code present in my index page.

<script type="text/javascript" charset="utf-8">
        $(document).ready(function() {
            namespace = '/test'; // change to an empty string to use the global namespace

            // the socket.io documentation recommends sending an explicit package upon connection
            // this is specially important when using the global namespace
            var socket = io.connect('http://' + document.domain + ':' + location.port + namespace);
            socket.on('connect', function () {
                socket.emit('my event', {data: 'I\'m connected!'});
            });

            // event handler for server sent data
            // the data is displayed in the "Received" section of the page
            socket.on('my response', function (msg) {
                $('#log').append('<br>Received #' + msg.count + ': ' + msg.data);
            });
        });
  </script>
like image 581
Paul Gonzales Avatar asked Mar 28 '15 17:03

Paul Gonzales


2 Answers

I ran into this problem a while back and it was caused by not monkey-patching the threading module correctly.

At the top of your app, apply the gevent patches:

from gevent import monkey, sleep
monkey.patch_all()
like image 76
tohster Avatar answered Sep 22 '22 02:09

tohster


you should avoid using time.sleep() with gevent . You should use gevent.sleep() instead. time.sleep() will block the gevent loop

like image 30
Bogdan L Avatar answered Sep 22 '22 02:09

Bogdan L