重新认识PHP中的set_time_limit

想必大家都已经知道set_time_limit函数的作用,即设置脚本最大执行时间。但是这个函数还是有一些细节有些人可能没太注意。

首先先看一下set_time_limit函数的解释。设置允许脚本运行的时间,单位为秒。如果超过了此设置,脚本返回一个致命的错误。默认值为30秒,或者是在php.ini的max_execution_time被定义的值,如果此值存在。当此函数被调用时,set_time_limit()会从零开始重新启动超时计数器。换句话说,如果超时默认是30秒,在脚本运行了了25秒时调用 set_time_limit(20),那么,脚本在超时之前可运行总时间为45秒。

还有一个注意的地方是set_time_limit()函数和配置指令max_execution_time只影响脚本本身执行的时间。任何发生在诸如使用system()的系统调用,流操作,数据库操作等的脚本执行的最大时间不包括其中。同时sleep函数的时间也不包括在其中。

也就是说set_time_limit()函数只针对的是PHP代码本身的执行时间。

例子1:

set_time_limit(5);
sleep(10);
echo "test";

以上代码在10秒后正常的输出test,并且不会报错。
换成下面的代码试试

set_time_limit(5);
for(;;){
   //do something  
}

以上代码执行5秒后会报Fatal error: Maximum execution time of 5 seconds exceeded的错误。

例子2:

set_time_limit(5);
//假设curl需要10秒才能返回数据
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, "http://example.com"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
$output = curl_exec($ch);  
curl_close($ch);  
var_dump($output);

以上代码在10秒后正常的打印出curl请求返回的结果,并不会在5秒的时候报错。
同理,数据库的相关操作也是如此。

所以,看似一个简单的函数调用,有些细节上的东西还是很有必要了解的。不然坑的可就是自己了?。

111 Responses

  1. Do you have a spam problem on this blog; I also am a blogger, and I was wondering your situation;
    we have created some nice procedures and we are looking to swap
    methods with others, please shoot me an email if interested.

  2. obviously like your web site however you have to check the
    spelling on quite a few of your posts. Several of
    them are rife with spelling issues and I to find it very troublesome to tell the reality however I’ll
    surely come back again.

  3. Heya i am for the primary time here. I came
    across this board and I to find It truly helpful & it helped me
    out a lot. I hope to present one thing back and aid
    others such as you aided me.

  4. Hi there everybody, here every person is sharing these familiarity,
    therefore it’s fastidious to read this web site, and I used to pay a quick visit this blog every day.

  5. Definitely consider that which you stated. Your favourite justification seemed to be on the net the easiest
    factor to keep in mind of. I say to you, I certainly get irked while folks think about worries that they plainly don’t recognize about.
    You controlled to hit the nail upon the highest as neatly as defined
    out the entire thing without having side-effects , people could take a signal.
    Will probably be again to get more. Thank you

  6. Do you have a spam problem on this site; I also am a blogger,
    and I was wanting to know your situation; many of
    us have created some nice procedures and we are looking to trade solutions with other folks, why not shoot me an email if interested.

  7. Pretty nice post. I just stumbled upon your blog and wanted to say that I have truly enjoyed surfing around your blog
    posts. In any case I will be subscribing to your feed and I hope you write again very soon!

  8. Wow, awesome weblog layout! How lengthy have you ever been running a blog for?
    you make blogging glance easy. The total look of
    your site is wonderful, let alone the content material!

  9. I have read some excellent stuff here. Certainly price
    bookmarking for revisiting. I wonder how a lot attempt you place to
    create one of these fantastic informative site.

  10. Great blog here! Also your web site loads up very
    fast! What web host are you using? Can I get your affiliate
    link to your host? I wish my site loaded up as quickly as yours lol

  11. Fantastic beat ! I would like to apprentice while you amend your web site, how could i subscribe for a blog web site?
    The account aided me a acceptable deal. I had been a little
    bit acquainted of this your broadcast offered bright clear idea

  12. You really make it seem so easy together with your presentation but I find this
    topic to be actually one thing which I feel I might never understand.
    It kind of feels too complex and extremely
    vast for me. I am taking a look ahead on your subsequent submit,
    I’ll attempt to get the grasp of it!

  13. Please let me know if you’re looking for a author for your weblog.
    You have some really good posts and I think I would be a good asset.
    If you ever want to take some of the load off, I’d absolutely love to write some
    content for your blog in exchange for a link back to
    mine. Please shoot me an e-mail if interested. Thanks!

  14. Greate pieces. Keep writing such kind of information on your site.

    Im really impressed by your site.
    Hey there, You’ve done a fantastic job. I’ll certainly digg it and individually recommend to my friends.
    I’m confident they will be benefited from this
    site.

  15. Good day! I know this is kinda off topic but I’d figured I’d ask.
    Would you be interested in exchanging links or maybe guest writing a blog post or
    vice-versa? My blog goes over a lot of the same topics as yours
    and I think we could greatly benefit from each other.
    If you are interested feel free to shoot me an e-mail.
    I look forward to hearing from you! Great blog by the way!

  16. Great post. I was checking continuously this weblog and I’m impressed!
    Extremely helpful info specially the ultimate phase
    🙂 I handle such info much. I used to be looking for this certain info for a long time.
    Thank you and good luck.

  17. Cool blog! Is your theme custom made or did you download it from somewhere?
    A design like yours with a few simple tweeks would really
    make my blog shine. Please let me know where you got your design.
    Thanks a lot

  18. Hello! I know this is kind of off-topic however I had to ask.
    Does managing a well-established website like yours take a massive amount work?

    I’m completely new to writing a blog however I do write
    in my journal everyday. I’d like to start a blog so I will be able
    to share my experience and feelings online. Please let
    me know if you have any recommendations or tips for new aspiring bloggers.
    Thankyou!

  19. Hello there! This blog post could not be written much better!

    Reading through this article reminds me of my previous roommate!
    He continually kept talking about this. I most certainly will
    send this post to him. Pretty sure he’s going to have a very good read.

    Thank you for sharing!

  20. May I simply say what a comfort to find someone who truly understands what they’re
    discussing on the internet. You certainly know how to bring a problem to light and make
    it important. More and more people really need to look at this and understand this
    side of the story. I was surprised you’re not more popular given that you certainly
    possess the gift.

  21. After checking out a number of the blog posts on your web site, I truly like your technique of writing a blog.
    I bookmarked it to my bookmark webpage list and will be checking back
    soon. Please check out my website too and tell me your
    opinion.

  22. Have you ever thought about publishing an e-book or guest authoring on other websites?
    I have a blog based upon on the same information you discuss and would really like to have
    you share some stories/information. I know my audience would
    enjoy your work. If you are even remotely interested, feel free to
    send me an e-mail.

  23. Hello! This is my first visit to your blog! We are a team of volunteers and starting a
    new project in a community in the same niche. Your
    blog provided us valuable information to work on. You have done a wonderful job!

  24. I like the helpful info you provide in your articles.

    I’ll bookmark your weblog and check again here frequently.

    I’m quite sure I will learn lots of new stuff right here!
    Good luck for the next!

  25. I know this if off topic but I’m looking into starting
    my own blog and was curious what all is required to get set up?
    I’m assuming having a blog like yours would cost a pretty penny?
    I’m not very internet smart so I’m not 100% sure.
    Any suggestions or advice would be greatly appreciated.
    Thank you

  26. My partner and I absolutely love your blog and find most of
    your post’s to be just what I’m looking for.

    Would you offer guest writers to write content to suit
    your needs? I wouldn’t mind producing a post or elaborating on a lot of the
    subjects you write regarding here. Again, awesome blog!

  27. I think this is among the so much important info for me.

    And i am glad reading your article. But should observation on some normal issues, The web site taste is great, the articles is really
    excellent : D. Just right job, cheers

  28. Wow that was strange. I just wrote an extremely long comment but after
    I clicked submit my comment didn’t show up.

    Grrrr… well I’m not writing all that over again. Anyway, just wanted to say great blog!

  29. I don’t even understand how I ended up right here, but I thought this submit used to be great.
    I do not recognise who you are however definitely you are going to a well-known blogger in the event you aren’t already.
    Cheers!

  30. This is very interesting, You are a very skilled blogger.
    I have joined your feed and look forward to seeking more
    of your excellent post. Also, I have shared your website in my social networks!

  31. I’ve been surfing online more than 4 hours today, yet I never found any
    interesting article like yours. It is pretty worth enough for me.

    Personally, if all site owners and bloggers made good content as you did, the internet will be a lot more useful than ever
    before.

  32. Nice post. I was checking continuously this
    weblog and I’m inspired! Very helpful information specifically the
    last phase 🙂 I deal with such information much.
    I used to be seeking this particular info for a long time.
    Thanks and good luck.

  33. What’s up i am kavin, its my first occasion to commenting anyplace,
    when i read this piece of writing i thought i could
    also make comment due to this sensible article.

  34. A person necessarily help to make significantly posts I would state.

    That is the first time I frequented your website page and up to now?

    I surprised with the research you made to create this actual publish amazing.
    Wonderful activity!

  35. You actually make it seem so easy with your presentation but
    I find this matter to be actually something which I think I
    would never understand. It seems too complex and very broad for me.
    I am looking forward for your next post, I will try to get the hang of it!

  36. Hey there! Someone in my Facebook group shared this website with us so
    I came to check it out. I’m definitely enjoying the information. I’m
    bookmarking and will be tweeting this to my followers!
    Wonderful blog and fantastic design.

  37. Thank you for sharing your thoughts. I truly appreciate your efforts
    and I am waiting for your further post thank you once again. plenty of fish natalielise

  38. I have read so many articles about the blogger
    lovers except this post is in fact a fastidious piece of
    writing, keep it up. natalielise plenty of fish

  39. Great weblog here! Additionally your site loads up very fast!
    What web host are you using? Can I am getting your affiliate
    hyperlink on your host? I wish my website loaded
    up as fast as yours lol

  40. I read this paragraph fully on the topic of the comparison of
    newest and preceding technologies, it’s amazing article.

  41. each time i used to read smaller articles or reviews that
    also clear their motive, and that is also happening with this post which I am
    reading at this place.

  42. Nice post. I was checking continuously this blog and I’m impressed!

    Very useful information specifically the last part 🙂 I care for such
    information a lot. I was looking for this certain information for a very long time.
    Thank you and good luck.

  43. I like the valuable information you provide in your articles.
    I’ll bookmark your blog and check again here regularly.
    I am quite sure I will learn many new stuff right here!
    Best of luck for the next!

发表评论

电子邮件地址不会被公开。 必填项已用*标注