note 84338 added to function.proc-get-status

From: Date: Wed, 09 Jul 2008 20:49:23 +0000
Subject: note 84338 added to function.proc-get-status
Groups: php.notes 
Request: Send a blank email to php-notes+get-141330@lists.php.net to get a copy of this message
The following function takes an array of shell commands and executes them. It is able to execute up to $nb_max_process at the same time. As soon as one process is terminated, another one is executed. Quite useful if you want to batch process commands on a multi-processor or multi-core environment. The example below tries to convert to PNG a list of SVG files submitted on the command line (using Inkscape). (it's quick and dirty but works very well for me) #!/usr/bin/php <?php function pool_execute($commandes,$nb_max_process) { $pool=array(); for($i=0;$i<$nb_max_process;$i++) { $pool[$i]=FALSE; } while(count($commandes)>0) { $commande=array_shift($commandes); $commande_lancee=FALSE; while($commande_lancee==FALSE) { usleep(50000); for($i=0;$i<$nb_max_process and $commande_lancee==FALSE;$i++) { if($pool[$i]===FALSE) { $pool[$i]=proc_open($commande,array(),$foo); $commande_lancee=TRUE; } else { $etat=proc_get_status($pool[$i]); if($etat['running']==FALSE) { proc_close($pool[$i]); $pool[$i]=proc_open($commande,array(),$foo); $commande_lancee=TRUE; } } } } } } $fichiers=$argv; array_shift($fichiers); $commandes=array(); foreach($fichiers as $fichier) { $entree=$fichier; $sortie=basename($fichier,'.svg').".png"; $commandes[]='inkscape --file='.escapeshellarg($entree).' --export-area-canvas --export-png='.escapeshellarg($sortie); } pool_execute($commandes,4); ---- Server IP: 217.174.223.124 Probable Submitter: 62.106.135.30 ---- Manual Page -- http://www.php.net/manual/en/function.proc-get-status.php Edit -- https://master.php.net/note/edit/84338 Del: integrated -- https://master.php.net/note/delete/84338/integrated Del: useless -- https://master.php.net/note/delete/84338/useless Del: bad code -- https://master.php.net/note/delete/84338/bad+code Del: spam -- https://master.php.net/note/delete/84338/spam Del: non-english -- https://master.php.net/note/delete/84338/non-english Del: in docs -- https://master.php.net/note/delete/84338/in+docs Del: other reasons-- https://master.php.net/note/delete/84338 Reject -- https://master.php.net/note/reject/84338 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#141330) next »