php8.1 Fiber初使用

时间:2021-08-23

Fiber有哪些方法?

final class Fiber
{
    /**
     * @param callable $callback Function to invoke when starting the fiber.
     */
    public function __construct(callable $callback) {}
 
    /**
     * Starts execution of the fiber. Returns when the fiber suspends or terminates.
     *
     * @param mixed ...$args Arguments passed to fiber function.
     *
     * @return mixed Value from the first suspension point or NULL if the fiber returns.
     *
     * @throw FiberError If the fiber has already been started.
     * @throw Throwable If the fiber callable throws an uncaught exception.
     */
    public function start(mixed ...$args): mixed {}
 
    /**
     * Resumes the fiber, returning the given value from {@see Fiber::suspend()}.
     * Returns when the fiber suspends or terminates.
     *
     * @param mixed $value
     *
     * @return mixed Value from the next suspension point or NULL if the fiber returns.
     *
     * @throw FiberError If the fiber has not started, is running, or has terminated.
     * @throw Throwable If the fiber callable throws an uncaught exception.
     */
    public function resume(mixed $value = null): mixed {}
 
    /**
     * Throws the given exception into the fiber from {@see Fiber::suspend()}.
     * Returns when the fiber suspends or terminates.
     *
     * @param Throwable $exception
     *
     * @return mixed Value from the next suspension point or NULL if the fiber returns.
     *
     * @throw FiberError If the fiber has not started, is running, or has terminated.
     * @throw Throwable If the fiber callable throws an uncaught exception.
     */
    public function throw(Throwable $exception): mixed {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted(): bool {}
 
    /**
     * @return bool True if the fiber is suspended.
     */
    public function isSuspended(): bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning(): bool {}
 
    /**
     * @return bool True if the fiber has completed execution (returned or threw).
     */
    public function isTerminated(): bool {}
 
    /**
     * @return mixed Return value of the fiber callback. NULL is returned if the fiber does not have a return statement.
     *
     * @throws FiberError If the fiber has not terminated or the fiber threw an exception.
     */
    public function getReturn(): mixed {}
 
    /**
     * @return self|null Returns the currently executing fiber instance or NULL if in {main}.
     */
    public static function this(): ?self {}
 
    /**
     * Suspend execution of the fiber. The fiber may be resumed with {@see Fiber::resume()} or {@see Fiber::throw()}.
     *
     * Cannot be called from {main}.
     *
     * @param mixed $value Value to return from {@see Fiber::resume()} or {@see Fiber::throw()}.
     *
     * @return mixed Value provided to {@see Fiber::resume()}.
     *
     * @throws FiberError Thrown if not within a fiber (i.e., if called from {main}).
     * @throws Throwable Exception provided to {@see Fiber::throw()}.
     */
    public static function suspend(mixed $value = null): mixed {}
}

示例一:无参数方法使用

<?php

function test(){
    echo 'test方法';
}
$fiber = new Fiber('test');
$fiber->start();

页面输出:test方法

暂停方法执行

<?php

function test(){
    Fiber::suspend();
    echo 'test方法';
}
$fiber = new Fiber('test');
$fiber->start();

页面无任何输出

恢复方法执行

<?php

function test(){
    Fiber::suspend();
    echo 'test方法';
}
$fiber = new Fiber('test');
$fiber->start();
$fiber->resume();

页面再次输出:test方法

示例二:有参数的方法使用

<?php

function test($name){
    Fiber::suspend('1');
    Fiber::suspend('2');
    echo 'name:'.$name.'<br>';
}
$fiber = new Fiber('test');
$value = $fiber->start('李四');
echo 'start:'.$value.'<br>';
$value = $fiber->resume();
echo 'resume 1:'.$value.'<br>';
$value = $fiber->resume();
echo 'resume 2:'.$value.'<br>';

页面输出

start:1
resume 1:2
name:李四
resume 2:

示例三:匿名函数使用

<?php

$fiber = new Fiber(function (): void {
    $value = Fiber::suspend('fiber');
    echo "Value used to resume fiber: ", $value, "\n";
});

$value = $fiber->start();

echo "Value from fiber suspending: ", $value, "\n";

$fiber->resume('test');

页面输出

Value from fiber suspending: fiber

Value used to resume fiber: test

总结:Fiber可以传递数据。