与龙卷风和原型异步COMET查询 [英] Asynchronous COMET query with Tornado and Prototype
问题描述
我想写使用龙卷风和JS的Prototype库简单的Web应用程序。因此,客户端可以在服务器上执行长期任务。我想,这个工作异步运行 - 好让别人的客户可以浏览网页,并做一些东西,有
I'm trying to write simple web application using Tornado and JS Prototype library. So, the client can execute long running job on server. I wish, that this job runs Asynchronously - so that others clients could view page and do some stuff there.
下面是我得:
#!/usr/bin/env/ pytthon
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
from tornado.options import define, options
import os
import string
from time import sleep
from datetime import datetime
define("port", default=8888, help="run on the given port", type=int)
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.render("templates/index.html", title="::Log watcher::", c_time=datetime.now())
class LongHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
def get(self):
self.wait_for_smth(callback=self.async_callback(self.on_finish))
print("Exiting from async.")
return
def wait_for_smth(self, callback):
t=0
while (t < 10):
print "Sleeping 2 second, t={0}".format(t)
sleep(2)
t += 1
callback()
def on_finish(self):
print ("inside finish")
self.write("Long running job complete")
self.finish()
def main():
tornado.options.parse_command_line()
settings = {
"static_path": os.path.join(os.path.dirname(__file__), "static"),
}
application = tornado.web.Application([
(r"/", MainHandler),
(r"/longPolling", LongHandler)
], **settings
)
http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(options.port)
tornado.ioloop.IOLoop.instance().start()
if __name__ == "__main__":
main()
这是服务器的一部分。它有主视图(显示欢迎语,当前服务器时间和网址为Ajax查询时,执行长时间运行的工作。如果你preSS一个按钮,一个长期运行的作业执行,而且服务器挂起:(我不能查看任何页面,而这个作业运行。 这里是模板页面:
This is server part. It has main view (shows little greeting, current server time and url for ajax query, that executes long running job. If you press a button, a long running job executes. And server hangs :( I can't view no pages, while this job is running. Here is template page:
<html>
<head>
<title>{{ title }}</title>
<script type="text/javascript" language="JavaScript" src="{{ static_url("js/prototype.js")}}"></script>
<script type='text/javascript' language='JavaScript'>
offset=0
last_read=0
function test(){
new Ajax.Request("http://172.22.22.22:8888/longPolling",
{
method:"get",
asynchronous:true,
onSuccess: function (transport){
alert(transport.responseText);
}
})
}
</script>
</head>
<body>
Current time is {{c_time}}
<br>
<input type="button" value="Test" onclick="test();"/>
</body>
</html>
我是什么做错了吗?如何实现长时间池,采用旋风式和原型(或jQuery的)
what am I doing wrong? How can implement long pooling, using Tornado and Prototype (or jQuery)
PS:我已经看过聊天的例子,但是它太复杂了。不明白它是如何工作:(
PS: I have looked at Chat example, but it too complicated. Can't understand how it works :(
PSS下载完整的例如
推荐答案
龙卷风是单线程的Web服务器。在 wait_for_smith while循环
办法阻止旋风。
Tornado is single-threaded web server. Your while loop in wait_for_smith
method is blocking Tornado.
您可以重写该方法是这样的:
You can rewrite that method like this:
def wait_for_smth(self, callback, t=10):
if t:
print "Sleeping 2 second, t=%s" % t
tornado.ioloop.IOLoop.instance().add_timeout(time.time() + 2, lambda: self.wait_for_smth(callback, t-1))
else:
callback()
您需要添加导入时间
上方,使这项工作。
You need to add import time
at the top to make this work.
这篇关于与龙卷风和原型异步COMET查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!