In principle, on Unixish system (Linux, BSD, etc.), you could use the proc_nice()
function to change the process priority, like this:
proc_nice( 20 ); // now this process has very low priority
However, there are a couple of major caveats here that make it less useful in practice:
- Until PHP 7.2.0 it wasn't supported on Windows at all.
- You're only allowed to increase the niceness, not to decrease it (not even back to its original value).
- The niceness persists until the PHP process exits, which could be a problem if you're running PHP as a FastCGI process, or, even worse, as a webserver extension.
- Because of the above issues,
proc_nice()
might be disabled for security reasons even on systems that could technically support it.
What you could try to do, if your webhost allows it, is to start a background process for the long-running task, so that your webserver can get back to serving requests while it's running. You can even use the nice
shell command to lower the priority of the background process, like this:
exec( "nice nohup php -f slow_script.php < /dev/null > output.txt 2>&1 &" );
Once the slow script has finished, you can get its output by downloading output.txt
.