kage3f/rux-concorrency

A lightweight, native PHP concorrency library using Fibers

Installs: 0

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

pkg:composer/kage3f/rux-concorrency

dev-main 2025-12-20 22:52 UTC

This package is auto-updated.

Last update: 2025-12-20 23:00:06 UTC


README

Latest Stable Version License

A lightweight, zero-dependency native PHP concurrency library built on top of Fibers (PHP 8.1+). It provides a simple async/await implementation for cooperative multitasking.

Features

  • 🚀 Cooperative Multitasking: Run multiple I/O-bound tasks concurrently.
  • 🛠 Simple API: Familiar async, await, and wait functions.
  • 📦 Zero Dependencies: Pure PHP, no extensions like parallel or swoole required.
  • 🔌 Non-blocking I/O: Easily integrate with non-blocking streams.

Installation

Install the package via Composer:

composer require kage3f/rux-concorrency

Quick Start

Basic Concurrency

use function Rux\async;
use function Rux\sleep;
use function Rux\wait;

async(function() {
    echo "Task 1: Starting...\n";
    sleep(1000); // Non-blocking sleep
    echo "Task 1: Finished!\n";
});

async(function() {
    echo "Task 2: Starting...\n";
    sleep(500);
    echo "Task 2: Finished!\n";
});

wait(); // Execute the scheduler

Async/Await Pattern

use function Rux\async;
use function Rux\await;
use function Rux\wait;

async(function() {
    $task1 = async(fn() => "Data from API 1");
    $task2 = async(fn() => "Data from API 2");

    // This will suspend the current fiber until the tasks are finished
    $res1 = await($task1);
    $res2 = await($task2);

    echo "$res1, $res2\n";
});

wait();

Real-World Examples

Check the examples/ directory for professional use cases:

  1. Dashboard Aggregator: Simulates fetching data from multiple microservices concurrently, reducing total response time.
  2. Concurrent HTTP: Demonstrates fetching multiple external URLs using non-blocking streams.

How it works

RuxConcorrency uses PHP Fibers to pause and resume execution. When a task performs a non-blocking operation (like Rux\sleep or a non-blocking stream read), it suspends itself, allowing the Scheduler to run other pending tasks.

License

The MIT License (MIT). Please see License File for more information.