我有一个使用线程的python瓶应用程序。由于我使用monkey.patch这一事实,线程阻止了应用程序执行(从线程触发的对话框阻止了瓶子路由响应客户端,直到被解雇。)
这里的一点研究表明我应该使用猴子补丁而不试图修补线程:
# Patch python's threads with greenlets
from gevent import monkey
monkey.patch_all(thread=False)
这不会阻止我写的最小example。
但是在使用线程进行密集使用时会引发这些错误,使用类似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的已知问题吗?有什么想法吗?
答案 0 :(得分:0)
瓶子应用程序是线程化的,所以你不能在瓶子路径中调用的任何函数中使用gevent。
为了帮助您,我需要推测您使用线程的原因。
如果要加速您的瓶子网站,请使用cherrypy服务器:
pip install cherrypy
(或者只是将当前目录中的樱桃目录转储,它是一个纯Python服务器)
然后以这种方式运行您的瓶子应用程序:
bottle.run(server='cherrypy')
如果是因为您希望在不阻止响应的情况下进行非阻塞调用(例如提取URL),则很容易手动执行: