innmind/async-operating-system

此包已被废弃,不再维护。作者建议使用 innmind/mantle 包。

1.0.0 2023-02-11 14:27 UTC

This package is auto-updated.

Last update: 2023-11-06 06:40:23 UTC


README

Build Status codecov Type Coverage

innmind/operating-system 的异步实现,允许在执行任何 I/O 或挂起当前进程时切换到另一个任务。

警告:以下功能在此异步上下文中已禁用

  • 进程创建
  • 信号处理

注意:SQL 连接尚未支持异步。

安装

composer require innmind/async-operating-system

使用方法

use Innmind\Async\OperatingSystem\Factory;
use Innmind\OperatingSystem\Factory as Synchronous;
use Innmind\Filesystem\Name;
use Innmind\Http\{
    Message\Request\Request,
    Message\Method,
    ProtocolVersion,
};
use Innmind\Url\{
    Url,
    Path,
};
use Innmind\Mantle\{
    Source\Predetermined,
    Suspend,
    Forerunner,
};

$synchronous = Synchronous::build();
$factory = Factory::of($synchronous);
$source = Predetermined::of(
    static fn(Suspend $suspend) => $factory
        ->build($suspend)
        ->filesystem()
        ->mount(Path::of('somewhere/'))
        ->get(Name::of('some-file'))
        ->match(
            static fn($file) => doYourStuff($file),
            static fn() => null,
        ),
    static fn(Suspend $suspend) => $factory
        ->build($suspend)
        ->remote()
        ->http()(new Request(
            Url::of('https://wikipedia.org')
            Method::get,
            ProtocolVersion::v11,
        ))
        ->match(
            static fn($success) => doYourStuff($success),
            static fn() => null,
        );
);

Forerunner::of($synchronous->clock())(null, $source);

在此示例中,我们加载一个文件并异步调用维基百科,但您可以使用 $factory->build($suspend) 返回的 OperatingSystem,就像使用其同步版本一样。