Fibers در PHP — همزمانی سبک و محدودیتهای async
Fiber در PHP 8.1 اجازه میدهد execution در میانه تابع متوقف و بعداً از همان نقطه ادامه یابد — مثل coroutine سبک. این thread نیست و magic async هم نیست؛ بیشتر building block برای کتابخانههای non-blocking است.
Fiber minimal example
$fiber = new Fiber(function (): void {
$value = Fiber::suspend('first suspend');
echo "Resumed with: {$value}\n";
});
$result = $fiber->start();
echo "Got: {$result}\n"; // first suspend
$fiber->resume('hello from outside');
start() تا اولین suspend — resume() ادامه میدهد.
cooperative multitasking
Fiber خودش scheduler نیست — کد باید voluntarily suspend کند. برخلاف OS thread preemptive.
چرا در Laravel روزمره نمیبینید؟
- Laravel request lifecycle سنتی blocking است — PHP-FPM یک request یک process
- Eloquent، file IO، curl — blocking مگر library خاص
- راهحل scale: Queue، Octane+Swoole جداگانه
Fiber vs Queue Job
| Fiber | Queue |
|---|---|
| درون یک process | worker جدا، async زمانی |
| برای lib author | برای business defer |
| پیچیدگی بالا | Laravel idiomatic |
ecosystem
ReactPHP، Amp، Revolt — event loop + fiber. Swoole/OpenSwoole — extension با coroutine native. Laravel Octane روی Swoole/RoadRunner process long-lived.
blocking هنوز مشکل است
Fiber بدون non-blocking IO فقط stack switch است — sleep(5) هنوز block میکند مگر در runtime خاص.
کی Fiber یاد بگیریم؟
- maintain کتابخانه async PHP
- Octane/Swoole در production
- curiosity — نه هر CRUD
جمعبندی
برای Laravel developer عملی: Queue، Horizon، Redis. Fiber آیندهنگری. Error: Error handling. Queue: معرفی Queue.