Asynchronous COMET query with Tornado and Prototype

后端 未结 4 635
说谎
说谎 2021-01-30 15:10

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 Asynchronou

相关标签:
4条回答
  • 2021-01-30 15:45
    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);
                    }
                })
            }
    
    0 讨论(0)
  • 2021-01-30 15:46

    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.

    0 讨论(0)
  • 2021-01-30 16:00

    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.

    0 讨论(0)
  • 2021-01-30 16:05

    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.

    0 讨论(0)
提交回复
热议问题