开发者

How to implement Comet server side with Python?

I once tried to implement Comet in PHP. Soon, I found that PHP is not suitable for Comet, since each HTTP request will occupy one proc开发者_开发百科ess/thread. As a result, it doesn't scale well.

I just installed mod_python in my XAMPP. I thought it would be easy to implement Comet with Python asynchronous programming. But still cannot get a clue how to implement it.

Is there any idea how to implement Comet in mod_python?


First of all, I'm not async expert at all, I just investigated the topic once. IMHO if you're using XAMPP then you're loosing the posibility of doing long polling because Apache uses thread/processes (depending on configuration) for each request.

What you need, is non-blocking web server, like Tornado, that allows splitting requests into two parts, of which the second one is fired on some event, but meanwhile server can accept subsequent inbound requests.

Example from Tornado documentation /license/:

class MainHandler(tornado.web.RequestHandler):
    @tornado.web.asynchronous
    def get(self):
        http = tornado.httpclient.AsyncHTTPClient()
        http.fetch("http://friendfeed-api.com/v2/feed/bret",
               callback=self.async_callback(self.on_response))

    def on_response(self, response):
        if response.error: raise tornado.web.HTTPError(500)
        json = tornado.escape.json_decode(response.body)
        self.write("Fetched " + str(len(json["entries"])) + " entries "
                   "from the FriendFeed API")
        self.finish()

-- as far as I know this is not possible under Apache - in which fetch is regular part of request handler, which of course block until it's complete - so what you end with is frozen thread or process.

Another famous library for doing non-blocking services in Python is Twisted, but I don't know much about it, only that it also is able to help you in handling a lot of connections with only one thread/process.


I'm not sure if you came across this question, but the question asked is pretty similar and there seem to be some good answers there. HTH.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜