← Back to PHP Course | Chapter 14: Advanced PHP | Lesson 16 of 24

PHP Fibers

एक fiber एक bookmark जैसा है जो किसी task को बीच में pause होकर बाद में resume होने देता है। यह एक program को jobs के बीच politely switch करने देता है, बारी-बारी से।
Syntax
php
$fiber = new Fiber(function () {
    $value = Fiber::suspend(data);   // pause here
});

$fiber->start();
$fiber->resume(value);

Fibers क्या हैं?

PHP 8.1 में add किए गए Fibers, lightweight cooperative coroutines हैं जो एक function को बीच में pause होने और बाद में exactly वहीं से resume होने देते हैं जहाँ यह रुका था, threads के बिना async-style code बनाने के लिए उपयोगी।

उदाहरण: What are Fibers?

php
<?php
// PHP 8.1+
$fiber = new Fiber(function () {
    echo "Fiber started\n";
});
echo get_class($fiber);
?>

एक Fiber बनाना और Start करना

एक Fiber बनाना code के एक block को new Fiber() को pass किए गए एक closure में wrap करता है; जब तक आप explicitly start() call नहीं करते कुछ नहीं चलता, जो उस closure को चलाना शुरू करता है जब तक यह suspend या finish न हो जाए।

उदाहरण: Creating and Starting a Fiber

php
<?php
$fiber = new Fiber(function () {
    // Print "Running inside the fiber\n" to the output
    echo "Running inside the fiber\n";
});
$fiber->start();
?>

Execution Suspend करना

चल रहे closure के अंदर से static Fiber::suspend() call करना उस exact point पर execution pause कर देता है और जिस भी code ने fiber शुरू या resume किया था उसे control वापस सौंप देता है।

उदाहरण: Suspending Execution

php
<?php
$fiber = new Fiber(function () {
    // Print "Before suspend\n" to the output
    echo "Before suspend\n";
    Fiber::suspend();
    // Print "After resume\n" to the output
    echo "After resume\n";
});
$fiber->start();
// Print "Back in main code\n" to the output
echo "Back in main code\n";
?>

Execution Resume करना

resume() एक suspended fiber को exactly वहीं से वापस उठाता है जहाँ suspend() ने इसे pause किया था, optionally paused code में एक value वापस pass करते हुए -- यही back-and-forth cooperative scheduling को enable करता है।

उदाहरण: Resuming Execution

php
<?php
$fiber = new Fiber(function () {
    // Declare `$value`, set to `Fiber::suspend('paused')`
    $value = Fiber::suspend('paused');
    // Print "Resumed with: $value\n" to the output
    echo "Resumed with: $value\n";
});
$fiber->start();
$fiber->resume('hello');
?>

Fiber States Check करना

isStarted(), isSuspended(), और isTerminated() आपको इस पर act करने से पहले किसी fiber की current state check करने देते हैं, पहले से खत्म हो चुके किसी fiber को resume करने की कोशिश जैसी errors रोकते हुए।

उदाहरण: Checking Fiber States

php
<?php
$fiber = new Fiber(function () {
    Fiber::suspend();
});
// Print a detailed dump (with types) of `$fiber->isStarted()`
var_dump($fiber->isStarted());
$fiber->start();
// Print a detailed dump (with types) of `$fiber->isSuspended()`
var_dump($fiber->isSuspended());
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. Fiber::resume() call करना भूल जाना, ताकि suspended fiber कभी continue न हो।
  2. किसी चल रहे fiber के बाहर Fiber::suspend() call करना, जो एक error throw करता है।
  3. fibers को threads की तरह treat करना, जबकि वे parallel में नहीं चलते।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.