Kore Nordmann - PHP / Projects / Politics ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ :Author: Kore Nordmann :Date: Thu, 06 May 2010 17:42:50 +0200 :Revision: 1 :Copyright: CC by-sa ============================= Native parallel PHP job queue ============================= :Description: To make use of multiple cores for some rather long processing operations I needed a way to fork multiple workers from a single PHP script multiple times lately. So I created a small project on github which implements this in a way, so that it should reusable by anybody. Read more for the details… To make use of multiple cores for some rather long processing operations I needed a way to fork multiple workers from a single PHP script multiple times lately. So I created a small project on github which implements this in a way, so that it should reusable by anybody. This is far from being rocket science, but still might be useful to someone out there. Native job queue ================ I also implemented a ``ShellJobProvider`` (which implements ``JobProvider``), which is constructed from an array of shell commands, which then are executed in parallel. A simple working example:: run( new \njq\ShellJobProvider( array( 'echo 1 >> test', 'echo 2 >> test', 'echo 3 >> test', 'echo 4 >> test', 'echo 5 >> test', ) ), 4 ); ?> The file ``test`` will then contain something like (the order might vary):: 5 4 3 2 1 The ``4`` (second parameter of ``\njq\Executor::run``) defines the number of parallel processes to spawn. This should not exceed the number of available cores in the most cases. Requirements ============ The job queue requires PHP 5.3 and the PHP PCNTL extension. Get it ====== I like github__ for dropping projects I do not want to maintain very actively, so this small piece of code is also hosted on github: http://github.com/kore/njq - happy forking. It, of course, has documentation and tests - as always. __ http://github.com .. Local Variables: mode: rst fill-column: 79 End: vim: et syn=rst tw=79 Trackbacks ========== Comments ======== - Lars Johansson at Sat, 08 May 2010 18:18:55 +0200 Hey Core, I like your way of pcntl_fork a lot, nice clean code. I do a lot PHP parallel processing mostly in 'map and reduce' stile. Oddly enough in a job scheduler written in PHP. Below is an example of a BOM explosion job that fork in two levels. The inner level is a map and reduce fork. And it really pays off. The execution time is 'proportional' to the amount of processes (up to about 12 processes for this job in my machine). DBPFXDB3,DB9DB3 C_DB if ("@DBPFX"=='DB3') return 'ACTADW'; elseif ("@DBPFX"=='DB9') return 'CPDDW'; else return FALSE; BOMTABLE@DBPFX_BOM_BASIC FILE@BOMTABLE.TXT TOPTABLE@BOMTABLE_TOP USE @C_DB; DROP TABLE IF EXISTS @TOPTABLE; create table @TOPTABLE (select P_PLANT, P_MATNR, MIN(ALT_BOM) AS ALT_BOM, BOM_USE from @BOMTABLE group by P_PLANT, P_MATNR, BOM_USE order by P_PLANT, P_MATNR,ALT_BOM,BOM_USE); USE @C_DB; select * from @TOPTABLE; BOMTREETABLE@BOMTABLE_TREE INFILE@J_explode/@FILE USE @C_DB; LOAD DATA LOCAL INFILE '@INFILE' REPLACE INTO TABLE @BOMTREETABLE FIELDS TERMINATED BY ';' IGNORE 0 LINES ; - Hyh at Sun, 09 May 2010 08:51:24 +0200 Where you are using multiple core? - Shyam at Fri, 02 Jul 2010 10:35:07 +0200 Thank you, this saved me a lot of trouble, very nicely done. - bn at Sun, 23 Oct 2011 07:15:33 +0200 I'm not sure but is it true that pcntl only works on CLI? if it does, have you tried this on apache/web interface? Thank you - Gucci pas cher femme at Tue, 29 Nov 2011 07:14:31 +0100 jour surpasser son idole et de gagner son Championnat WWE quatrième femme en moins de trois years.Yet, le recul, qui est vraiment surpris