2016-05-31 82 views
0

我是相當新的使用協程編程,我試圖建立一個使用Python 2.7.10的Tornado web服務器的自定義數據庫的數據庫接口。但是,我不斷收到BadYieldError。我覺得這可能是我不瞭解如何在Python的yield中完全使用tornado.gen.coroutine。我認爲我如何產生未來可能會出現問題。Python BadYieldError當產生未來

這是我的代碼保持失敗,其中第15行的testGet函數是對外部數據庫訪問的模擬。

from tornado import gen 
import tornado.ioloop 
import tornado.web 


class MainHandler(tornado.web.RequestHandler): 
    @gen.coroutine 
    def get(self): 
     response = "Hello, world" 
     response = yield db_interface("default", "user_query") 
     self.write(str(response)) 


# meant to demonstrate 
def testGet(query): 
    gen.sleep(2) 
    response = query 
    return response 


@gen.coroutine 
def db_interface(db, key): 
    print str(db) 
    d = yield testGet(key) 
    raise gen.Return(d) 


def make_app(): 
    return tornado.web.Application([ 
     (r"/", MainHandler), 
    ]) 


if __name__ == "__main__": 
    app = make_app() 
    app.listen(8889) 
    tornado.ioloop.IOLoop.current().start() 

輸出:

default 
ERROR:tornado.application:Uncaught exception GET/(127.0.0.1) 
HTTPServerRequest(protocol='http', host='127.0.0.1:8889', method='GET', uri='/', version='HTTP/1.1', remote_ip='127.0.0.1', headers={'Accept-Language': 'en-CA,en;q=0.8,fr;q=0.6', 'Accept-Encoding': 'gzip, deflate, sdch', 'Host': '127.0.0.1:8889', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36', 'Dnt': '1', 'Connection': 'keep-alive', 'Cookie': 'ui-auth-127.0.0.1%3A8091=4f1301ef98f1b6fbd80ad059cd5aa2dc', 'Cache-Control': 'max-age=0', 'Upgrade-Insecure-Requests': '1'}) 
Traceback (most recent call last): 
    File "/usr/local/lib/python2.7/site-packages/tornado/web.py", line 1415, in _execute 
    result = yield result 
    File "/usr/local/lib/python2.7/site-packages/tornado/gen.py", line 870, in run 
    value = future.result() 
    File "/usr/local/lib/python2.7/site-packages/tornado/concurrent.py", line 215, in result 
    raise_exc_info(self._exc_info) 
    File "/usr/local/lib/python2.7/site-packages/tornado/gen.py", line 876, in run 
    yielded = self.gen.throw(*exc_info) 
    File "test.py", line 12, in get 
    response = yield db_interface("default", "user_query") 
    File "/usr/local/lib/python2.7/site-packages/tornado/gen.py", line 870, in run 
    value = future.result() 
    File "/usr/local/lib/python2.7/site-packages/tornado/concurrent.py", line 215, in result 
    raise_exc_info(self._exc_info) 
    File "/usr/local/lib/python2.7/site-packages/tornado/gen.py", line 876, in run 
    yielded = self.gen.throw(*exc_info) 
    File "test.py", line 26, in db_interface 
    d = yield testGet(key) 
    File "/usr/local/lib/python2.7/site-packages/tornado/gen.py", line 870, in run 
    value = future.result() 
    File "/usr/local/lib/python2.7/site-packages/tornado/concurrent.py", line 215, in result 
    raise_exc_info(self._exc_info) 
    File "/usr/local/lib/python2.7/site-packages/tornado/gen.py", line 956, in handle_yield 
    self.future = convert_yielded(yielded) 
    File "/usr/local/lib/python2.7/site-packages/tornado/gen.py", line 1026, in convert_yielded 
    raise BadYieldError("yielded unknown object %r" % (yielded,)) 
BadYieldError: yielded unknown object 'user_query' 
ERROR:tornado.access:500 GET/(127.0.0.1) 2011.67ms 

我一直在試圖尋找如何做到這一點上正確Tornado's official documentationthis blog,但我不認爲我已經能夠完全掌握什麼他們在說如何使用協程。

回答

2

要麼轉成testGet這樣的協同程序,以便它返回一個未來:

@gen.coroutine 
def testGet(query): 
    gen.sleep(2) 
    raise gen.Return(query) 

還是不要用它yieldd = testGet(key)

如果你這樣做,代碼工作。對於返回Future s的函數,您應該使用yield

+0

好'testGet'將是一個數據庫調用,而不是一個函數,所以我不能把它變成一個協程。有什麼辦法可以從一個函數產生?如果是這樣,你認爲這是一個壞主意? – goatandsheep

+0

我在答案中給出了兩個選項:convert to coroutine或從'd = yield testGet(key)'中刪除'yield'。你說這兩個選項都不行嗎? – Louis

+0

我不可能將'testGet'變成協程,因爲它不是一個函數,而是我用於演示目的的一個外部調用的表示。第二個工程,但爲什麼?難道不可能產生代表未來的未來嗎?如果是這樣,我做錯了什麼,你的解決方案更好? – goatandsheep