0

I have a few foreach loops and at the end of every I use

set_time_limit(30) 

which restarts the counter back to zero.

Now the script runs for longer time (I fetch 5000-10000 articles using an API and store them in DB), but after a while (when it processes already lots of data) I get the "Maximum execution time of 30 seconds exceeded".

Could it be because of lack of memory? How could I tackle this problem?

The script is fetching articles using an API, and a foreach loop is used basically like this

foreach($articles as $article) { //do stuff with single article using $article set_time_limit(30); } 

and I do not expect it that it needs more than 30 seconds to fetch and process a single article, but apparently after the script is run for some time, it hits that limit. What am I doing wrong? I don't want to allow the script to do a 5-secs job longer than a maximum of 30 seconds by using set_time_limit(9000) or something like that, which will probably get the job done but I suppose this is not a good way to solve the issue?

3
  • 1
    set_time_limit() accounts for the entire PHP page, not just your loop. It should not be in a loop. Commented Oct 25, 2013 at 1:38
  • Are you sure the query is actually getting to results before your set_time_limit(30) applies? If the query takes longer than 30 seconds to get to the first results, your code won't add any time to the total execution. Commented Oct 25, 2013 at 1:39
  • Yes, I'm sure, as at the end of foreach loops (but just before the set_time_limit(30) ) there is a mysql query which inserts data into the database, and I manually can refresh the table in database and see the data being inserted every few seconds. Commented Oct 25, 2013 at 1:41

3 Answers 3

3

Why don't you just make

set_time_limit(0); 

the first line of your script?

Sign up to request clarification or add additional context in comments.

6 Comments

That's funny. I just spit on my screen.
But is this the way to go? Foreach loop obviously takes every time a little bit longer to complete than previous.
Yeah, I suppose the 30 seconds default is fine. What should I rather do with the foreach loop obviously taking more and more time with each iteration?
@PHPglue, according to the docs, it means no limit will be imposed. Am I missing something?
It's worth linking the docs and quoting the relevant bit: "The maximum execution time, in seconds. If set to zero, no time limit is imposed."
|
1

If, for some crazy reason you feel that you must impose a time limit, consider the following:

set_time_limit(0); $n = 0; $inc = 1; foreach($article as $v){ // do stuff with $v - Less characters is faster $n+=$inc; } set_time_limit($n); 

Change $inc to suit your needs.

1 Comment

Solved, with set_time_limit(0) at the top.
0

Try this:

PHP has a function set_time_limit which sets the time limit of execution. It has one argument set in seconds. If you use like this, set_time_limit(0) then it will set unlimited time of execution. Write this code in top. 

You can refer more from here: http://php.net/manual/en/function.set-time-limit.php

  • Thanks

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.