问题
Hopefully I can explain this well enough. I am working on creating a PHP library to handle ajax requests through PHP in an object oriented manner. I am currently pondering a good way to implement a long polling solution but am curious about something.
Apache doesn't handle keeping multiple connections open very well. The thread-per-request model makes Apache highly inefficient for long polling. Using server's such as nginx and lighttpd handle these threads much better so in the library, I plan on implementing different functions optimized to the specific servers available from a single function call. In the case of Apache, which happens to hold an enormous market share for PHP applications, I need a better solution.
Is there a way to place an incoming request on hold/pause and continue processing other requests until I am ready to reactivate it?
I'm not sure if that makes sense or if I am even on the right track. What is a possible solution that SO recommends for long polling in PHP on an Apache server?
回答1:
This sounds like continuations. You can definitely not do this in PHP, in any elegant way. If you want to do this, your best chance would be to save the current state, and write the code in such a way that you can resume from where you left off when you load the state.
回答2:
I don't think there is a solution. You can't distinguish a polling request from a normal request. Only avoid Apache that could help (e.g. running nginx on 80, forward all requests to Apache on 81, except polling requests).
I also don't think you have a problem. Nginx or other server is not much more efficient than Apache. Polling is a PHP request, Apache with mod_php is less or more a good choice. Nginx won't use less resource than Apache in serving PHP.
来源:https://stackoverflow.com/questions/9730857/long-polling-with-php-on-apache