omegaalfa/event-loop

使用 Fibers 实现的 PHP 循环,具有延迟任务和计时器

dev-main 2024-07-05 02:04 UTC

This package is not auto-updated.

Last update: 2024-09-23 02:21:07 UTC


README

使用 Fibers 在 PHP 中实现的简单循环。

描述

Loop 类提供了一个基本的事件循环机制,支持延迟任务和计时器。它使用 PHP Fibers 实现任务的异步执行。

功能

  • 任务的延迟执行。
  • 具有指定延迟的计时器。
  • 使用 PHP Fibers 的异步执行。

要求

PHP 8.1 或更高版本。

安装

composer require omegaalfa/loop

使用

use Omegaalfa\Loop\Loop;

// Create an instance of the EventLoop
$loop = new Loop();

// Add a deferred task
$loop->defer(function () {
    // Code to be executed asynchronously
    echo "Deferred task executed!\n";
});

// Add a timer with a 2-second delay
$loop->setTimeOut(2.0, function () {
    echo "Timer task executed after 2 seconds!\n";
});

// Add a timer with a 1-second delay using addTimer
$loop->addTimer(1.0, function () {
    echo "Timer task executed after 1 second!\n";
});

$loop->repeat(number: 5, intervalSeconds: 2, callback: function () {
    echo "REPEAT\n";
});

// Run the event loop
$loop->run();

贡献

如果您想贡献改进或修正,请随意在存储库中创建拉取请求或打开一个问题。

许可

此项目采用 MIT 许可证。