note 126129 added to parallel-runtime.close
| From: | andre at webtechnik dot ch | Date: | Tue, 01 Jun 2021 06:45:19 +0000 |
| Subject: | note 126129 added to parallel-runtime.close | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-216459@lists.php.net to get a copy of this message | ||
This is a simple test to show how close and kill works.
<?php
/**
* Test close and kill
* - main thread writes to main.log
* - second thread writes to thread.log and shutdown.log
* - main thread runs 10 seconds, second thread runs 20 seconds
* - close will wait until second thread stops
* - kill don't waits and kill immediately the second thread
*/
// main thread
logToFile("start");
// create a second thread
$thread = new parallel\Runtime();
$thread->run(function(){
// what to do if thread shuts down
register_shutdown_function (
function(){file_put_contents("shutdown.log",date('Y-m-d H:i:s') . "\t last
will");} );
// loop and count for 20 seconds
$c=0;
while ($c++ < 20){
sleep(1);
echo $c."|";
file_put_contents("thread.log",date('Y-m-d H:i:s') . "\t" .
$c.PHP_EOL, FILE_APPEND);
}
});
// sleep 10 seconds
sleep(10);
// mainthread kills thread immediately
//$thread->kill();
// mainthread waits on thread
$thread->close();
logToFile("end");
function logToFile($msg){
echo PHP_EOL.$msg.PHP_EOL;
file_put_contents("main.log",date('Y-m-d H:i:s') . "\t" .
$msg.PHP_EOL, FILE_APPEND);
}
?>
----
Server IP: 45.112.84.4
Probable Submitter: 45.112.85.242 (proxied: 31.171.147.162)
----
Manual Page -- https://php.net/manual/en/parallel-runtime.close.php
Edit -- https://main.php.net/note/edit/126129
Del: integrated -- https://main.php.net/note/delete/126129/integrated
Del: useless -- https://main.php.net/note/delete/126129/useless
Del: bad code -- https://main.php.net/note/delete/126129/bad+code
Del: spam -- https://main.php.net/note/delete/126129/spam
Del: non-english -- https://main.php.net/note/delete/126129/non-english
Del: in docs -- https://main.php.net/note/delete/126129/in+docs
Del: other reasons-- https://main.php.net/note/delete/126129
Reject -- https://main.php.net/note/reject/126129
Search -- https://main.php.net/manage/user-notes.php