مقاله

Fiber::suspend و resume، تفاوت با thread و چرا Laravel هنوز sync-first است — واقع‌بینانه نه hype.

Fibers در PHP — همزمانی سبک و محدودیت‌های async

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

FiberQueue
درون یک processworker جدا، 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.

معماری async