Python 瓶子gevent和螺纹:gevent只能从单个螺纹上使用

Python 瓶子gevent和螺纹:gevent只能从单个螺纹上使用,python,bottle,gevent,python-multithreading,Python,Bottle,Gevent,Python Multithreading,我有一个python瓶子应用程序,它使用线程。由于我正在使用的事实,线程阻止了应用程序的执行(从线程触发的对话框阻止了瓶子路由对客户端的响应,直到被解除) 这里的一项小研究表明,我应该使用monkey patch,而不必尝试修补线程: # Patch python's threads with greenlets from gevent import monkey monkey.patch_all(thread=False) 这并没有阻碍我写的最小值 但是在大量使用线程时,使用threadin

我有一个python瓶子应用程序,它使用线程。由于我正在使用的事实,线程阻止了应用程序的执行(从线程触发的对话框阻止了瓶子路由对客户端的响应,直到被解除)

这里的一项小研究表明,我应该使用monkey patch,而不必尝试修补线程:

# Patch python's threads with greenlets
from gevent import monkey
monkey.patch_all(thread=False)
这并没有阻碍我写的最小值

但是在大量使用线程时,使用
threading.setEvent()

这是我得到的错误:

C:\Users\IEUser\downloadloft-localserver>python mainserver.py
Exception in thread Thread-1:
Traceback (most recent call last):
  File "C:\Program Files\DownloadLoft\Python27\lib\threading.py", line 551, in _
_bootstrap_inner
self.run()
  File "C:\Program Files\DownloadLoft\Python27\lib\threading.py", line 753, in r
un
self.finished.wait(self.interval)
  File "C:\Program Files\DownloadLoft\Python27\lib\threading.py", line 403, in w
ait
self.__cond.wait(timeout)
  File "C:\Program Files\DownloadLoft\Python27\lib\threading.py", line 262, in w
ait
_sleep(delay)
  File "c:\users\admini~1\appdata\local\temp\easy_install-sch3hm\gevent-0.13.8-p
y2.7-win32.egg.tmp\gevent\hub.py", line 79, in sleep
switch_result = get_hub().switch()
  File "c:\users\admini~1\appdata\local\temp\easy_install-sch3hm\gevent-0.13.8-p
y2.7-win32.egg.tmp\gevent\hub.py", line 135, in get_hub
raise NotImplementedError('gevent is only usable from a single thread')
NotImplementedError: gevent is only usable from a single thread

Bottle v0.12-dev server starting up (using GeventSocketIOServer())...
Listening on http://localhost:8080/
Hit Ctrl-C to quit.

Exception in thread Thread-2:
Traceback (most recent call last):
  File "C:\Program Files\DownloadLoft\Python27\lib\threading.py", line 551, in _
_bootstrap_inner
self.run()
  File "C:\Program Files\DownloadLoft\Python27\lib\threading.py", line 753, in r
un
self.finished.wait(self.interval)
  File "C:\Program Files\DownloadLoft\Python27\lib\threading.py", line 403, in w
ait
self.__cond.wait(timeout)
  File "C:\Program Files\DownloadLoft\Python27\lib\threading.py", line 262, in w
ait
_sleep(delay)
  File "c:\users\admini~1\appdata\local\temp\easy_install-sch3hm\gevent-0.13.8-p
y2.7-win32.egg.tmp\gevent\hub.py", line 79, in sleep
switch_result = get_hub().switch()
  File "c:\users\admini~1\appdata\local\temp\easy_install-sch3hm\gevent-0.13.8-p
y2.7-win32.egg.tmp\gevent\hub.py", line 135, in get_hub
raise NotImplementedError('gevent is only usable from a single thread')
NotImplementedError: gevent is only usable from a single thread

这是gevent.monkeypatch的已知问题吗?有什么想法吗?

瓶子应用程序是线程化的,所以不能在瓶子路由中调用的任何函数中使用gevent

为了帮助你,我需要推测你为什么使用线程

如果是为了加速你的瓶子网站,只需使用cherrypy服务器:

pip install cherrypy 
(或者在当前目录中转储cherrypy目录,这是一个纯Python服务器)

然后按以下方式运行瓶子应用程序:

bottle.run(server='cherrypy')
如果是因为您希望在不阻塞响应的情况下进行非阻塞调用(如获取URL),那么手动执行该操作非常简单:

  • 创建一个队列对象(它是一个特殊的队列,可以在线程之间填充和弹出)
  • 创建并运行一个具有无限while循环的线程,每次取消队列的绑定并执行操作
  • 当您需要一个非阻塞调用时,将该操作推送到队列并执行一个