2013-05-13 40 views
3

我有一个使用线程的python瓶子应用程序。由于我使用monkey.patch,线程被阻塞的应用程序执行(从一个线程中触发从响应客户端阻塞瓶的路线,直到被解散的对话框。)瓶子的gevent和线程:gevent只能从一个线程使用

一个小小的研究显示,在这里,我应该使用猴补丁,但不尝试补丁主题:

# 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

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

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

如果是加速你的瓶子网站,中庸之道使用CherryPy的服务器:

pip install cherrypy 

(或只是倾倒在你的当前目录的CherryPy的目录,它是一个纯Python服务器)

然后运行瓶的应用程序是这样的:

bottle.run(server='cherrypy') 

如果那是因为你想不堵的响应,使非阻塞调用(如获取网址),很容易足够多的做手动:

  • 创建一个队列对象(这是一个特殊的队列,可以填充并在线程之间弹出)。
  • 创建并运行带有无限循环的线程,取消排队并每次执行操作。
  • 当您需要非阻塞呼叫时,将操作推送到队列并携带一个。