An event loop can resume suspended Fibers when timers or I/O become ready.
The examples focus on the operation itself, including the important distinction: A raw Fiber needs manual resume calls; loop-backed tasks schedule resumes automatically.
What It Does
Use the examples below to observe the behavior on a small dataset before extending it to application code.
Before You Start
Read the input requirements before applying this pattern to production data.
Example 1: Basic Use
Requires the Composer package revolt/event-loop. The timer schedules a resume; replace it with an I/O readiness callback for actual non-blocking I/O.
<?php
use Revolt\EventLoop;
require __DIR__ . '/vendor/autoload.php';
$fiber = new Fiber(static function (): void {
echo "started" . PHP_EOL;
Fiber::suspend();
echo "resumed" . PHP_EOL;
});
EventLoop::queue(static function () use ($fiber): void { $fiber->start(); });
EventLoop::delay(0.01, static function () use ($fiber): void {
if ($fiber->isSuspended()) { $fiber->resume(); }
});
EventLoop::run();
This example is meant for a configured application environment; output depends on the installed extension, external service, or database contents.
Example 2: Checking Behavior
Requires the Composer package revolt/event-loop. The timer schedules a resume; replace it with an I/O readiness callback for actual non-blocking I/O. The task queue resumes the Fiber cooperatively rather than launching a thread.
<?php
use Revolt\EventLoop;
require __DIR__ . '/vendor/autoload.php';
$fiber = new Fiber(static function (): void {
echo "started" . PHP_EOL;
Fiber::suspend();
echo "resumed" . PHP_EOL;
});
EventLoop::queue(static function () use ($fiber): void { $fiber->start(); });
EventLoop::delay(0.02, static function () use ($fiber): void {
if ($fiber->isSuspended()) { $fiber->resume(); }
});
EventLoop::run();
This example is meant for a configured application environment; output depends on the installed extension, external service, or database contents.
Example 3: Practical Example
Requires the Composer package revolt/event-loop. The timer schedules a resume; replace it with an I/O readiness callback for actual non-blocking I/O. The task queue resumes the Fiber cooperatively rather than launching a thread.
<?php
use Revolt\EventLoop;
require __DIR__ . '/vendor/autoload.php';
$fiber = new Fiber(static function (): void {
echo "started" . PHP_EOL;
Fiber::suspend();
echo "resumed" . PHP_EOL;
});
EventLoop::queue(static function () use ($fiber): void { $fiber->start(); });
EventLoop::delay(0.03, static function () use ($fiber): void {
if ($fiber->isSuspended()) { $fiber->resume(); }
});
EventLoop::run();
This example is meant for a configured application environment; output depends on the installed extension, external service, or database contents.
Important Behavior and Edge Cases
A Fiber alone does not make blocking operations non-blocking; use a compatible loop and async I/O APIs.
Comparison with Related Approaches
| Approach | Difference |
|---|---|
| Using Fibers with Event Loop Libraries | An event loop can resume suspended Fibers when timers or I/O become ready. |
| Related API or alternative | A raw Fiber needs manual resume calls; loop-backed tasks schedule resumes automatically. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Ignoring the operation-specific limits | A Fiber alone does not make blocking operations non-blocking; use a compatible loop and async I/O APIs. |
| Confusing it with a related API | A raw Fiber needs manual resume calls; loop-backed tasks schedule resumes automatically. |
Key Takeaways
- An event loop can resume suspended Fibers when timers or I/O become ready.
- A Fiber alone does not make blocking operations non-blocking; use a compatible loop and async I/O APIs.
- A raw Fiber needs manual resume calls; loop-backed tasks schedule resumes automatically.
Use Using Fibers with Event Loop Libraries when its documented behavior matches the problem. Test the relevant edge cases in your own runtime before deploying it.