{
    "slug": "php_fiber_scheduler",
    "term": "Fiber-Based Task Scheduler",
    "category": "php",
    "difficulty": "advanced",
    "short": "Building a cooperative multitasking scheduler with PHP Fibers — suspending and resuming tasks at I/O wait points to run multiple tasks concurrently in a single thread.",
    "long": "PHP Fibers (8.1) are stackful coroutines — a Fiber can suspend itself with Fiber::suspend(), returning control to the caller, and be resumed later with $fiber->resume(). A scheduler maintains a queue of Fibers and runs each until it suspends or completes. Combined with non-blocking I/O (event loops like ReactPHP/Revolt), this enables genuine concurrent I/O in a single PHP thread. This is the foundation of async PHP frameworks and how libraries like Amp v3 implement their concurrency model.",
    "aliases": [
        "Fibers",
        "coroutine scheduler",
        "cooperative multitasking",
        "async PHP"
    ],
    "tags": [
        "php",
        "php81",
        "concurrency",
        "async"
    ],
    "misconception": "PHP Fibers provide parallel execution — Fibers are cooperative (not preemptive) and single-threaded; they enable concurrency (interleaved I/O) not parallelism (simultaneous CPU execution).",
    "why_it_matters": "Understanding how Fiber-based schedulers work explains how ReactPHP, Amp, and Swoole achieve high concurrency without threads — essential for writing correct async PHP code.",
    "common_mistakes": [
        "Blocking I/O inside a Fiber — blocks the entire scheduler, defeating concurrency.",
        "Not calling Fiber::suspend() when waiting for I/O — the Fiber monopolises the scheduler.",
        "Creating Fibers for CPU-bound work — they share the same thread; use separate processes instead.",
        "Not handling Fiber exceptions — uncaught exceptions in a Fiber terminate it silently."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "fibers",
        "async_vs_parallel",
        "event_loop_internals",
        "php_async_frameworks"
    ],
    "prerequisites": [
        "fibers",
        "php_async_frameworks",
        "coroutines"
    ],
    "refs": [
        "https://www.php.net/manual/en/class.fiber.php"
    ],
    "bad_code": "// Blocking I/O inside Fiber — no concurrency gained:\n$fiber = new Fiber(function(): void {\n    $data = file_get_contents('https://api.example.com/data'); // BLOCKS!\n    Fiber::suspend($data); // Too late — already waited synchronously\n});",
    "good_code": "// Cooperative scheduler with non-blocking I/O:\n$scheduler = new SplQueue();\n\n$addTask = function(Fiber $fiber) use ($scheduler) {\n    $scheduler->enqueue($fiber);\n};\n\n// Run scheduler:\nwhile (!$scheduler->isEmpty()) {\n    $fiber = $scheduler->dequeue();\n    if (!$fiber->isStarted()) $fiber->start();\n    elseif ($fiber->isSuspended()) $fiber->resume();\n    if ($fiber->isSuspended()) $scheduler->enqueue($fiber); // Re-queue\n}\n\n// Task: suspend at I/O point, resume when ready:\n$task = new Fiber(function() use ($loop): void {\n    $promise = $loop->httpGet('https://api.example.com');\n    $data = Fiber::suspend($promise); // Suspend — scheduler runs other tasks\n    echo $data;\n});",
    "quick_fix": "Use Amp v3 or ReactPHP which include a Fiber-based scheduler — writing your own scheduler is complex; use the framework's event loop abstraction",
    "severity": "info",
    "effort": "high",
    "created": "2026-03-16",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/php_fiber_scheduler",
        "html_url": "https://codeclaritylab.com/glossary/php_fiber_scheduler",
        "json_url": "https://codeclaritylab.com/glossary/php_fiber_scheduler.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[Fiber-Based Task Scheduler](https://codeclaritylab.com/glossary/php_fiber_scheduler) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/php_fiber_scheduler"
            }
        }
    }
}