我使用Python和Tornado构建带有jquery UI滑块元素的基本应用程序。我的目标是,当用户与滑块交互时,它将向python函数发送一个值,结果将显示在python控制台中。 我的custom.js是:
$(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
我不知道如何处理这个&#34;产生了未知对象无&#34;错误或我的方法是否正确。任何想法都会非常有用。
答案 0 :(得分:1)
例外来自yield print(speed)
。 print
返回None
,您无法获得None
。您只能产生Futures和类似的等待对象,通常是在您产生调用协程的结果时。有关调用协同程序的指南,请参阅Refactoring Tornado Coroutines。
如果要打印速度值,请执行以下操作:
def startApp(self, speed):
print(speed)