重新认识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秒的时候报错。
同理,数据库的相关操作也是如此。

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

672 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!

  44. Hi, I think your website might be having browser
    compatibility issues. When I look at your blog
    in Safari, it looks fine but when opening in Internet Explorer, it has some overlapping.

    I just wanted to give you a quick heads up! Other then that, wonderful blog!

  45. Just want to say your article is as astounding. The clearness for your submit is just excellent and i can suppose you are a professional in this subject.
    Well along with your permission let me to grasp your RSS feed to stay up
    to date with coming near near post. Thank you a million and please carry on the rewarding work.

  46. Hi there just wanted to give you a quick heads up. The text in your content seem
    to be running off the screen in Internet explorer.
    I’m not sure if this is a format issue or something to do with internet browser compatibility
    but I thought I’d post to let you know. The design look great though!
    Hope you get the issue resolved soon. Kudos

  47. Nice post. I learn something new and challenging on websites I stumbleupon everyday.
    It’s always useful to read through content from other
    authors and use a little something from other websites.

  48. Very good blog! Do you have any tips and hints for aspiring
    writers? I’m hoping to start my own blog soon but I’m
    a little lost on everything. Would you suggest starting with a free platform like WordPress or go for a paid option?
    There are so many choices out there that I’m completely confused ..
    Any suggestions? Thank you!

  49. I’d like to thank you for the efforts you’ve put in writing
    this website. I really hope to check out the same high-grade
    content by you in the future as well. In truth, your creative writing abilities has encouraged me to get my own website now 😉

  50. Thanks for the auspicious writeup. It in truth was a entertainment
    account it. Glance complicated to far introduced agreeable from you!
    By the way, how can we be in contact?

  51. Link exchange is nothing else but it is just placing the other person’s
    website link on your page at appropriate place and other
    person will also do similar in support of you.

  52. Hey I know this is off topic but I was wondering if you knew of any widgets I could add to my blog that automatically
    tweet my newest twitter updates. I’ve been looking for a plug-in like this
    for quite some time and was hoping maybe you would have some experience with something like this.

    Please let me know if you run into anything. I truly enjoy reading your blog and
    I look forward to your new updates.

  53. Thank you, I have just been looking for info about this topic for a while and yours is
    the best I have came upon till now. However, what about the bottom line?

    Are you certain about the source?

  54. When I originally commented I clicked the “Notify me when new comments are added”
    checkbox and now each time a comment is added I get four emails with the same comment.
    Is there any way you can remove people from that service?

    Thank you!

  55. I do not even know how I ended up here, but I thought this post was great.
    I do not know who you are but certainly you are going to a famous
    blogger if you are not already 😉 Cheers!

  56. What’s Going down i’m new to this, I stumbled upon this I’ve found It positively helpful and it has helped me out loads.
    I hope to give a contribution & help different customers like its aided me.
    Good job.

  57. I really like your blog.. very nice colors & theme. Did you
    create this website yourself or did you hire someone to do it
    for you? Plz answer back as I’m looking to create my own blog and would like to find out where
    u got this from. cheers

  58. Have you ever considered about including a little bit more than just your
    articles? I mean, what you say is valuable and all.
    However think of if you added some great pictures or video clips to give your
    posts more, “pop”! Your content is excellent but
    with images and clips, this website could definitely be one
    of the most beneficial in its niche. Awesome blog!

  59. It’s a shame you don’t have a donate button! I’d certainly donate to this superb blog!
    I guess for now i’ll settle for bookmarking and adding your RSS feed to my Google account.

    I look forward to new updates and will share this site with my Facebook group.

    Talk soon!

  60. Hmm is anyone else encountering problems with the pictures on this blog loading?
    I’m trying to determine if its a problem on my end or if it’s the blog.
    Any responses would be greatly appreciated.

  61. Hello! I could have sworn I’ve visited this website before but after browsing through many of the posts I realized it’s new to
    me. Regardless, I’m definitely happy I found it and I’ll be bookmarking
    it and checking back regularly!

  62. Heya i am for the first time here. I came across this board and I find It really helpful &
    it helped me out much. I’m hoping to give one thing back and aid others like you aided me.

  63. I’ve been exploring for a bit for any high quality articles
    or weblog posts on this sort of space . Exploring in Yahoo I
    ultimately stumbled upon this site. Studying this information So i’m glad to exhibit that I have a very just right uncanny feeling I came upon exactly what I needed.
    I so much unquestionably will make sure to don?t disregard this website and provides it a look on a
    relentless basis.

  64. Hello there, I discovered your site by the use of Google even as searching for a
    similar subject, your site came up, it seems to be great. I have
    bookmarked it in my google bookmarks.
    Hi there, just was alert to your weblog via Google, and
    located that it is really informative. I am going to watch out for brussels.
    I’ll appreciate in the event you proceed this in future.
    Lots of other folks will be benefited from your writing.

    Cheers!

  65. You could certainly see your skills in the article you write.
    The sector hopes for more passionate writers like you who aren’t afraid to mention how they believe.

    Always go after your heart.

  66. Hey there I am so excited I found your weblog, I really found you by mistake, while I was searching
    on Google for something else, Anyhow I am here now and would just like to
    say thanks for a incredible post and a all round interesting
    blog (I also love the theme/design), I don’t have time to
    look over it all at the moment but I have bookmarked it
    and also added your RSS feeds, so when I have time I will be back to read a lot more, Please do keep
    up the fantastic work.

  67. Thank you a lot for sharing this with all people you actually know what you
    are speaking about! Bookmarked. Please also discuss with my site =).
    We can have a link trade agreement between us

  68. I am extremely impressed with your writing skills as well as with
    the layout on your blog. Is this a paid theme or did you customize it yourself?

    Anyway keep up the excellent quality writing, it is rare to see a great
    blog like this one these days.

  69. Today, I went to the beachfront with my children. I found a sea shell and
    gave it to my 4 year old daughter and said “You can hear the ocean if you put this to your ear.” She placed the shell to her
    ear and screamed. There was a hermit crab inside and it pinched her ear.

    She never wants to go back! LoL I know this is completely
    off topic but I had to tell someone!

  70. Can I simply say what a relief to uncover someone who actually understands what they’re discussing on the internet. You actually understand how to bring a problem to light and make it important. More people need to check this out and understand this side of the story. It’s surprising you’re not more popular given that you most certainly have the gift.

    http:///www.manulescu.com – manu manumanu

  71. When I initially commented I clicked the “Notify me when new comments are added” checkbox and now each time a comment
    is added I get four e-mails with the same comment. Is there any way you can remove me from that service?
    Thanks a lot!

  72. Hi there I am so thrilled I found your weblog, I really found you by
    mistake, while I was browsing on Digg for something else, Regardless I am here now and would just like to say many thanks for a remarkable post
    and a all round exciting blog (I also love the theme/design), I don’t have time to browse it all at the minute but
    I have saved it and also included your RSS feeds, so when I have time I will be back
    to read a lot more, Please do keep up the superb work.

  73. I’ll immediately grab your rss as I can’t find your e-mail subscription link
    or e-newsletter service. Do you’ve any? Kindly allow
    me understand in order that I could subscribe.
    Thanks.

  74. Hiya very nice site!! Man .. Excellent .. Superb ..
    I’ll bookmark your website and take the feeds also?
    I’m glad to search out a lot of useful info
    right here in the post, we’d like work out more techniques in this
    regard, thanks for sharing. . . . . .

  75. Nice post. I used to be checking constantly this blog and I’m impressed!
    Extremely helpful info specifically the ultimate section 🙂 I take care of such
    info much. I used to be looking for this certain information for a long time.
    Thank you and good luck.

  76. Thanks for one’s marvelous posting! I actually enjoyed reading it,
    you could be a great author. I will make sure to bookmark
    your blog and definitely will come back down the
    road. I want to encourage you continue your great writing, have a nice day!

  77. When someone writes an paragraph he/she retains the plan of a user
    in his/her brain that how a user can be aware of
    it. Thus that’s why this article is great. Thanks!

  78. really nice

    This is really beneficial tidings my friend. you are a dialect right most luxurious scribbler . i dearth to portion with you my website as well. foresee me what do you believe about it

    buy youtube views

    Thankyou again for posting such good content. Cheers

  79. I used to be suggested this website via my cousin. I am not certain whether or not this post
    is written by him as nobody else know such detailed about
    my trouble. You are incredible! Thank you!

  80. After checking out a few of the articles on your website,
    I really like your technique of writing a blog. I book-marked it to my bookmark
    site list and will be checking back in the near future.

    Please visit my website as well and let me know what you think.

  81. I would like to thank you for the efforts you have put in penning this website.
    I’m hoping to see the same high-grade content from you
    in the future as well. In truth, your creative writing abilities has motivated me
    to get my own, personal website now 😉

  82. Hey there I am so thrilled I found your website,
    I really found you by error, while I was searching on Digg for something else, Regardless I
    am here now and would just like to say cheers for a tremendous post
    and a all round enjoyable blog (I also love the theme/design), I don’t have time to read
    through it all at the moment but I have book-marked it and also added
    in your RSS feeds, so when I have time I will be back to read much more,
    Please do keep up the superb work.

  83. This is a really riveting website I forced to say.

    Too bad that there are not too various websites like This anymore. But I am pleased that I found it. Meet scribble literary works on it .

    You can see my website here as luxuriously: xbody dristor

  84. It is perfect time to make some plans for the future and
    it’s time to be happy. I’ve read this post and if I
    could I want to suggest you some interesting things or
    tips. Maybe you could write next articles referring to this article.
    I wish to read more things about it!

  85. I appreciate, result in I discovered exactly what I
    used to be taking a look for. You’ve ended my four day long hunt!
    God Bless you man. Have a great day. Bye

  86. Hmm it seems like your blog ate my first comment (it was extremely long) so
    I guess I’ll just sum it up what I submitted and say, I’m thoroughly enjoying your blog.
    I as well am an aspiring blog blogger but I’m
    still new to the whole thing. Do you have any recommendations for newbie blog writers?
    I’d genuinely appreciate it.

  87. Hello, this weekend is nice designed for me, for the reason that this occasion i am reading this
    wonderful informative piece of writing here at my residence.

  88. Hi there everybody, here every one is sharing these kinds of knowledge, thus it’s fastidious to read this weblog, and I used to
    go to see this website every day.

  89. Can I just say what a relief to discover somebody
    who really understands what they are discussing online. You definitely
    know how to bring a problem to light and make it important.
    More and more people should check this out and understand this side of your story.
    I was surprised you are not more popular because you definitely have the gift.

  90. Heya just wanted to give you a brief heads up and let you know a few
    of the pictures aren’t loading properly. I’m not sure why but I
    think its a linking issue. I’ve tried it in two different internet browsers and both show the same results.

  91. Hi there to every one, the contents present at this web
    page are in fact amazing for people experience, well,
    keep up the nice work fellows.

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

  93. When I originally commented I clicked the “Notify me when new comments are added” checkbox and
    now each time a comment is added I get four e-mails with the
    same comment. Is there any way you can remove people from
    that service? Bless you!

  94. Hey would you mind letting me know which hosting company you’re using?

    I’ve loaded your blog in 3 different internet browsers and I must say this blog loads a lot faster then most.

    Can you recommend a good hosting provider at a honest price?
    Cheers, I appreciate it!

  95. Have you ever considered about including a little bit more than just your articles?

    I mean, what you say is fundamental and all. However think of
    if you added some great visuals or videos to give your posts more,
    “pop”! Your content is excellent but with images and clips, this blog could undeniably be one of
    the most beneficial in its field. Fantastic blog!

  96. Right here is the perfect web site for anyone who hopes to understand this topic.

    You know so much its almost tough to argue with you (not that I personally will need to…HaHa).

    You definitely put a new spin on a subject that’s been discussed
    for decades. Great stuff, just great!

  97. Great goods from you, man. I’ve take into account your stuff previous to and you
    are just extremely excellent. I really like
    what you have obtained right here, really like what you’re
    stating and the way in which by which you assert
    it. You’re making it entertaining and you still take care of to stay it sensible.

    I can’t wait to read far more from you. This is actually a tremendous
    site.

  98. Hi my loved one! I wish to say that this article is
    awesome, great written and come with almost all significant infos.
    I’d like to see more posts like this .

发表评论

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