![](/img/trans.png)
[英]Using gevent.queue.Queue.get(): gevent.hub.LoopExit: 'This operation would block forever'
[英]Exception gevent.hub.LoopExit: LoopExit('This operation would block forever',)
运行带有Websockets的Flask应用程序时,我总是收到此错误。 我试图按照本指南 - http://blog.miguelgrinberg.com/post/easy-websockets-with-flask-and-gevent
我有一个烧瓶应用程序,为我的网络嗅探器提供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
嗅探器运行良好没有socketio我能够在遍历我的gui时嗅探网络。但是,当我在代码中包含socketio时,我首先看到socketio在我的索引页面中工作,我能够从服务器到页面。 我也可以遍历我的GUI中的其他静态页面;但是,激活我的线程网络嗅探器会让我的浏览器挂起。 我总是得到Exception gevent.hub.LoopExit:LoopExit('此操作将永远阻止')错误,当我重新运行我的程序时,控制台会说该地址已被使用。 在我看来,我可能没有正确关闭我的套接字,因为这种情况正在发生。 我还认为某些操作是基于错误而阻塞的。 我的python烧瓶应用程序中的代码如下所示
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')
这是我的索引页面中的代码。
<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>
我不久前遇到了这个问题,这是因为没有正确修补threading
模块。
在应用顶部,应用gevent补丁:
from gevent import monkey, sleep
monkey.patch_all()
你应该避免使用gevent的time.sleep time.sleep()
。 你应该使用gevent.sleep()
代替。 time.sleep time.sleep()
将阻止gevent循环
来自https://github.com/gevent/gevent/issues/585#issuecomment-122406552
确保您的GEvent Pool大小超过1.如果将其设置为1,则GEvent在处理事件时将不会有任何工作人员切换到这样做,因此会抱怨它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.