note 129711 added to class.parallel-channel

From: Date: Mon, 19 Aug 2024 07:40:32 +0000
Subject: note 129711 added to class.parallel-channel
Groups: php.notes 
Request: Send a blank email to php-notes+get-221485@lists.php.net to get a copy of this message
<?php /** * Bzz reloaded! * Run two simple tasks in parallel and synchronize them with a channel * * parallel\Channel(int $capacity): Buffered channel * Creates a buffered channel for communication between tasks * @ref https://www.php.net/manual/en/class.parallel-channel.php */ echo "zzz... " . PHP_EOL; // Create new buffered channel $channel = new \parallel\Channel(2); \parallel\run( function($channel) { $snaps_count = rand (8, 12); echo "Number of snaps: $snaps_count" . PHP_EOL; for ($i=1; $i<=$snaps_count; $i++) { $other_sleep_time = rand(3, 5); $my_sleep_time = rand(1, 3); echo "Send sleep time to buffer" . PHP_EOL; $start = microtime(true); $channel->send($other_sleep_time); $wait_time = microtime(true) - $start; if ($wait_time > .1) { echo "Buffer was full. I waited " . round($wait_time) . "s" . PHP_EOL; } echo "I sleep for {$my_sleep_time}s" . PHP_EOL; sleep($my_sleep_time); } echo "I finished sleeping. Closing channel" . PHP_EOL; $channel->close(); }, [$channel] ); \parallel\run( function($channel) { try { while(true) { $my_sleep_time = $channel->recv(); echo "Other sleeps for {$my_sleep_time}s" . PHP_EOL; sleep($my_sleep_time); } } catch(\parallel\Channel\Error\Closed $e) { echo "Channel is closed. Other die."; die; } }, [$channel] ); ---- Server IP: 45.112.84.4 Probable Submitter: 80.90.5.137 (proxied: 77.141.36.155) ---- Manual Page -- https://php.net/manual/en/class.parallel-channel.php Edit -- https://main.php.net/note/edit/129711 Del: integrated -- https://main.php.net/note/delete/129711/integrated Del: useless -- https://main.php.net/note/delete/129711/useless Del: bad code -- https://main.php.net/note/delete/129711/bad+code Del: spam -- https://main.php.net/note/delete/129711/spam Del: non-english -- https://main.php.net/note/delete/129711/non-english Del: in docs -- https://main.php.net/note/delete/129711/in+docs Del: other reasons-- https://main.php.net/note/delete/129711 Reject -- https://main.php.net/note/reject/129711 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#221485) next »