2013-05-13 44 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') 

如果那是因爲你想不堵的響應,使非阻塞調用(如獲取網址),很容易足夠多的做手動:

  • 創建一個隊列對象(這是一個特殊的隊列,可以填充並在線程之間彈出)。
  • 創建並運行帶有無限循環的線程,取消排隊並每次執行操作。
  • 當您需要非阻塞呼叫時,將操作推送到隊列並攜帶一個。