PHP Fibers
In this page:
$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 8.1+
$fiber = new Fiber(function () {
echo "Fiber started\n";
});
echo get_class($fiber);
?>
Login to try C/C++/Java/PHP code in the editor
एक Fiber बनाना और Start करना
एक Fiber बनाना code के एक block को new Fiber() को pass किए गए एक closure में wrap करता है; जब तक आप explicitly start() call नहीं करते कुछ नहीं चलता, जो उस closure को चलाना शुरू करता है जब तक यह suspend या finish न हो जाए।
उदाहरण: Creating and Starting a Fiber
<?php
$fiber = new Fiber(function () {
// Print "Running inside the fiber\n" to the output
echo "Running inside the fiber\n";
});
$fiber->start();
?>
Login to try C/C++/Java/PHP code in the editor
Execution Suspend करना
चल रहे closure के अंदर से static Fiber::suspend() call करना उस exact point पर execution pause कर देता है और जिस भी code ने fiber शुरू या resume किया था उसे control वापस सौंप देता है।
उदाहरण: Suspending Execution
<?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";
?>
Login to try C/C++/Java/PHP code in the editor
Execution Resume करना
resume() एक suspended fiber को exactly वहीं से वापस उठाता है जहाँ suspend() ने इसे pause किया था, optionally paused code में एक value वापस pass करते हुए -- यही back-and-forth cooperative scheduling को enable करता है।
उदाहरण: Resuming Execution
<?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');
?>
Login to try C/C++/Java/PHP code in the editor
Fiber States Check करना
isStarted(), isSuspended(), और isTerminated() आपको इस पर act करने से पहले किसी fiber की current state check करने देते हैं, पहले से खत्म हो चुके किसी fiber को resume करने की कोशिश जैसी errors रोकते हुए।
उदाहरण: Checking Fiber States
<?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());
?>
Login to try C/C++/Java/PHP code in the editor
Fiber::resume()call करना भूल जाना, ताकि suspended fiber कभी continue न हो।- किसी चल रहे fiber के बाहर
Fiber::suspend()call करना, जो एक error throw करता है। - fibers को threads की तरह treat करना, जबकि वे parallel में नहीं चलते।
Chapter Quiz — Complete all 24 topics to unlock
0/24 topics done
Complete these topics first:
- PHP Date & Time
- PHP Math Functions
- PHP JSON Handling
- PHP XML Handling
- PHP cURL Introduction
- PHP REST API Basics
- PHP Composer & Packages
- PHP Autoloading
- PHP Design Patterns
- PHP MVC Architecture
- PHP Security Best Practices
- PHP Performance Optimization
- PHP 8 New Features
- PHP Type Declarations
- PHP Match Expression Advanced
- PHP Fibers
- PHP Attributes
- PHP Magic Constants
- PHP Include & Require
- PHP Iterables
- PHP SimpleXML Parser
- PHP SimpleXML Get
- PHP XML Expat Parser
- PHP DOM Parser