PHP Classes

How to Implement a PHP Task Scheduler Using the Package Concurrent: Manage the execution of several tasks using Fibers

Recommend this page to a friend!
     
  Info   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2026-02-11 (1 month ago) RSS 2.0 feedNot yet rated by the usersTotal: Not yet counted Not yet ranked
Version License PHP version Categories
concurrent 1.0.0MIT/X Consortium ...8.1Language, Performance and optimization, P...
Description 

Author

This package can manage the execution of several tasks using Fibers.

It provides a task class that can take a PHP Fiber object that can run a parallel task to manage the state of the task run using the Fiber object.

The package also provides a Concurrent class that can start parallel tasks using callback functions that can execute the code of the task when the functions are called.

It can also perform several operations to manage the execution of a task like:

- Yield a task

- Make a task sleep for a period

- Wait for a task to finish and return the task execution result

- Get a list of all tasks being managed

- Wait for the next task to finish

- Wait for all tasks to finish until a period

- Cancel the execution of a task

- Run all managed tasks

Innovation Award
PHP Programming Innovation award nominee
February 2026
Number 2
Fibers is a feature introduced in PHP 8.1 to allow developers to execute multiple tasks in parallel.

This package helps to manage multiple task running in parallel using Fibers.

Manuel Lemos
Picture of Dwight José Trujillo Barco
Name: Dwight José Trujillo ... is available for providing paid consulting. Contact Dwight José Trujillo ... .
Classes: 1 package by
Country: Venezuela Venezuela
Age: 61
All time rank: Not yet ranked
Week rank: Not yet ranked
Innovation award
Innovation award
Nominee: 1x

Instructions

Concurrent brings safe, structured concurrency to PHP 8.1+ using native Fibers.

It offers a simple, blocking-style API that any PHP developer can understand – without needing to learn event-loop patterns, promise chains, or process forking.

Concurrent brings safe, structured concurrency to PHP 8.1+ using native Fibers. It offers a simple, blocking-style API that any PHP developer can understand – without needing to learn event-loop patterns, promise chains, or process forking.

  • The Problem

PHP applications frequently need to perform multiple I/O operations at once: calling several REST APIs, querying different databases, reading multiple files, or aggregating data from microservices. Traditional solutions force you to either:

  • Run tasks sequentially (slow and inefficient).
  • Use complex async libraries (ReactPHP, Amp) with a completely different programming model.
  • Fork processes (heavy, often unavailable on shared hosts).

PHP 8.1 introduced Fibers – low-level primitives for cooperative multitasking – but left a gap for a high-level, beginner-friendly API.

  • The Solution

Concurrent fills that gap. It provides a tiny scheduler that manages Fibers transparently. You write plain, sequential code – Concurrent runs it in parallel.

$userTask   = Concurrent::spawn(fn() => $this->db->query('SELECT * FROM users'));
$orderTask  = Concurrent::spawn(fn() => $this->api->getOrders());
$emailTask  = Concurrent::spawn(fn() => $this->mailer->sendBulk());

// All three run concurrently – wait for all of them
[$users, $orders, $emails] = Concurrent::all([$userTask, $orderTask, $emailTask]);

  • Key Features
  • spawn(callable): Task – launches a new concurrent task (Fiber).
  • await(Task): mixed – waits for a specific task and returns its result (re-throws any exception).
  • all(array $tasks): array – waits for all given tasks; returns results in the original order.
  • any(array $tasks): Task – waits for the first task to complete, cancels the others.
  • withTimeout(float $seconds, array $tasks): array – fails fast if tasks don’t finish in time.
  • cancel(Task): void – safely cancels a running task.
  • yield(): void – voluntarily yields control to other tasks (cooperative multitasking).
  • sleep(float $seconds): void – non-blocking sleep (uses yield).

All methods are static and fully re-entrant – you can run multiple independent schedulers in different Fibers.

  • Why It’s Different
  • No global event loop – the scheduler runs only when tasks are active.
  • No promises – you don’t chain .then(), you simply block and get a result.
  • Zero dependencies – pure PHP, no Composer packages, no PECL extensions.
  • Structured concurrency – tasks are bound to the scheduler that spawned them; no orphaned fibers.
  • How It Works
  1. Concurrent::run() starts the scheduler (if not already running).
  2. spawn() creates a new Fiber and adds it to the run queue.
  3. The scheduler repeatedly picks the next runnable task and resumes it.
  4. When a task calls yield() or sleep(), it is suspended and re-queued.
  5. When a task finishes, all Fibers waiting for it (via await/all/any) are resumed.
  6. Exceptions thrown inside a task are captured and re-thrown when you await() that task.
  • Use Cases
  • API gateways – aggregate 10+ microservices in parallel.
  • CLI tools – scrape websites, process multiple files, batch database inserts.
  • Job workers – handle several jobs concurrently with lightweight Fibers.
  • Laravel/Symfony – parallelise event listeners, command bus handlers, or queued jobs.
  • Current Limitations
  • Cooperative multitasking – tasks must yield periodically (via yield() or sleep()) to avoid blocking the whole scheduler.

Blocking I/O functions (file_get_contents, PDO::query, curl_exec) do not yield – use non-blocking alternatives or wrap them in a custom Fiber-aware adapter.

  • No I/O multiplexing yet – future versions may include stream_select() or curl_multi wrappers that yield automatically.
  • Future Direction
  • Adapters for Guzzle, PDO, Redis, and file streams that transparently yield.
  • Optional coroutine-style async / await syntactic sugar (requires PHP RFC).
  • Composer package with PSR-11 container integration.

Concurrent is a solid foundation for modern, high-performance PHP. Drop it into any 8.1+ project and start parallelizing I/O-bound work today.

  Files folder image Files (1)  
File Role Description
Plain text file Concurrent.php Class Code

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads  
 0%
Total:0
This week:0