decodelabs/eventful

异步IO事件调度器

v0.3.5 2024-08-22 01:07 UTC

This package is auto-updated.

Last update: 2024-09-04 21:04:40 UTC


README

PHP from Packagist Latest Version Total Downloads GitHub Workflow Status PHPStan License

PHP的异步IO事件调度器

Eventful提供可扩展的IO事件调度器,用于交互式和异步进程。

DecodeLabs博客上获取新闻和更新。

安装

使用composer安装库

composer require decodelabs/eventful

使用

监听IO、信号和定时器上的事件,并相应地做出响应。如果PHP的Event扩展可用,将使用它;否则,将使用基本的select()循环来填补空白。

use DecodeLabs\Deliverance;
use DecodeLabs\Eventful\Factory;

$broker = Deliverance::newCliBroker();

$eventLoop = Factory::newDispatcher()

    // Run every 2 seconds
    ->bindTimer('timer1', 2, function() use($broker) {
        $broker->writeLine('Timer 1');
    })

    // Listen for reads, but frozen - won't activate until unfrozen
    ->bindStreamReadFrozen($input = $broker->getFirstInputReceiver(), function() use($broker) {
        $broker->writeLine('You said: '.$broker->readLine());
    })

    // Run once after 1 second
    ->bindTimerOnce('timer2', 1, function($binding) use($broker, $input) {
        $broker->writeLine('Timer 2');

        // Unfreeze io reads
        $binding->eventLoop->unfreeze($intput);
    })

    // Check if we want to bail every second
    ->setCycleHandler(function(int $cycles) {
        if($cycles > 10) {
            return false;
        }
    });


/*
Outputs something like:

Timer 2
Timer 1
Timer 1
You said: Hello world
Timer 1
*/

许可

Eventful遵循MIT许可协议。有关完整许可协议,请参阅LICENSE