How to implement Comet server side with Python?

有些话、适合烂在心里 提交于 2019-11-27 13:35:38

问题


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 process/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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/2441533/how-to-implement-comet-server-side-with-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!