tags:

views:

126

answers:

3

Hello,

I am running a huge import to my database(about 200k records) and I'm having a serious issue with my import script timing out. I used my cell phone as a stop watch and found that it times out at exactly 45 seconds every pass(internal server error)... it only does about 200 records at a time, sometimes less. I scanned my phpinfo() and nothing is set to 45 seconds; so, I am clueless as to why it would be doing this.

My max_execution_time is set to 5 minutes and my max_input_time is set to 60 seconds. I also tried setting set_time_limit(0); ignore_user_abort(1); at the top of my page but it did not work.

It may also be helpful to note that my error file reads: "Premature end of script headers" as the execution error.

Any assistance is greatly appreciated.

A: 

It's quite possible that you are hitting an enforced resource limit on your server, especially if the server isn't fully under your control.

Assuming it's some type of Linux server, you can see your resource limits with ulimit -a on the command line. ulimit -t will also show you just the limits on cpu time.

If your cpu is limited, you might have to process your import in batches.

zombat
I don't think it is a resource limit... the script is timing out at exactly 45 seconds.... if it was memory type thing wouldn't the time be very inconsistent?
Dave C
You can set limits on other things than memory, and server hosts will often limit CPU time to prevent runaway processes. In such a case it would time out at pretty close to exactly the limit. Try `ulimit -t` and see what you get.
zombat
@Dave: There's also Apache's RLimitCPU: http://httpd.apache.org/docs/2.2/mod/core.html#rlimitcpu
Marc B
A: 

First, you should be running the script from the command line if it's going to take a while. At the very least your browser would timeout after 2 minutes if it receives no content.

php -f filename.php

But if you need to run it from the browser, try add header("Content-type: text/html") before the import kicks.

If you are on a shared host, then it's possible there are restrictions on the system when any long running queries and/or scripts are automatically killed after a certain length of time. These restrictions are generally loosened for non-web running scripts. Thus, running it from the command line would help.

Brent Baisley
A: 

The 45 seconds could be a coincidence -- it could be how long it takes for you to reach the memory limit.. increasing the memory limit would be like:

ini_set('memory_limit', '256M');

It could also be the actual db connection that is timing out.. what db server are you using? For me, mssql times out with an extremely unhelpful error, "Database context changed", after 60 seconds by default. To get around this, you do:

ini_set('mssql.timeout', 60 * 10); // 10 min
Stephen J. Fuhry