gevent.hub.LoopExit异常:LoopExit('此操作将永远阻止',)

Pau*_*les 4 python multithreading flask socket.io flask-socketio

使用Websockets运行Flask应用程序时,总是出现此错误。我尝试遵循此指南-http://blog.miguelgrinberg.com/post/easy-websockets-with-flask-and-gevent

我有一个flask应用程序,为我的网络嗅探器提供GUI界面。嗅探器位于线程内,如下所示:(l是我的嗅探器线程; isRunning是一个布尔值,用于检查线程是否已在运行)

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
Run Code Online (Sandbox Code Playgroud)

嗅探器在没有socketio的情况下运行良好,我可以在遍历gui的同时嗅探网络。但是,当我在代码中包含socketio时,我首先看到socketio在索引页面中正常工作,并且能够接收来自服务器到页面。我还可以遍历GUI中的其他静态方法;但是,激活线程网络嗅探器会使浏览器挂断。我总是收到异常gevent.hub.LoopExit:LoopExit('此操作将永远阻止',)错误,当我重新运行程序时,控制台会说该地址已被使用。在我看来,这种情况可能无法正确关闭我的套接字。我还认为某些操作正在阻止该错误。我的python flask应用程序中的代码如下所示

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')
Run Code Online (Sandbox Code Playgroud)

这是我的索引页面中显示的代码。

<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>
Run Code Online (Sandbox Code Playgroud)

toh*_*ter 7

我前一段时间遇到了这个问题,这是由于未threading正确修补模块而造成的。

在您应用的顶部,应用gevent补丁:

from gevent import monkey, sleep
monkey.patch_all()
Run Code Online (Sandbox Code Playgroud)


小智 4

您应该避免与 gevent 一起使用time.sleep()。你应该改用gevent.sleep()time.sleep()将阻塞 gevent 循环