在PHP中使用Comet吗? [英] Using comet with PHP?

查看:108
本文介绍了在PHP中使用Comet吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当时正在考虑使用PHP后端实现实时聊天,但是我在讨论彗星的网站上遇到了此评论:

I was thinking of implementing real time chat using a PHP backend, but I ran across this comment on a site discussing comet:

我的理解是PHP是一个 彗星的糟糕语言,因为 彗星要求您保持 持久的连接向每个人开放 浏览器客户端.使用mod_php这个 意味着绑一个Apache孩子 专职为每个客户 根本无法扩展.我的人 知道做彗星的东西大部分是 使用设计的Twisted Python 处理成千上万的 同时连接.

My understanding is that PHP is a terrible language for Comet, because Comet requires you to keep a persistent connection open to each browser client. Using mod_php this means tying up an Apache child full-time for each client which doesn’t scale at all. The people I know doing Comet stuff are mostly using Twisted Python which is designed to handle hundreds or thousands of simultaneous connections.

这是真的吗?还是可以围绕它进行配置?

Is this true? Or is it something that can be configured around?

推荐答案

同意/扩展已经说过的内容,我认为FastCGI不能解决问题.

Agreeing/expanding what has already been said, I don't think FastCGI will solve the problem.

每个进入Apache的请求都将使用一个工作线程,直到请求完成为止,这对于COMET请求来说可能是很长的时间.

Each request into Apache will use one worker thread until the request completes, which may be a long time for COMET requests.

有关Ajaxian的文章提到在Apache上使用COMET,并且很难.该问题并非特定于PHP,而是适用于您可能希望在Apache上使用的任何后端CGI模块.

This article on Ajaxian mentions using COMET on Apache, and that it is difficult. The problem isn't specific to PHP, and applies to any back-end CGI module you may want to use on Apache.

建议的解决方案是使用事件" MPM模块更改了将请求分配到工作线程的方式.

The suggested solution was to use the 'event' MPM module which changes the way requests are dispatched to worker threads.

此MPM试图修复 HTTP中的保持活动状态". 客户完成第一个之后 要求,客户可以保留 连接已打开,并进一步发送 请求使用相同的套接字.这 可以节省大量的开销 创建TCP连接.然而, 传统上,Apache会保留整个 子进程/线程等待数据 来自客户,这带来了自己的 缺点.为了解决这个问题, 该MPM使用专用线程 处理两个侦听套接字,并 保持活动状态中的所有套接字 状态.

This MPM tries to fix the 'keep alive problem' in HTTP. After a client completes the first request, the client can keep the connection open, and send further requests using the same socket. This can save signifigant overhead in creating TCP connections. However, Apache traditionally keeps an entire child process/thread waiting for data from the client, which brings its own disadvantages. To solve this problem, this MPM uses a dedicated thread to handle both the Listening sockets, and all sockets that are in a Keep Alive state.

不幸的是,这也不起作用,因为它只会在请求完成后才会延后" ,等待客户端的新请求.

Unfortunately, that doesn't work either, because it will only 'snooze' after a request is complete, waiting for a new request from the client.

现在,考虑问题的另一面,即使通过在每个彗星请求中保留一个线程来解决该问题,每个请求仍将需要一个PHP线程-这就是为什么FastCGI无法提供帮助.

Now, considering the other side of the problem, even if you resolve the issue with holding up one thread per comet request, you will still need one PHP thread per request - this is why FastCGI won't help.

您需要类似 Continuations 之类的东西,它们允许彗星请求在事件发生时得以恢复被触发. AFAIK,这在PHP中是不可能的.我只在Java中看到过它-请参阅Apache Tomcat服务器.

You need something like Continuations which allow the comet requests to be resumed when the event they are triggered by is observed. AFAIK, this isn't something that's possible in PHP. I've only seen it in Java - see the Apache Tomcat server.

这里有文章关于使用负载平衡器( HAProxy )来允许您同时运行apache服务器和启用了彗星的服务器(例如码头,用于Java的tomcat)在同一服务器的端口80上.

There's an article here about using a load balancer (HAProxy) to allow you to run both an apache server and a comet-enabled server (e.g. jetty, tomcat for Java) on port 80 of the same server.

这篇关于在PHP中使用Comet吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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