Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用gevent.queue.queue.get():gevent.hub.LoopExit:';此操作将永远阻止';_Python_Multithreading_Flask_Uwsgi_Gevent - Fatal编程技术网

Python 使用gevent.queue.queue.get():gevent.hub.LoopExit:';此操作将永远阻止';

Python 使用gevent.queue.queue.get():gevent.hub.LoopExit:';此操作将永远阻止';,python,multithreading,flask,uwsgi,gevent,Python,Multithreading,Flask,Uwsgi,Gevent,在过去的几天里,我一直在尝试将事件流集成到我的flask应用程序中,在本地测试中取得了很好的结果,但在服务器上运行带有uWSGI的应用程序时,结果有些糟糕。我的代码基本上是建立在烧瓶的基础上的。我正在使用python3.4.2 问题 在我的uWSGI服务器上运行应用程序时,每当客户端尝试连接到/streaming端点时,就会引发gevent.hub.LoopExit:“此操作将永远被阻止”。。我的假设是,这是由于无限期地对空队列调用get()造成的 完全回溯: Traceback (most r

在过去的几天里,我一直在尝试将事件流集成到我的flask应用程序中,在本地测试中取得了很好的结果,但在服务器上运行带有uWSGI的应用程序时,结果有些糟糕。我的代码基本上是建立在烧瓶的基础上的。我正在使用
python3.4.2

问题 在我的uWSGI服务器上运行应用程序时,每当客户端尝试连接到
/streaming
端点时,就会引发
gevent.hub.LoopExit:“此操作将永远被阻止”。
。我的假设是,这是由于无限期地对空队列调用
get()
造成的

完全回溯:

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/werkzeug/wsgi.py", line 691, in __next__
    return self._next()
  File "/usr/lib/python3/dist-packages/werkzeug/wrappers.py", line 81, in _iter_encoded
    for item in iterable:
  File "./voting/__init__.py", line 49, in gen
    result = queue.get(block=True)
  File "/usr/local/lib/python3.4/dist-packages/gevent/queue.py", line 284, in get
    return self.__get_or_peek(self._get, block, timeout)
  File "/usr/local/lib/python3.4/dist-packages/gevent/queue.py", line 261, in __get_or_peek
    result = waiter.get()
  File "/usr/local/lib/python3.4/dist-packages/gevent/hub.py", line 878, in get
    return self.hub.switch()
  File "/usr/local/lib/python3.4/dist-packages/gevent/hub.py", line 609, in switch
    return greenlet.switch(self)
gevent.hub.LoopExit: ('This operation would block forever', <Hub at 0x7f717f40f5a0 epoll default pending=0 ref=0 fileno=6>)
将事件添加到队列:

def notify():
    msg = {"type": "users", "data": db_get_all_registered(session_id)}
    subscriptions.add_item(session_id, msg)      # Adds the item to the relevant queues.

gevent.spawn(notify)
如前所述,它在本地运行良好,使用
werkzeug

from app import app
from gevent.wsgi import WSGIServer
from werkzeug.debug import DebuggedApplication

a = DebuggedApplication(app, evalex=True)
server = WSGIServer(("", 5000), a)
server.serve_forever()
我试过的
  • 使用
    Monkey.patch_all()
    进行Monkey修补

  • 队列
    切换到
    可连接队列

  • gevent.sleep(0)
    Queue.get()结合使用


该异常基本上意味着该循环/线程中没有其他运行的greenlet可以切换到。因此,当greenlet进入阻塞(queue.get())时,集线器无处可去,无事可做

同样的代码也可以在gevent的WSGIServer中使用,因为服务器本身是一个运行socket.accept循环的greenlet,所以总是有另一个greenlet可以切换到。但显然uwsgi不是这样工作的


解决这一问题的方法是安排其他Greenlet运行。例如,与其生成一个greenlet按需通知,不如安排这样一个greenlet已经在自己的队列中运行并阻塞。

Aha这很有意义。我想这就是我在使用图书馆之前没有学习图书馆的原因。你解决了你的问题吗?
from app import app
from gevent.wsgi import WSGIServer
from werkzeug.debug import DebuggedApplication

a = DebuggedApplication(app, evalex=True)
server = WSGIServer(("", 5000), a)
server.serve_forever()