略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: parallel\Runtime::close

2024-03-29

parallel\Runtime::close

(0.8.0)

parallel\Runtime::closeRuntime Graceful Join

说明

public parallel\Runtime::close(): void

Shall request that the runtime shutsdown.

注意:

Tasks scheduled for execution will be executed before the shutdown occurs.

Exceptions

警告

Shall throw parallel\Runtime\Error\Closed if Runtime was already closed.

add a noteadd a note

User Contributed Notes 1 note

up
1
andre at webtechnik dot ch
1 year ago
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);
}
?>

官方地址:https://www.php.net/manual/en/parallel-runtime.close.php

冷却塔厂家 广告
-- 广告
北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3