3

Instead of starting new instances of a PHP script when an HTTP request is received, is there any way for one PHP script to handle multiple requests?

3
  • 2
    Why do you want something like that? That seems to break the basic stateless HTTP metaphor; can you explain your scenario in detail, and we'll be able to offer a more comprehensive, bigger picture answer? Sep 29, 2009 at 19:07
  • 1
    Well basically imagine my AJAX search program keeps asking questions to the PHP script ... gimme the results for this keyword, gimme the results for that keyword, etc. The start of the PHP script reads thru a MySQL DB and initializes, so if the PHP keeps restarting, it will have to read the DB millions of times. If I can keep it alive and let multiple silly AJAX requests be served by it I'm sure it would be better. Sep 29, 2009 at 19:23
  • 1
    Ah I see. Then all you need is a simple caching solution. Memcached or APC would be just what the doctor ordered. All you need to solve is the problem of cache stampedes, which has been answered here a couple of times. Sep 29, 2009 at 20:52

6 Answers 6

2

PHP is built around the "Share Nothing" concept which gives you the ability to load balance and scale an application better by having a distributed network. So "no" this cant be done. If you imagine the initiation costs are high then maybe adjust the architecture to conceptually 'cache' your objects/data/views as much as you can. Use serialize() or something.

1

Haven't seen an implementation for http requests for that. All I've been able to accomplish is that you wait for all the requests to come back. You could do this in command line by forking the process and sending it to the background. Or you could utilize Gearman (distributed work) for that.

4
  • "is that you wait for all the requests to come back" ?? what sort of response do you expect from a client? After the request is over, after PHP sends some HTTP text, how can the client respond in the same request so as to be handled by the same PHP? Sep 29, 2009 at 19:10
  • I was thinking of the problem the other way round. You receive 1 request and you instantiate several new requests. Sep 29, 2009 at 19:14
  • How do I initiate requests from PHP? What does that mean? Sep 29, 2009 at 19:17
  • It means that PHP in response to a request sends out more requests (think web services). Sep 29, 2009 at 20:51
1

If you make the file an HTTP server and run it as a process, then yes.

If it gets ran through Apache and mod_php, no.

(why on earth would you want that anyway?)

2
  • So that one script can keep track of repetitive requests from a client, instead of re-initialising every time. Sep 29, 2009 at 19:04
  • That's a feature/limitation of PHP depending on how you look at it. Usually tracking client data is done using $_SESSION, which is probably what you should look into - or a completely different technology. Sep 29, 2009 at 19:08
1

As far as I know, there isn't a way to do that. Closest thing I can think of is using a php opcode cache like (xcache or APC). Those will cache the code for faster script execution. I believe every request will have its own instance of the script.

1

What you want is to cache data.

Your php script should simply check to see if there are valid data for the request in the cache. If not, then do your database read, update the cache, and return the results to the user.

I would suggest looking into various caching libraries and carefully considering how you will scale your cache. One place to start is Zend_Cache, possibly with memcached on the back-end.

0

The scripts that handle the HTTP requests can get the data from a small PHP daemon using sockets.

Here is a useful library for PHP daemons: http://github.com/kvz/system_daemon

And some documentation:

http://kevin.vanzonneveld.net/techblog/article/create_daemons_in_php/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.