Is there a better way to yield CPU to other processes in PHP?
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 ef开发者_Python百科ficient 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 nice
isn'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, nice
levels 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.
In general Linux is very conservative. But you can try schedtool and libmlock. The latter is to lock php in ram to avoid swap. Also you can try swapiness because is mostly very high. You can also try to compile php yourself with aggressive cflags or even with the Intel C Compiler (I've a licence to sell). IMO schedtool is the same as nice but with some more variables.
"sleeping for 0 time" is generally the best that can be done to "yield control right away". Other methods generally require running in kernel-mode (and even kernel mode may be preempted by hardware interrupts, depending).
With a "0 sleep time" the process (or thread) will be scheduled to execute "again some point in the [near] future" -- the exact guarantees are system-specific. The minimum resolution actually slept depends upon environment and system configuration (as a general rule it won't be truly "0 time"). The exact time yielded is generally not guaranteed on non-realtime systems (e.g. Linux or Windows) -- the scheduler will do what it wants to do.
Happy coding.
Maybe you can rewrite parts of the application to use command-line tools (that don't need your application logic) outside PHP.
For instance, let's say you have an big XML file to import. Instead of doing everything in PHP, you can always try to convert your XML file into a CSV format using the CLI tool xsltproc
which would give you a file that would lighter for PHP to work with.
I know it's not the main purpose, but wouldn't the yield keyword with no arguments effectively cause the processor to yield to other processes? I always thought that's why it was called yield.
精彩评论