使用 Tornado 和 Prototype 进行异步 COMET 查询

发布于 2024-08-22 19:04:33 字数 2784 浏览 6 评论 0原文

我正在尝试使用 Tornado 和 JS Prototype 库编写简单的 Web 应用程序。因此,客户端可以在服务器上执行长时间运行的作业。我希望这项工作异步运行 - 以便其他客户端可以查看页面并在那里做一些事情。

这是我得到的:

#!/usr/bin/env/ python

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 查询的 url,执行长时间运行的作业。如果按下按钮,则会执行长时间运行的作业。服务器挂起:( 我无法查看任何页面,而该作业正在运行。 这是模板页面:

<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>

我做错了什么?如何使用Tornado和Prototype(或jQuery)实现长池

PS:我已经看过Chat示例,但它太复杂了。无法理解它是如何工作的:(

PSS 下载完整的示例

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.

Here's what I've got:

#!/usr/bin/env/ python

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()

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>

what am I doing wrong? How can implement long pooling, using Tornado and Prototype (or jQuery)

PS: I have looked at Chat example, but it too complicated. Can't understand how it works :(

PSS Download full example

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

浊酒尽余欢 2024-08-29 19:04:33

Tornado 是单线程 Web 服务器。 wait_for_smith 方法中的 while 循环正在阻止 Tornado。

您可以像这样重写该方法:

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()

您需要在顶部添加导入时间才能使其工作。

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.

没企图 2024-08-29 19:04:33
function test(){
            new Ajax.Request("http://172.22.22.22:8888/longPolling",
            {
                method:"get",
                asynchronous:true,
                onSuccess: function (transport){
                    alert(transport.responseText);
                }
            })
        }

应该是

function test(){
            new Ajax.Request("/longPolling",
            {
                method:"get",
                asynchronous:true,
                onSuccess: function (transport){
                    alert(transport.responseText);
                }
            })
        }
function test(){
            new Ajax.Request("http://172.22.22.22:8888/longPolling",
            {
                method:"get",
                asynchronous:true,
                onSuccess: function (transport){
                    alert(transport.responseText);
                }
            })
        }

should be

function test(){
            new Ajax.Request("/longPolling",
            {
                method:"get",
                asynchronous:true,
                onSuccess: function (transport){
                    alert(transport.responseText);
                }
            })
        }
小傻瓜 2024-08-29 19:04:33

我已将 Tornado 的聊天示例转换为在 gevent 上运行。观看此处的现场演示这里的解释和源代码

它使用轻量级用户级线程(greenlet)并且速度相当/ Tornado 的内存使用。然而,代码很简单,您可以在处理程序中调用 sleep() 和 urlopen() 而不会阻塞整个进程,并且您可以生成执行相同操作的长时间运行的作业。在底层,应用程序是异步的,由用 C 编写的事件循环提供支持 (libevent )。

您可以在此处阅读简介

I've converted Tornado's chat example to run on gevent. Take a look at the live demo here and the explanation and source code here.

It uses lightweight user-level threads (greenlets) and is comparable in speed/memory use with Tornado. However, the code is straightforward, you can call sleep() and urlopen() in your handlers without blocking the whole process and you can spawn long running jobs that do the same. Under the hood the application is asynchronous, powered by an event loop written in C (libevent).

You can read the introduction here.

绅士风度i 2024-08-29 19:04:33

我读过 Ted Roden 的“构建实时用户体验”一书,这非常有帮助。我成功地使用 Tornado (python) 创建了一个复杂的实时聊天系统。我推荐您阅读这本书以及 John Goerzen 的 《Python 网络编程基础》

I have read the book called "Building the Realtime User Experience" by Ted Roden, and it was very helpful. I have managed to create a complex realtime chat system using Tornado (python). I recommend this book to be read as well as "Foundations of Python Network Programming" by John Goerzen.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文