我有一个使用线程的python瓶应用程序。由于我使用的是 monkey.patch ,这些线程阻止了应用执行(从线程触发的对话框阻止瓶路线响应客户端,直到被关闭。)
I have a python bottle application, which uses threads. due to the fact I'm using monkey.patch, the threads were blocking app execution (a dialog box fired from a thread was blocking bottle routes from responding to the client, until dismissed.)
此处的一项小研究表明,我应该使用猴子补丁而不要尝试补丁线程:
A little research here showed I should use monkey patch without trying to patch Thread:
# Patch python's threads with greenlets from gevent import monkey monkey.patch_all(thread=False)这不会在最小的示例。我写过。
This does not block on a minimal example I wrote.
但是在与线程大量使用时会出现这些错误,使用像 threading.setEvent() 这样的方法,这是我得到的错误:
But raises these errors on intensive use with Threads, with methods like threading.setEvent() This is the error I get:
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 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的已知问题?有任何想法吗?
Is this a known issue with gevent.monkeypatch? Any ideas?
推荐答案瓶子应用程序是线程化的,所以您不能在瓶子路线中调用的任何函数中使用gevent。
Bottle applications are threaded, so you can't use gevent in any function called in bottle routes.
为帮助您,我需要推测您为什么使用线程。
To help you, I need to speculate why you use threads.
如果是为了加速您的酒瓶网站,请贾斯汀使用cherrypy服务器:
If it's to accelerate your bottle website, juste use the cherrypy server :
pip install cherrypy(或者只是将当前目录中的cherrypy目录转储,这是一个纯Python服务器)
(or just dump the cherrypy dir in your current dir, it's a pure Python server)
然后运行您的bottle应用方式:
Then run your bottle app this way :
bottle.run(server='cherrypy')如果是因为您希望在不阻止响应的情况下进行非阻止性调用(例如,获取URL),则很容易手动进行操作:
If it's because you want to make non blocking calls (such as fetching URLs) without blocking the response, it's easy enought to do it manually :
- 创建一个队列对象(这是一个特殊的队列,可以在线程之间填充并弹出)。
- 创建并运行一个无限while循环的线程取消弹出队列并每次执行操作时间。
- 当您需要无阻塞呼叫时,将操作推入队列并携带一个。
更多推荐
bottle gevent和线程:gevent仅可用于单个线程
发布评论