A high-performance event loop library for PHP

Overview

workbunny

workbunny/event-loop

🐇 A high-performance event loop library for PHP 🐇

Latest Stable Version Total Downloads Latest Unstable Version License PHP Version Require

简介

一个event-loop实验品;

是一个类似ReactPHP、AMPHP的事件循环组件;

该项目主要研究ext-parallel和PHP-fiber在event-loop中如何有效结合,
研究PHP在不利用ext-event、ext-ev、ext-uv等拓展的前提下是否可以实现
更高的处理能力。

An event loop experiment;

It is an event loop component similar to ReactPHP and AMPHP;

This project mainly studies how ext-parallel and PHP-fiber can
be effectively combined in event-loop, and studies whether PHP 
can achieve higher processing power without using extensions 
like ext-event, ext-ev, ext-uv, etc.

使用

1. 安装

composer require workbunny/event-loop

2. 创建loop

use WorkBunny\EventLoop\Loop;
use WorkBunny\EventLoop\Drivers\NativeLoop;
use WorkBunny\EventLoop\Drivers\EventLoop;
use WorkBunny\EventLoop\Drivers\EvLoop;
use WorkBunny\EventLoop\Drivers\OpenSwooleLoop;

# 创建PHP原生loop
$loop = Loop::create(NativeLoop::class);

# 创建ext-event loop
$loop = Loop::create(EventLoop::class);

# 创建ext-ev loop
$loop = Loop::create(EvLoop::class);

# 创建ext-openswoole loop
$loop = Loop::create(OpenSwooleLoop::class);

3. 创建定时器

  • 无延迟触发器

    通过loop立即执行一次回调函数;

# 立即执行
$id = $loop->addTimer(0.0, 0.0, function (){
    # 业务
});
  • 延迟触发器

    延迟 delay 参数的数值后,执行注册的回调函数,仅执行一次;

# 延迟1秒后执行一次
$id = $loop->addTimer(1.0, 0.0, function (){
    # 业务
});
  • 无延迟定时器

    通过loop立即执行一次回调函数后,根据 repeat 参数的数值间隔执行,直到主动移除该定时器;

# 立即执行一次以后间隔0.1s执行
$id = $loop->addTimer(0.0, 0.1, function (){
    # 业务
});
  • 延迟定时器

    延迟 delay 参数的数值后,执行一次注册的回调函数,之后根据 repeat 参数的数值间隔执行,直到主动移除该定时器;

# 延迟0.1s后间隔0.1s执行
$id = $loop->addTimer(0.1, 0.1, function (){
    # 业务
});

# 延迟0.5s后间隔0.1s执行
$id = $loop->addTimer(0.5, 0.1, function (){
    # 业务
});

4. 创建流事件

这里的流是指 PHP Streams

  • 读取流
$loop->addReadStream(resource, function (){
    # 业务
});
$loop->delReadStream(resource);
  • 写入流
$loop->addWriteStream(resource, function (){
    # 业务
});
$loop->delWriteStream(resource);

5. 创建信号事件

用于接收系统的信号,比如kill等

$loop->addSignal(\SIGUSR1, function (){
    # 业务
});

$loop->delSignal(\SIGUSR1, function (){
    # 业务
});

6. 启动/停止

  • 启动

    以下代码会持续阻塞,请放在程序最后一行

# 该函数后会阻塞
$loop->loop();

# 该行代码不会执行
var_dump('123');
  • 停止

    以下代码不会阻塞等待

$loop->destroy();

# 该行代码会执行
var_dump('123');

说明

1. 测试用例中各个loop比较特殊的地方会在对应测试用例中说明

  • EvLoop 的Stream总是后于Timer 详见EvLoopTest.php
  • EventLoop 的延迟定时器区别于其他Loop的定时器,需要多一个loop周期 详见EventLoopTest.php
  • OpenSwoole 的读/写流不能通过 testReadStreamHandlerTriggeredMultiTimes 测试 详见OpenSwooleLoopTest.php

2. 相同定时器/触发器的优先级遵循先注册先触发

3. OpenSwoole在同一周期内是有优先级的

  1. 通过 Process::signal 设置的信号处理回调函数

  2. 通过 Timer::tick 和 Timer::after 设置的定时器回调函数

  3. 通过 Event::defer 设置的延迟执行函数

  4. 通过 Event::cycle 设置的周期回调函数

4. OpenSwoole的无延迟触发器/无延迟定时器利用了 Event::defer,需要注意优先级

5. OpenSwoole的 Event::defer 可以重复注册多个回调


You might also like...
Ip2region is a offline IP location library with accuracy rate of 99.9% and 0.0x millseconds searching performance. DB file is ONLY a few megabytes with all IP address stored. binding for Java,PHP,C,Python,Nodejs,Golang,C#,lua. Binary,B-tree,Memory searching algorithm

Ip2region是什么? ip2region - 准确率99.9%的离线IP地址定位库,0.0x毫秒级查询,ip2region.db数据库只有数MB,提供了java,php,c,python,nodejs,golang,c#等查询绑定和Binary,B树,内存三种查询算法。 Ip2region特性

Stringy - A PHP string manipulation library with multibyte support, performance optimized

Stringy - A PHP string manipulation library with multibyte support, performance optimized

This library is an implementation of League\Event for Slim Framework

Slim Event Dispatcher This library is an implementation of League\Event for Slim Framework. This works with the latest version of Slim (V3). Installat

Zephir is a compiled high level language aimed to the creation of C-extensions for PHP.

Zephir - is a high level programming language that eases the creation and maintainability of extensions for PHP. Zephir extensions are exported to C c

True coroutines for PHP>=8.1 without worrying about event loops and callbacks.
True coroutines for PHP=8.1 without worrying about event loops and callbacks.

Moebius Pure coroutines for PHP 8.1. To promises and callbacks needed. Just pure parallel PHP code inside coroutines. Moebius Band: A loop with only o

True coroutines for PHP>=8.1 without worrying about event loops and callbacks.
True coroutines for PHP=8.1 without worrying about event loops and callbacks.

Moebius Pure coroutines for PHP 8.1. No promises and callbacks needed. Just pure parallel PHP code inside coroutines. Moebius Band: A loop with only o

Ecotone Framework is Service Bus Implementation. It enables message driven architecture and DDD, CQRS, Event Sourcing PHP
Ecotone Framework is Service Bus Implementation. It enables message driven architecture and DDD, CQRS, Event Sourcing PHP

This is Read Only Repository To contribute make use of Ecotone-Dev repository. Ecotone is Service Bus Implementation, which enables message driven arc

PHP Event Emitter

InitPHP EventEmitter This library has been designed to emit events in its simplest and simplest form. Requirements PHP 5.6 or higher Installation comp

A "from scratch" PHP-based implementation of Event-Sourcing

In here, you will find a "from scratch" PHP-based implementation of Event-Sourcing, kept to a minimum on purpose, to allow workshop attendees to explore and experiment with its concepts.

Releases(1.1.2)
Owner
workbunny
An open source group that has nothing to do.
workbunny
Simple Loop Class

Simple Loop Class

İsa Eken 19 Aug 12, 2022
High performance view templating API for PHP applications using tags & expressions inspired by Java JSTL and C compiler

View Language API Table of contents: About Expressions Tags Configuration Compilation Installation Unit Tests Examples Reference Guide About This API

Lucian Gabriel Popescu 0 Jan 9, 2022
High-performance, low-memory-footprint, single-file embedded database for key/value storage

LDBA - a fast, pure PHP, key-value database. Information LDBA is a high-performance, low-memory-footprint, single-file embedded database for key/value

Simplito 12 Nov 13, 2022
High performance pocketmine 4.0 vote plugin

Voting38 This is a threaded high performance voting plugin for PocketMine-MP API 4.0 You can configure messages and voting rewards in the config. Ther

KingOfTurkey38 7 Jun 1, 2022
A cross-language remote procedure call(RPC) framework for rapid development of high performance distributed services.

Motan Overview Motan is a cross-language remote procedure call(RPC) framework for rapid development of high performance distributed services. Related

Weibo R&D Open Source Projects 5.8k Dec 20, 2022
Tars is a high-performance RPC framework based on name service and Tars protocol, also integrated administration platform, and implemented hosting-service via flexible schedule.

TARS - A Linux Foundation Project TARS Foundation Official Website TARS Project Official Website WeChat Group: TARS01 WeChat Offical Account: TarsClou

THE TARS FOUNDATION PROJECTS 9.6k Jan 1, 2023
High performance mine reset plugin for PocketMine-MP 4.0.0

MineReset38 High performance mine reset plugin for PocketMine-MP 4.0.0 Unlike other plugins, this plugin is not threaded but asynchronous. When a mine

KingOfTurkey38 6 Dec 17, 2022
A high-performance license server system service for creating and managing products, major versions, and software licenses for the purpose of selling installable software products.

A high-performance license server system service for creating and managing products, major versions, and software licenses for the purpose of selling installable software products. Comes with a SDK and command-line tool. Works anywhere that PHP runs.

CubicleSoft 32 Dec 5, 2022
A high-level machine learning and deep learning library for the PHP language.

Rubix ML A high-level machine learning and deep learning library for the PHP language. Developer-friendly API is delightful to use 40+ supervised and

Rubix 1.7k Jan 1, 2023
Rubix ML - A high-level machine learning and deep learning library for the PHP language.

A high-level machine learning and deep learning library for the PHP language. Developer-friendly API is delightful to use 40+ supervised and

Rubix 1.7k Jan 6, 2023