note 126411 added to class.parallel-runtime

From: Date: Fri, 10 Sep 2021 17:54:07 +0000
Subject: note 126411 added to class.parallel-runtime
Groups: php.notes 
Request: Send a blank email to php-notes+get-216945@lists.php.net to get a copy of this message
<?php // make sure that is really parallel execution (showcase2). // Then you use echo within task-function there is a real chance that the application will hang. // We should control Futures to avoid this behaviour. // You will get different output on each execution. use parallel\{Future, Runtime}; const THREADS_COUNT = 10; const THREADS_I_MAX = 100; // executed within thread $task = static function (int $i, int $to): void { echo "[enter$i]"; for ($j = 0; $j < $to; $j++) { echo $i; } echo "[exit$i]"; }; // creating a few threads $runtimeList = []; for ($i = 0; $i < THREADS_COUNT; $i++) { $runtimeList[] = new Runtime(); } // run all threads $futureList = []; foreach ($runtimeList as $i => $runtime) { echo "[run$i]"; $futureList[] = $runtime->run($task, [$i, THREADS_I_MAX]); } // waiting until all threads are done // if you delete code bellow then your script will hang do { usleep(1); $allDone = array_reduce( $futureList, function (bool $c, Future $future): bool { return $c && $future->done(); }, true ); } while (false === $allDone); echo "done\n"; ---- Server IP: 45.112.84.4 Probable Submitter: 45.112.85.242 (proxied: 176.104.221.149) ---- Manual Page -- https://php.net/manual/en/class.parallel-runtime.php Edit -- https://main.php.net/note/edit/126411 Del: integrated -- https://main.php.net/note/delete/126411/integrated Del: useless -- https://main.php.net/note/delete/126411/useless Del: bad code -- https://main.php.net/note/delete/126411/bad+code Del: spam -- https://main.php.net/note/delete/126411/spam Del: non-english -- https://main.php.net/note/delete/126411/non-english Del: in docs -- https://main.php.net/note/delete/126411/in+docs Del: other reasons-- https://main.php.net/note/delete/126411 Reject -- https://main.php.net/note/reject/126411 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#216945) next »