長時間の仕事をしたいのですが、最大実行時間を変更できないので、解決策を講じます。スクリプトがシャットダウンしようとすると、スクリプトは自分自身に http 要求を送信するため、スクリプトは引き続き実行できます。 これは私のテストスクリプト t.php です
<?php
define('DS', DIRECTORY_SEPARATOR);
define('ROOT', dirname(__FILE__));
function post_request_async($url, ARRAY $data) {
// Convert the data array into URL Parameters like a=b&foo=bar etc.
$data = http_build_query($data);
// parse the given URL
$url = parse_url($url);
$host = $url['host'];
$path = $url['path'];
$port = isset($url['port']) ? $url['port'] : "80";
$fp = fsockopen($host, $port, $errno, $errstr, 5);
if ($fp){
$req = "";
$req.="POST $path HTTP/1.0\r\n";
$req.="Host: $host\r\n";
$req.="Content-type: application/x-www-form-urlencoded\r\n";
$req.="Content-length: ". strlen($data) ."\r\n";
$req.="Connection: close\r\n\r\n";
$req.= $data;
fputs($fp, $req);
fclose($fp);
}
}
set_time_limit(10);
register_shutdown_function("shutdown_func");
file_put_contents(ROOT.DS."zz.txt", date("Y-m-d H:i:s")." start -->".PHP_EOL, FILE_APPEND);
function shutdown_func(){
file_put_contents(ROOT.DS."zz.txt", date("Y-m-d H:i:s")." shutdown_func -->".PHP_EOL, FILE_APPEND);
post_request_async("http://127.0.0.1/t.php", array());
exit();
}
for(;;) {
sleep(2);
}
?>
しかし、結果は奇妙です: zz.txt
2012-05-16 10:11:42 start -->
2012-05-16 10:11:54 shutdown_func -->
2012-05-16 10:11:54 start -->
2012-05-16 10:12:04 shutdown_func -->
2012-05-16 10:12:04 start -->
2012-05-16 10:12:14 shutdown_func -->
2012-05-16 10:12:14 start -->
2012-05-16 10:12:26 shutdown_func -->
2012-05-16 10:12:26 start -->
2012-05-16 10:12:38 shutdown_func -->
2012-05-16 10:12:38 start -->
2012-05-16 10:12:48 shutdown_func -->
2012-05-16 10:12:48 start -->
2012-05-16 10:12:58 shutdown_func -->
2012-05-16 10:12:58 start -->
2012-05-16 10:13:08 shutdown_func -->
2012-05-16 10:13:08 start -->
2012-05-16 10:13:18 shutdown_func -->
2012-05-16 10:13:18 start -->
2012-05-16 10:13:28 shutdown_func -->
2012-05-16 10:13:28 start -->
2012-05-16 10:13:40 shutdown_func -->
2012-05-16 10:13:40 start -->
2012-05-16 10:13:50 shutdown_func -->
2012-05-16 10:13:50 start -->
2012-05-16 10:14:02 shutdown_func -->
2012-05-16 10:14:02 start -->
2012-05-16 10:14:14 shutdown_func -->
2012-05-16 10:14:14 start -->
2012-05-16 10:14:26 shutdown_func -->
2012-05-16 10:14:26 start -->
2012-05-16 10:14:36 shutdown_func -->
2012-05-16 10:14:36 start -->
2012-05-16 10:14:48 shutdown_func -->
2012-05-16 10:14:48 start -->
2012-05-16 10:14:58 shutdown_func -->
2012-05-16 10:14:58 start -->
2012-05-16 10:15:08 shutdown_func -->
2012-05-16 10:15:08 start -->
2012-05-16 10:15:18 shutdown_func -->
2012-05-16 10:15:18 start -->
php_errors.log
[16-May-2012 02:11:54 UTC] PHP Fatal error: Maximum execution time of 10 seconds exceeded in E:\eclipse_php_workspace\caijiche_new2\t.php on line 41
[16-May-2012 02:12:04 UTC] PHP Fatal error: Maximum execution time of 10 seconds exceeded in E:\eclipse_php_workspace\caijiche_new2\t.php on line 41
[16-May-2012 02:12:14 UTC] PHP Fatal error: Maximum execution time of 10 seconds exceeded in E:\eclipse_php_workspace\caijiche_new2\t.php on line 41
[16-May-2012 02:12:26 UTC] PHP Fatal error: Maximum execution time of 10 seconds exceeded in E:\eclipse_php_workspace\caijiche_new2\t.php on line 41
[16-May-2012 02:12:38 UTC] PHP Fatal error: Maximum execution time of 10 seconds exceeded in E:\eclipse_php_workspace\caijiche_new2\t.php on line 41
[16-May-2012 02:12:48 UTC] PHP Fatal error: Maximum execution time of 10 seconds exceeded in E:\eclipse_php_workspace\caijiche_new2\t.php on line 41
[16-May-2012 02:12:58 UTC] PHP Fatal error: Maximum execution time of 10 seconds exceeded in E:\eclipse_php_workspace\caijiche_new2\t.php on line 41
[16-May-2012 02:13:08 UTC] PHP Fatal error: Maximum execution time of 10 seconds exceeded in E:\eclipse_php_workspace\caijiche_new2\t.php on line 41
[16-May-2012 02:13:18 UTC] PHP Fatal error: Maximum execution time of 10 seconds exceeded in E:\eclipse_php_workspace\caijiche_new2\t.php on line 41
[16-May-2012 02:13:28 UTC] PHP Fatal error: Maximum execution time of 10 seconds exceeded in E:\eclipse_php_workspace\caijiche_new2\t.php on line 41
[16-May-2012 02:13:40 UTC] PHP Fatal error: Maximum execution time of 10 seconds exceeded in E:\eclipse_php_workspace\caijiche_new2\t.php on line 41
[16-May-2012 02:13:50 UTC] PHP Fatal error: Maximum execution time of 10 seconds exceeded in E:\eclipse_php_workspace\caijiche_new2\t.php on line 41
[16-May-2012 02:14:02 UTC] PHP Fatal error: Maximum execution time of 10 seconds exceeded in E:\eclipse_php_workspace\caijiche_new2\t.php on line 41
[16-May-2012 02:14:14 UTC] PHP Fatal error: Maximum execution time of 10 seconds exceeded in E:\eclipse_php_workspace\caijiche_new2\t.php on line 41
[16-May-2012 02:14:26 UTC] PHP Fatal error: Maximum execution time of 10 seconds exceeded in E:\eclipse_php_workspace\caijiche_new2\t.php on line 41
[16-May-2012 02:14:36 UTC] PHP Fatal error: Maximum execution time of 10 seconds exceeded in E:\eclipse_php_workspace\caijiche_new2\t.php on line 41
[16-May-2012 02:14:48 UTC] PHP Fatal error: Maximum execution time of 10 seconds exceeded in E:\eclipse_php_workspace\caijiche_new2\t.php on line 41
[16-May-2012 02:14:58 UTC] PHP Fatal error: Maximum execution time of 10 seconds exceeded in E:\eclipse_php_workspace\caijiche_new2\t.php on line 41
[16-May-2012 02:15:08 UTC] PHP Fatal error: Maximum execution time of 10 seconds exceeded in E:\eclipse_php_workspace\caijiche_new2\t.php on line 41
[16-May-2012 02:15:18 UTC] PHP Fatal error: Maximum execution time of 10 seconds exceeded in E:\eclipse_php_workspace\caijiche_new2\t.php on line 41
[16-May-2012 02:15:28 UTC] PHP Fatal error: Maximum execution time of 10 seconds exceeded in E:\eclipse_php_workspace\caijiche_new2\t.php on line 41
[16-May-2012 02:15:28 UTC] PHP Fatal error: Maximum execution time of 10 seconds exceeded in E:\eclipse_php_workspace\caijiche_new2\t.php on line 33
シャットダウン関数が約 22 回呼び出され、突然呼び出されなかったようです。
私のPHPバージョンは5.3.9で、サーバーはIIS 7.5です 本当に助けが必要です! どうもありがとう!
-----------------------更新1------------------------- -------------------
掘り下げた後、phpソースにテストスクリプトが見つかりました。登録されたシャットダウン機能がタイムアウトする可能性があるようです。
<?php
set_time_limit(1);
register_shutdown_function("plop");
function plop() {
$ts = time();
while(true) {
if ((time()-$ts) > 2) {
echo "Failed!";
break;
}
}
}
plop();
?>
結果は次のとおりです。
Fatal error: Maximum execution time of 1 second exceeded in E:\eclipse_php_workspace\caijiche_new2\t.php on line 9
Fatal error: Maximum execution time of 1 second exceeded in E:\eclipse_php_workspace\caijiche_new2\t.php on line 9