开发者

PHP Script Times out after 45 seconds

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.


I tried all the solutions on this page and, of course, running from the command line:

php -f filename.php

as Brent says is the sensible way round it.

But if you really want to run a script from your browser that keeps timing out after 45 seconds with a 500 internal server error (as I found when rebuilding my phpBB search index) then there's a good chance it's caused by mod_fcgid.

I have a Plesk VPS and I fixed it by editing the file

/etc/httpd/conf.d/fcgid.conf

Specifically, I changed

FcgidIOTimeout 45

to

FcgidIOTimeout 3600

3600 seconds = 1 hour. Should be long enough for most but adjust upwards if required. I saw one example quoting 7200 seconds in there.

Finally, restart Apache to make the new setting active.

apachectl graceful

HTH someone. It's been bugging me for 6 months now!

Cheers,

Rich


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.


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.


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


First of all

max_input_time and set_time_limit(0)

will only work with VPS or dedicated servers . Instead of that you can follow some rules to your implementation like below

  1. First read the whole CSV file .
  2. Then grab only 10 entries (rows) or less and make a ajax calls to import in DB
  3. Try to call ajax every time with 10 entries and after that echo out something on browser . In this method your script will never timeout .
  4. Follow the same method untill the CSV rows are finished .
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜