NotionCommotion Posted October 29, 2016 Share Posted October 29, 2016 Why doesn't the following script timeout and return a fatal error after 2 seconds? Thread Safety is disabled in PHP (is this what http://php.net/manual/en/ini.sect.safe-mode.php#ini.safe-mode is?) Also, instead of returning a fatal error, how can I do something else such as just die? I suppose I could use an error handler to do so, but expect there is a cleaner way. <?php echo("1\n"); set_time_limit(2); echo("2\n"); sleep(4); echo("3\n"); Quote Link to comment https://forums.phpfreaks.com/topic/302427-using-set_time_limit/ Share on other sites More sharing options...
Barand Posted October 29, 2016 Share Posted October 29, 2016 I guess you need to give it over 2 seconds of actual work to do Note: The set_time_limit() function and the configuration directive max_execution_time only affect the execution time of the script itself. Any time spent on activity that happens outside the execution of the script such as system calls using system(), stream operations, database queries, etc. is not included when determining the maximum time that the script has been running. This is not true on Windows where the measured time is real. Quote Link to comment https://forums.phpfreaks.com/topic/302427-using-set_time_limit/#findComment-1538719 Share on other sites More sharing options...
NotionCommotion Posted October 29, 2016 Author Share Posted October 29, 2016 I guess you need to give it over 2 seconds of actual work to do In reality, I have a blocking function, and not sleep. Any workarounds? Quote Link to comment https://forums.phpfreaks.com/topic/302427-using-set_time_limit/#findComment-1538722 Share on other sites More sharing options...
requinix Posted October 29, 2016 Share Posted October 29, 2016 Measure the time yourself. $start = time(); whatever loop { $remaining = $limit - (time() - $start); blocking function with timeout of $remaining if possible if function timed out or time() > $start + $limit then die } Quote Link to comment https://forums.phpfreaks.com/topic/302427-using-set_time_limit/#findComment-1538725 Share on other sites More sharing options...
NotionCommotion Posted October 29, 2016 Author Share Posted October 29, 2016 Thanks requinx, Originally, I was planning on making the blocking function indefinite, and just timeout the script, but that won't work as it will never get past it to check the time. Quote Link to comment https://forums.phpfreaks.com/topic/302427-using-set_time_limit/#findComment-1538735 Share on other sites More sharing options...
Solution requinix Posted October 29, 2016 Solution Share Posted October 29, 2016 Originally, I was planning on making the blocking function indefinite, and just timeout the scriptAnecdotally and without any sort of source to back me up, you shouldn't do an indefinite timeout on a blocking function. I think that's from personal experience. Do a "long" timeout of less than a minute, then worst case your loop just jumps back to the start and it starts blocking again immediately. Quote Link to comment https://forums.phpfreaks.com/topic/302427-using-set_time_limit/#findComment-1538737 Share on other sites More sharing options...
NotionCommotion Posted October 29, 2016 Author Share Posted October 29, 2016 Anecdotally ... Nice word! Quote Link to comment https://forums.phpfreaks.com/topic/302427-using-set_time_limit/#findComment-1538739 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.