What is the most efficient way to spawn and shutdown PHP processes?
I'm currently looking at spawning 50+ PHP worker script insta开发者_运维百科nces that will be mostly network bound. They will dequeue from a central job queue and execute their work, then grab more work from the queue. I expect these will be just while(1) jobs that run.
At startup I'll need to spawn these processes, what is the best way to spawn the PHP scripts aside from the obvious exec in a loop? I'd also like to be able to gracefully shutdown.
I could put some code in for looking for a shutdown notice but wasn't sure if there was a cleaner way in PHP
thanks
Look into the PCNTL functions specifically functions like pcntl_fork()
The way we currently use it is within a loop each new process forks off does it's job and exits. While the main loop keeps going processing info etc...
Look at Gearman, thats exactly, what you are talking about: Jobs and Workers.
If possible avoid forking php scripts. It makes things messy and it's something even Rasmus Lerdof of PHP fame thinks is a bad idea. PHP wasn't meant for long running processes which is what your parent process that does the forking would be.
A better solution (but not the only) would be to use something like pythons supervisord (no python knowledge required to use) to manage the php workers. It cam restart dead workers, restart live ones after a preset time, or when they exceed a memory limit.
For extra win, you could also use gearman for scheduling the work.
精彩评论