Hey there I am having some CPU spikes due to PHP script I run every 30 mins.
Script sends twits to signed up twitter users everyday and there are a lot of users.
So basically when PHP script sends out twits it causes a CPU spike.
I am asking for a direction on how should I handle this situation. Thanks a lot.
Usleep
Just a tiny little usleep will return the CPU to other available process(CPU scheduling).
Hog
Take this simple script for example:
This process consumes 20% of my CPU-time on average.
Schedule
This simple script only consumes 10% CPU-time on averqage.
Of-course this script does take a little longer, but the CPU is better scheduled. The longer you usleep the better the CPU can schedule.
usleep(1000)for example only used 2% CPU-time.I tested this on my Ubuntu Box
Message Queue
Also your operating system is very good at scheduling processes(of course that process needs to be friendly to your CPU) so I would advice you to use a message queue to speed up your work(sending tweets). For example Redis can also be used as a message queue or beanstalkd. Run a couple of worker processes which process work(sending out tweets). As a bonus you don’t incur the price of spawning processes which is relative expensive. On the web there is more than enough information available using message queue.