2016-05-16 55 views
2

我使用Python和Tornado來構建具有jQuery UI滑塊元素的基本應用程序。我的目標是,當用戶與滑塊交互時,它將被髮送一個值給一個python函數,並且結果將顯示在python控制檯中。 我custom.js是:tornado.gen.BadYieldError:產生未知對象無

$(function() { 
$("#slider-range-max").slider({ 
    min : 0, 
    max : 100, 
    slide : function(event, ui) { 
     $("#amount").val(ui.value); 
     ajax({ 
       url: "/action", 
       data: {parameter:ui.value}, 
      }); 
     }, 
    }); 
    $("#amount").val($("#slider-range-max").slider("value")); 
}); 

main.py

define("port", default=8888, help="run on the given port", type=int) 
class Application(tornado.web.Application): 
    def __init__(self): 
    handlers = [ 
     (r"/", AuxHandler), 
     (r"/action", MainHandler) 
    ] 
    settings = { 
     "template_path": Settings.TEMPLATE_PATH, 
     "static_path": Settings.STATIC_PATH, 
    } 

    tornado.web.Application.__init__(self, handlers, **settings) 

class AuxHandler(tornado.web.RequestHandler): 
    def get(self): 
    self.render("index.html") 

class MainHandler(tornado.web.RequestHandler): 
    @asynchronous 
    @tornado.gen.coroutine 
    def get(self): 
    speed = int(self.get_argument("parameter")) 
    p=P() 
    if speed > 1: 
     p.startApp(speed) 
    if speed<1: 
     p.stopApp() 

def main(): 
    tornado.options.parse_command_line() 
    http_server = tornado.httpserver.HTTPServer(Application()) 
    http_server.listen(options.port) 
    tornado.ioloop.IOLoop.instance().start() 
if __name__ == "__main__": 
    main() 

和p.py

@tornado.gen.coroutine 
def startApp(self,speed): 
    x= yield print(speed) 
    while True: 
     yield x 

在控制檯我收到此:

12 
[I 160516 12:47:19 web:1946] 304 GET /action?parameter=12 (::1) 0.00ms 
13 
[I 160516 12:47:19 web:1946] 304 GET /action?parameter=13 (::1) 15.60ms 
14 
[E 160516 12:47:19 concurrent:336] Future <tornado.concurrent.Future object at 0x02FAA7D0> exception was never retrieved: Traceback (most recent call last): 
    File "C:\Users\home\AppData\Local\Programs\Python\Python35-32\lib\site-packages\tornado\gen.py", line 1014, in run 
    yielded = self.gen.throw(*exc_info) 
    File "E:\work\python\Example2\p.py", line 11, in startApp 
    x= yield print(speed) 
    File "C:\Users\home\AppData\Local\Programs\Python\Python35-32\lib\site-packages\tornado\gen.py", line 1008, in run 
    value = future.result() 
    File "C:\Users\home\AppData\Local\Programs\Python\Python35-32\lib\site-packages\tornado\concurrent.py", line 232, in result 
    raise_exc_info(self._exc_info) 
    File "<string>", line 3, in raise_exc_info 
    File "C:\Users\home\AppData\Local\Programs\Python\Python35-32\lib\site-packages\tornado\gen.py", line 1090, in handle_yield 
    self.future = convert_yielded(yielded) 
    File "C:\Users\home\AppData\Local\Programs\Python\Python35-32\lib\functools.py", line 743, in wrapper 
    return dispatch(args[0].__class__)(*args, **kw) 
    File "C:\Users\home\AppData\Local\Programs\Python\Python35-32\lib\site-packages\tornado\gen.py", line 1222, in convert_yielded 
    raise BadYieldError("yielded unknown object %r" % (yielded,)) 
tornado.gen.BadYieldError: yielded unknown object None 

我不不知道如何處理這個問題「產生了未知的問題目標無「錯誤或者我的方法是否正確。任何想法都會非常有幫助。

+0

您想獲得'打印()',它返回'None' – kwarunek

+0

,當我在瀏覽器中移動滑塊時,它會被髮送的值速度打印()。如果我沒有收益(在p.py中),將永遠打印這個值(速度),但對於收益率,對於第一個值,不會收到任何錯誤,但過了一段時間後我會收到錯誤。 –

回答

1

例外是從yield print(speed)print返回None,並且不能產生None。您只能產生期貨和類似的等待對象,通常在產生調用協程的結果時。請參閱Refactoring Tornado Coroutines以獲取調用協程的指南。

如果你要打印速度的價值,只是這樣做:

def startApp(self, speed): 
    print(speed) 
+0

我使用了yield,因爲由ajax調用的get方法(在main.py中)將根據速度(值)啓動函數p.startApp(速度)。當用戶從左到右(或從右到左)將瀏覽器中的滑塊移動到每個新值時,將調用p.startApp(速度)。我不明白,爲什麼自從我開始移動滑塊後一段時間後會出現錯誤。 –

相關問題