如何用Python实现Comet服务器端?

发布于 2024-08-24 16:28:31 字数 196 浏览 2 评论 0原文

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

我刚刚在我的 XAMPP 中安装了 mod_python。我认为用Python异步编程实现Comet会很容易。但仍然不知道如何实施它。

有什么想法如何在 mod_python 中实现 Comet 吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷荒 2024-08-31 16:28:31

首先,我根本不是异步专家,我只是研究过这个主题一次。
恕我直言,如果您使用 XAMPP,那么您将失去进行长轮询的可能性,因为 Apache 对每个请求使用线程/进程(取决于配置)。

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

示例来自 Tornado 文档 /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()

-- 据我所知,这在 Apache 下是不可能的 - 其中 fetch 是请求处理程序的常规部分,当然会阻塞直到它完成 - 所以你怎么做以冻结线程或进程结束。

另一个在Python中做非阻塞服务的著名库是Twisted,但我对它了解不多,只是它还能够帮助您仅使用一个线程/进程处理大量连接。

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.

奢华的一滴泪 2024-08-31 16:28:31

我不确定您是否遇到过这个问题,但提出的问题非常相似,而且似乎那里有一些好的答案。 HTH。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文