1

I have an application written in PHP (Not website, an application), that has an infinite loop for its logic until the application shuts down. One issue with it that I've noticed is I get incredibly high CPU usage (Obviously), and I've tried using C++ methods I've done in the past to lower the CPU usage. Not many of them seem to work.

I've tried:

sleep(1);

and:

time_sleep_until(microtime(true)+0.2);

But neither seem to lower utilization.

Any tips?

5
  • 2
    with out seeing the script, no idea. Commented Jul 23, 2012 at 20:05
  • Is this some sort of cron process? Commented Jul 23, 2012 at 20:10
  • sleep(1); usually does sort this problem out, although it is a horrible way to do it. A more important point is what does your application actually do? It's best to let your script stick on a blocking function call until it returns data (e.g. a socket read operation) if possible, can you show your code (or at least an outline of it)? Commented Jul 23, 2012 at 20:17
  • PoX: Nope! Not a Cron process. Commented Jul 23, 2012 at 20:24
  • DaveRandom: My script is actually an IRC bot, that's a bit overblown... haha Commented Jul 23, 2012 at 20:24

2 Answers 2

1

You can configure execution of your jobs when they are needed to be executed. So if this infinite script needs to do/check something every minute you can just do it when there is a change otherwise sleep.

Your problem can be on something else your script does other than itself. Here is an example hope it helps.

    set_time_limit(0);
    ignore_user_abort(true);
    $minutes = NULL;

    do {
        try {
            // Run jobs, only when the minute has changed
            $now = getdate();
            if ($now['minutes'] != $minutes) {
                $minutes = $now['minutes'];
            }       
        }
        catch (Exception $e) {
            // Catch here and handle
        }
        sleep(10);
    } while (TRUE);
Sign up to request clarification or add additional context in comments.

9 Comments

YOU ARE AWESOME. This works 100% great! My process went from 100% CPU to an acceptable 1.9% CPU!! :D
how is this different than a a cron job run every minute ?
I don't think people would enjoy an IRC bot connecting and disconnecting every minute... plus it's pointless if the bot just connects and them immediately disconnects!
ok, next time explain what your doing when you ask the question.
@RyanCopley well thanks for you lack of consideration for any one else searching S.O for a similar issue.
|
0

As far as I know there is no way to do this within the PHP application, but when launching the process you can pass it to a command-line utility called cpulimit. Another option is to change the nice level of the program so at least when it is busy it won't hold up other more important processes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.