homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

How do I stop my PHP script from eating the processor?

 1:51 pm on Mar 9, 2009 (gmt 0)

I have a heavy script that needs to loop 1000s of times analysing on-page SEO.

The trouble that it takes over the processor and prevents any other PHP script from running at the same time.

Is there a way to reduce the process priority from within PHP to give the other processes a chance.

I hoping to do this on Windows and Linux (a shared host!).

I know about sys_getloadavg(), it wont help me on a shared host and doesn't work on windows.

I know I could introduce a sleep, but maybe there's a better way.




 3:30 pm on Mar 9, 2009 (gmt 0)

@ jezzer,

As far as i know you cant give priority to processes, for the simple reason that php can only do 1 process (single-threaded).

Maybe you should optimize your script? cause 1000s of times sounds kinda 'impractical'



 4:08 pm on Mar 9, 2009 (gmt 0)


The 1000s of loops are necessary to analyse and pick-out all the top phrases on a page - for seo.

I know PHP isn't the best language for this, but it fits in with the rest of my website which came first.


 4:25 pm on Mar 9, 2009 (gmt 0)

Well, you could try forking the process. Part of the problem in a large or lengthy process is it has to maintain a connection with the browser.

So if you could initiate the process, then return an immediate response to the browser, the child process would continue in the background. You'd need another tool to check the progress of the child. $pid=process ID.

## First you submit and return an immediate
## response. Do not exit after the response.
&html('Checking the data');
## This is the parent, which just marks the progress
if ($pid = fork) {
## This is the child. You want to close STDOUT so the
## call to your HTML sub above doesn't "hang."
else {
close (STDOUT);
## So the parent doesn't kill the child

The previous is a perl approach (close(),waitpid()) but it would be very easy to emulate this in PHP.


 5:57 pm on Mar 9, 2009 (gmt 0)


I actually attempt to avoid hanging the browser by the parent PHP script using CURL to start the more busy child PHP script (which is set not to timeout and ignore_user_abort - client disconnection).

This works, but the child then takes all the processor and prevents the other sessions from doing anything.

It's looking like I can't force a lower priority, but maybe I will experiment with some micro sleeps etc.

In general I don't think I can run this script on a shared server, I might upset someone!

FYI: All current testing is on windows.


 6:18 pm on Mar 9, 2009 (gmt 0)

Can you call sleep(1) on a regular basis? sleep() is designed to relinquish control to the OS.


 6:30 pm on Mar 9, 2009 (gmt 0)

Yes, thanks.

I mentioned this in the 1st post.

When the project is complete it will also run in the middle of the night, when it's quiet.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved