What about a long runnig script (maybe run from cron) that has already started when the load starts to ramp up? Or if you want the script to still carry on running but giving up resources to higher priority threads?
This can be done on unix by starting scripts using `nice`. This will allow you to specify the thread priority to be below that of apache or any other services or scripts that are more important to the running of your site.
Manuel Lemos - 2010-11-11 23:28:35 - In reply to message 1 from Tom Davidson
nice just changes the priority by which the CPU is given to a process.
It does not prevent that a low priority process gets the CPU and still effectively slow down the rest of the processes.
For instance, lets say that you have a typical Web script process that needs to access a database to show some data in HTML.
If you have a low priority process running a CPU intensive task in the background, when the Web process accesses the database, it has to wait for the database server to respond. In that moment, the Web process goes idle and the background task gets the CPU.
When the Web process gets the response of the database, it does not get the CPU immediately. The OS scheduler will only gives the CPU to the Web process when the current multi-tasking time slice ends.
This would be different if you use the technique that I have described in the article. It consists in effectively stopping low priority background processes when the average CPU load is too high. This way, the scheduler will never split the CPU with the low priority process because it was intentionally put to sleep for a good period.
The point of asynchronous programming is mainly to run in parallel tasks that otherwise would be serialized, I mean if you have multiple long tasks to run in a script, you could save a lot of time by running them in parallel instead of running one task only after the previous has ended.
The gain can be more noticeable in applications that need to access external resources, such as databases or remote servers via network, as those are typically the ones that take more time and most of the time your script stays idle waiting for the database server or the remote host to respond.