views:

493

answers:

5

I have a php script that checks for updates on many (thousands) of sites. On occasion (more frequently as the number of sites increases), I get an execution timeout on the update of one of these sites, and the whole script goes down the drain.

The best idea I could come up with is to fork each update, so if it dies, the overall update just continues. From what I gathered, it seems PHP threading isn't something to rely on, especially on a windows platform (which I unfortunately have to work on).

I thought about using curl as a forking mechanism, but won't it be much more costly for the server (in time and processing power)?

I'd be happy to hear some ideas and experience about PHP forking/threading...

thanks, Omer.

+1  A: 

You could set the ini directive max_execution_time to 0. This should remove the max execution time, and allow the script to run without incurring this error. This value has to be set in your php.ini file however - using ini_set doesn't work.

Ross
You can set it to 0 with set_time_limit. ini_set of max_execution_time should work in script if you dont use safe mode.
OIS
yeah, but then what if something actually gets stuck?will the script stall forever?
Omer
Not quite sure - you could check the return HTTP status codes using cURL to check what's happening. Crashes (as in fatal errors) still occur as normal however.
Ross
+2  A: 

If you're going to use cURL look into the multi* family of methods which allows you to streamline cURL and interacting with a set of sites. That being said, you could also set your max execution time (via ini_set) to some arbitrarily high number and/or keep your own watchdog counter so that you never let it get too high.

But yeah, ultimately are going to run into issues and the lack of full thread support in PHP. So your best bet is to look at other alternatives, e.g. other languages/platforms that provide native threading support.

Cody Caughlan
A: 
set_time_limit(0);

set_time_limit docs

Karsten
+2  A: 

I managed to get some form of threading in php using pcntl extension.It was not the best of solutions but it did the trick.

http://www.php.net/manual/en/ref.pcntl.php

try the following links also, the gave me a idea how to go about the implementation.

http://www.van-steenbeek.net/?q=php_pcntl_fork

http://www.hudzilla.org/phpbook/read.php/16_1_3

http://www.electrictoolbox.com/article/php/process-forking/

I hope this helps , but php is not very good with threading though.

Ronald Conco
did you do pcntl on windows? seems to be more troublesome.
Omer
A: 

Well, at the end I went for curl, and it works just fine.

I needed a cross-platform solution, as I develop on a Mac while in this case the production serer is Winodws. That meant pcntl was out of the question.

I was worried that sending 1000s of curl requests to my own server might hog it and bother users on the site at the time, but I was mistaken.

I did, however, have to add a set_time_limit(0) to the script that initiates all the curl calls, otherwise it just get's a timeout.

thanks for all the help.

Omer