How to process multiple requests using the same PHP running instance?

355 Views Asked by At

As we know, PHP works in the "share nothing" phylosophy. It therefore is bound with serious performance limitations.

While a compiled script can be accelerated by some caching extensions we cannot avoid very heavy initialization (for example, we have a web-service and every call will at least require parsing and bootstrapping DTO schemas, setting up data bindings, connecting database (persistent mysql connections is really dirty hack from that perspective), opening another remote services and so on).

Also this problem seems to be solved by ReactPHP framework, but... do any lightweight non-framework solutions exist? Anything from hack ways, one-file examples to lightweight libraries (not frameworks) accepted. No complex web-server recreated functionality required. Just handling plain POST requests is enough.

<offtopic>

The task itself is so essential so I wonder why PHP is not providing this out-of-the-box yet...

</offtopic>

2

There are 2 best solutions below

0
On

One fine way to do it is have a CLI script that would act like your normal Java/nodeJS server. And use a light PHP gateway to receive the HTTP requests and require the needed info from the CLI through socket.

An interesting read is http://liveforeverbook.info/blog/2008/01/31/persistent-web-apps-in-php/

The key to this is that the CLI script keeps the message store in-memory (like the Java service), and communicates with the client through a web-based PHP “gateway”.

IRC server <--> PHP Gateway <--> Client The gateway step is not actually required, but helps to filter out the rubbish that might come through if the IRC server was connected directly to the net.

You start the IRC server running as a script from the console (php server.php), and leave it running. CLI scripts have no timeout, so it will keep running until you manually turn it off.

0
On

do any lightweight non-framework solutions exist? Anything from hack ways, one-file examples to lightweight libraries (not frameworks) accepted.

Depends on what you're looking for. You will need some sort of event loop / scheduler, which is mostly what amphp/amp / react/event-loop provide. If you don't want to die in a callback-hell, you'll need some form of promise implementation, provided by amphp/amp / react/promise.

If you see those parts as a framework and don't want to use those, you can of course write them yourself. But then again you're looking for stable software, any of these will be more stable than your own implementation, because they're way more widely used and bugs are reported and fixed.

Anything on top of these basics are just libraries. You can use them or write your own. If you happen to just need a socket server, why not use the libraries that already deal with PHP's edge cases as well as possible?

No complex web-server recreated functionality required. Just handling plain POST requests is enough.

Oh, what's a complex web-server? "Just" handling plain POST requests? You will need an HTTP protocol parser that deals correctly with pipelined requests, decodes chunked encoding, a multipart parser if you want to deal with uploaded files, etc.

If you're just looking for handling multiple requests in a single PHP process without repeated bootstrapping, I'd suggest taking a look at PHP-PM, but at this point it's not production ready.