异常gevent.hub.LoopExit:LoopExit('此操作将永远阻止') [英] Exception gevent.hub.LoopExit: LoopExit('This operation would block forever',)

查看:2113
本文介绍了异常gevent.hub.LoopExit:LoopExit('此操作将永远阻止')的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用Websockets运行我的Flask应用程序时,我总是遇到这个错误。
我试图遵循本指南 - http:/ /blog.miguelgrinberg.com/post/easy-websockets-with-flask-and-gevent



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

 尝试:
如果l.isRunning == False:#如果线程已经关闭
l.isRunning = True#将其更改为true,因此可以再循环
running = True $ b $ l.start()#启动永久循环/从顶部声明为全局变量
print str(running)
else:
running =真
打印str(运行)
l.start()
除了例外,e:
提高e
返回flask.render_template('test.html',正在运行= running)#goes到test.html页面

嗅探器运行良好,没有socketio,我是能够在遍历我的gui时嗅探网络。但是,当我在代码中包含socketio时,首先看到socketio在我的索引页中工作,并且我能够接收到消息f rom服务器到页面。我也可以很好地在我的GUI中遍历其他静态页面;但是,激活我的线程化网络嗅探器会导致浏览器挂起。我总是得到异常gevent.hub.LoopExit:LoopExit('此操作将永远阻止')错误,当我重新运行我的程序时,控制台会说,地址已被使用。在我看来,我可能不会正确地关闭套接字,因为发生这种情况。我也认为有些操作是基于错误而阻塞的。在我的python烧瓶应用程序中的代码如下所示:

$ p $ def c background_thread():
发送服务器生成的事件给客户端。
count = 0
while True:
time.sleep(10)
count + = 1
socketio.emit如果socketflag是None,
thread = Thread(target = background_thread) )
thread.start()


@ socketio.on('my event',namespace ='/ test')
def test_message(message):
emit('my response',{'data':message ['data']})
$ b $ 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':'Connec (''disconnect','namespace ='/ test')
def test_disconnect():
print('Client disconnected')

以下是我的索引页中的代码。

 < script type =text / javascriptcharset =utf-8> 
$(document).ready(function(){
namespace ='/ test'; //改为空字符串以使用全局命名空间

//套接字.io文档建议在连接时发送一个显式包
//当使用全局命名空间时,这是特别重要的
var socket = io.connect('http://'+ document.domain +': + location.port + namespace);
socket.on('connect',function(){
socket.emit('my event',{data:'I \'m connected!'} );
});

//服务器发送数据的事件处理程序
//数据显示在页面的已收到部分
套接字中。 ('my response',function(msg){
$('#log')。append('< br> Received#'+ msg.count +':'+ msg.data);
});
});
< / script>


解决方案

我碰到这个问题,由于没有正确修改线程模块造成的。



在应用程序的顶部,应用gevent补丁:

  from gevent import monkey,sleep 
monkey.patch_all()


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>

解决方案

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()

这篇关于异常gevent.hub.LoopExit:LoopExit('此操作将永远阻止')的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆