note 115855 added to function.pcntl-fork

From: Date: Fri, 03 Oct 2014 15:20:28 +0000
Subject: note 115855 added to function.pcntl-fork
Groups: php.notes 
Request: Send a blank email to php-notes+get-200582@lists.php.net to get a copy of this message
I just thought of contributing to this awesome community and hope this can be of use to someone. Although PHP provides threaded options, and multi curl handles that run in parallel, I managed to bash out a solution to run each function as it's own process for non-threaded versions of PHP. Usage: #!/usr/bin/php Usage: php -f /path/to/file #!/usr/bin/php <?php function fork_process($options) { $shared_memory_monitor = shmop_open(ftok(__FILE__, chr(0)), "c", 0644, count($options['process'])); $shared_memory_ids = (object) array(); for ($i = 1; $i <= count($options['process']); $i++) { $shared_memory_ids->$i = shmop_open(ftok(__FILE__, chr($i)), "c", 0644, $options['size']); } for ($i = 1; $i <= count($options['process']); $i++) { $pid = pcntl_fork(); if (!$pid) { if($i==1) usleep(100000); $shared_memory_data = $options['process'][$i - 1](); shmop_write($shared_memory_ids->$i, $shared_memory_data, 0); shmop_write($shared_memory_monitor, "1", $i-1); exit($i); } } while (pcntl_waitpid(0, $status) != -1) { if(shmop_read($shared_memory_monitor, 0, count($options['process'])) == str_repeat("1", count($options['process']))) { $result = array(); foreach($shared_memory_ids as $key=>$value) { $result[$key-1] = shmop_read($shared_memory_ids->$key, 0, $options['size']); shmop_delete($shared_memory_ids->$key); } shmop_delete($shared_memory_monitor); $options['callback']($result); } } } // Create shared memory block of size 1M for each function. $options['size'] = pow(1024,2); // Define 2 functions to run as its own process. $options['process'][0] = function() { // Whatever you need goes here... // If you need the results, return its value. // Eg: Long running proccess 1 sleep(1); return 'Hello '; }; $options['process'][1] = function() { // Whatever you need goes here... // If you need the results, return its value. // Eg: // Eg: Long running proccess 2 sleep(1); return 'World!'; }; $options['callback'] = function($result) { // $results is an array of return values... // $result[0] for $options['process'][0] & // $result[1] for $options['process'][1] & // Eg: echo $result[0].$result[1]."\n"; }; fork_process($options); ?> If you'd like to get the results back from a webpage, use exec(). Eg: echo exec('php -f /path/to/file'); Continue hacking! :) ---- Server IP: 72.52.91.14 Probable Submitter: 222.164.33.152 ---- Manual Page -- http://php.net/manual/en/function.pcntl-fork.php Edit -- https://master.php.net/note/edit/115855 Del: integrated -- https://master.php.net/note/delete/115855/integrated Del: useless -- https://master.php.net/note/delete/115855/useless Del: bad code -- https://master.php.net/note/delete/115855/bad+code Del: spam -- https://master.php.net/note/delete/115855/spam Del: non-english -- https://master.php.net/note/delete/115855/non-english Del: in docs -- https://master.php.net/note/delete/115855/in+docs Del: other reasons-- https://master.php.net/note/delete/115855 Reject -- https://master.php.net/note/reject/115855 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#200582) next »