note 129764 added to class.fiber

From: Date: Tue, 24 Sep 2024 18:08:31 +0000
Subject: note 129764 added to class.fiber
Groups: php.notes 
Request: Send a blank email to php-notes+get-221547@lists.php.net to get a copy of this message
<?php class Scheduler { private array $taskQueue = []; public function newTask(callable $task): void { $fiber = new Fiber($task); $this->taskQueue[] = $fiber; } public function run(): void { while (!empty($this->taskQueue)) { $fiber = array_shift($this->taskQueue); if ($fiber->isStarted()) { // Resume the fiber if it has been started before $fiber->resume(); } else { // Start the fiber for the first time $fiber->start(); } if (!$fiber->isTerminated()) { // Re-queue the fiber if it hasn't finished execution $this->taskQueue[] = $fiber; } } } } // Define some tasks function task1(): void { for ($i = 1; $i <= 3; $i++) { echo "Task 1 - Iteration $i\n"; Fiber::suspend(); // Pause execution and yield control back to the scheduler } } function task2(): void { for ($i = 1; $i <= 5; $i++) { echo "Task 2 - Iteration $i\n"; Fiber::suspend(); } } // Initialize the scheduler and add tasks $scheduler = new Scheduler(); $scheduler->newTask('task1'); $scheduler->newTask('task2'); // Run the scheduler $scheduler->run(); ?> Scheduler Class: - The Scheduler class manages a queue of fibers (tasks). - The newTask method creates a new Fiber from a callable and adds it to the task queue. - The run method loops through the task queue, starting or resuming each fiber. If a fiber suspends itself (i.e., it hasn’t terminated), it’s re-added to the queue for further execution. Tasks: - task1 and task2 are simple functions that simulate work by looping and printing messages. - Each task uses Fiber::suspend() to yield control back to the scheduler after each iteration. Execution Flow: - The scheduler starts by initializing the task queue with the provided tasks. - It enters a loop where it takes the first fiber in the queue: - If the fiber hasn’t started yet, it starts it with start(). - If it has been started before, it resumes execution with resume(). - After a fiber runs, if it hasn’t terminated, it’s added back to the end of the queue. - This process continues until all fibers have terminated. Output: Task 1 - Iteration 1 Task 2 - Iteration 1 Task 1 - Iteration 2 Task 2 - Iteration 2 Task 1 - Iteration 3 Task 2 - Iteration 3 Task 2 - Iteration 4 Task 2 - Iteration 5 ---- Server IP: 45.112.84.4 Probable Submitter: 80.90.5.137 (proxied: 86.123.117.239) ---- Manual Page -- https://php.net/manual/en/class.fiber.php Edit -- https://main.php.net/note/edit/129764 Del: integrated -- https://main.php.net/note/delete/129764/integrated Del: useless -- https://main.php.net/note/delete/129764/useless Del: bad code -- https://main.php.net/note/delete/129764/bad+code Del: spam -- https://main.php.net/note/delete/129764/spam Del: non-english -- https://main.php.net/note/delete/129764/non-english Del: in docs -- https://main.php.net/note/delete/129764/in+docs Del: other reasons-- https://main.php.net/note/delete/129764 Reject -- https://main.php.net/note/reject/129764 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#221547) next »