如何用Python实现Comet服务器端? [英] How to implement Comet server side with Python?

查看:177
本文介绍了如何用Python实现Comet服务器端?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾经尝试用PHP实现Comet。很快,我发现PHP不适合Comet,因为每个HTTP请求将占用一个进程/线程。结果,它无法很好地扩展。



我刚刚在XAMPP中安装了mod_python。我认为使用Python异步编程来实现Comet会很容易。



有没有办法在mod_python中实现Comet?

恕我直言,如果您使用的是XAMPP,那么您就失去了进行长时间轮询的可能性,因为Apache为每个请求使用线程/进程(取决于配置)。



您需要的是无阻塞的Web服务器,例如 Tornado ,该服务器可以将请求分为两部分,其中第二个事件是在某个事件上触发的,但与此同时服务器可以接受后续的入站请求。



示例来自龙卷风文档 / license /:

  class MainHandler(tornado.web.RequestHandler):
@ tornado.web.asynchronous
def获取(自己):
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):
如果response.error:引发tornado.web.HTTPError(500)
json = tornado.escape.json_decode(response.body)
self.write( Fetched + str(len(json [ entries]))+ entry
来自FriendFeed API)
self.finish()

-据我所知,在Apache下这是不可能的-在其中,获取是请求处理程序的常规部分,当然阻塞直到完成为止-所以最终的结果是冻结的线程或进程。



另一个在Python中进行非阻塞服务的著名库是扭曲,但我对此了解不多,只是它还能够帮助您仅用一个线程处理许多连接/ process。


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?

解决方案

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.

这篇关于如何用Python实现Comet服务器端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆