I have some long-running CLI PHP scripts that run regularly via cron. I’d like them to complete as quickly as possible but without seriously impacting other processes (such as web server responsiveness).
Currently I am running the script with
nice -n 19
and also have experimented with inserting very short usleep() calls, such as 50 microseconds in my main loop. Still this isn’t always yielding as quickly as I’d like on a single-core VM. BTW, I’m not saturating RAM so there’s no paging happening.
I’ve read that usleep() is a system call that will allow the scheduler to assign priority to other processes if needed more quickly than if I didn’t have any system calls.
What I’m wondering is if there’s a better method of doing this in PHP. Such as a call that doesn’t sleep but yields priority right away.
Also, I know other languages are more efficient than PHP, but this is part of a bigger app written in Symfony+Doctrine. I don’t want to split into multiple languages and lose the business logic benefits of the app’s models.
If
niceisn’t cutting it for you, consider picking a different scheduler for your kernel. This is a problem with your OS prioritizing processes. It isn’t something that you can readily solve inside your application (whether it’s written in PHP or any other language).Oh and remember,
nicelevels don’t really kick in unless you’re starving for CPU cycles. If your CPU is mostly idle, even a process with a nice level of 19 is allowed to eat all the CPU cycles it wants.Edit: In fact, make sure you’re CPU-bound before going down this path. If you’re I/O bound then CPU prioritisation isn’t going to have much of an effect.