note 128227 added to function.pcntl-fork

From: Date: Sat, 18 Feb 2023 02:46:41 +0000
Subject: note 128227 added to function.pcntl-fork
Groups: php.notes 
Request: Send a blank email to php-notes+get-220183@lists.php.net to get a copy of this message
Here is an APCu alternative (instead of shared memory) function of fork_process by kenneth at fellowrock dot com https://www.php.net/manual/en/function.pcntl-fork.php#115855 <?php // usage $namespace = 'some_namespace'; $processes = [ function () { sleep(1); return 'Hello'; }, function () { sleep(1); return ' world!'; }, ]; $callback = function ($result) { echo $result[0] . $result[1]; }; threadize($namespace, $processes, $callback); function threadize(string $namespace, $processes, callable $callback) { apcu_store($namespace . '_monitor', 0, 86400 * 3); for ($i = 0; $i < count($processes); $i++) { $pid = pcntl_fork(); if (!$pid) { if ($i == 1) { usleep(100000); } apcu_store($namespace . '_process_result_' . $i, $processes[$i](), 86400 * 3); apcu_inc($namespace . '_monitor'); exit(0); } } while (pcntl_waitpid(0, $status) != -1) { if (apcu_fetch($namespace . '_monitor') === count($processes)) { $result = []; for ($i = 0; $i < count($processes); $i++) { $result[$i] = apcu_fetch($namespace . '_process_result_' . $i); apcu_delete($namespace . '_process_result_' . $i); } $callback($result); apcu_delete($namespace . '_monitor'); } } } ?> ---- Server IP: 45.112.84.4 Probable Submitter: 80.90.13.130 (proxied: 174.91.26.233) ---- Manual Page -- https://php.net/manual/en/function.pcntl-fork.php Edit -- https://main.php.net/note/edit/128227 Del: integrated -- https://main.php.net/note/delete/128227/integrated Del: useless -- https://main.php.net/note/delete/128227/useless Del: bad code -- https://main.php.net/note/delete/128227/bad+code Del: spam -- https://main.php.net/note/delete/128227/spam Del: non-english -- https://main.php.net/note/delete/128227/non-english Del: in docs -- https://main.php.net/note/delete/128227/in+docs Del: other reasons-- https://main.php.net/note/delete/128227 Reject -- https://main.php.net/note/reject/128227 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#220183) next »