How to implement Comet server side with Python?

后端 未结 2 1863
后悔当初
后悔当初 2020-12-14 13:32

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 sc

相关标签:
2条回答
  • 2020-12-14 14:03

    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 讨论(0)
  • 2020-12-14 14:10

    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.

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