Forum Moderators: coopster

Message Too Old, No Replies

a tightly scoped kind of timeout. possible?

         

httpwebwitch

7:56 pm on Jun 18, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I know I can set a max execution time on an entire script.

But can I set a max execution time on a block of code? say, a function that connects to an SMTP server?

I have a script here that attempts to connect to a sequence of SMTP servers and send a message. If one of them is down, the connection will timeout. I'm using Pear Mail::factory to do it... it has a "timeout" parameter, but evidently it's useless. I looked in the source code and that parameter isn't used anywhere.

So, I have some commands that might time out. They don't throw an error, they don't die, they just hang hang hang.

I wish there was a try/catch sort of solution for this. Like, "try" to do this, but "catch" if it takes longer than 5 seconds.

What can I do?

Readie

8:49 pm on Jun 18, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How about having this block of code in an include, and using

ini_set('max_execution_time', 120);


in the included file?

httpwebwitch

9:03 pm on Jun 18, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



@Readie that doesn't work - the ini_set applies to the entire script, not just the code in the include

I've found a workaround... I put the ornery code into a separate script hosted on the www, and called it with CURL. Then I put a timeout on the curl request; it returns a 200 status if it wins and 0 if it fails.

I wish there was a better way... my web server is groaning under the load

Readie

9:07 pm on Jun 18, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hmm, how about an ini_set before and after the code block then?

One to set the timeout, another to revert to default.