PHP code example of morjodrom / time-limiter
1. Go to this page and download the library: Download morjodrom/time-limiter library. Choose the download type require. 2. Extract the ZIP file and open the index.php. 3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
morjodrom / time-limiter example snippets
// maximum time to execute one request
$REQUEST_TIMEOUT_SEC = 5;
$curlClient = new SomeCurlClient(); // just an example.
// shared hosting often has a limit. E.g. 30 seconds
$maxExecutionTime = ini_get('max_execution_time');
$timeLimiter = new \timelimiter\TimeLimiter($maxExecutionTime, $REQUEST_TIMEOUT_SEC);
// check if there is time left to prevent 504 timeout
// recommended
foreach($timeLimiter => $timeLeft){
$result = $curlClient->doSomeHeavyJob([
'timeout' => $REQUEST_TIMEOUT_SEC
]);
// handle the result
// ...
}
// or alternatively while loop might be used with respective Iterator calls.
while($timeLimiter->valid()){
$result = $curlClient->doSomeHeavyJob([
'timeout' => $REQUEST_TIMEOUT_SEC
]);
$timeLimiter->next(); // must be called to adapt to long iterations
}