{"id":599,"date":"2019-10-22T03:29:18","date_gmt":"2019-10-22T03:29:18","guid":{"rendered":"http:\/\/info.juliusgoh.life\/?p=599"},"modified":"2019-10-30T09:24:49","modified_gmt":"2019-10-30T09:24:49","slug":"php-forking-with-child-process-limit","status":"publish","type":"post","link":"https:\/\/info.juliusgoh.life\/?p=599","title":{"rendered":"PHP Forking with child process limit"},"content":{"rendered":"<pre>function runProcessor()\r\n{\r\n    $max_fork = 2;\r\n    $tasks = [\r\n      \"test1\" =&gt; \"process1\",\r\n      \"test2\" =&gt; \"process2\",\r\n      \"test3\" =&gt; \"process3\",\r\n      \"test4\" =&gt; \"process4\",\r\n      \"test5\" =&gt; \"process5\",\r\n      \"test6\" =&gt; \"process6\",\r\n      \"test7\" =&gt; \"process7\",\r\n      \"test8\" =&gt; \"process8\",\r\n      \"test9\" =&gt; \"process9\",\r\n      \"test10\" =&gt; \"process10\",\r\n    ];\r\n    $pids = array();\r\n    $pid_count = 0;\r\n\r\n    foreach ( $tasks as $seq =&gt; $task ) {\r\n\r\n        $pids[$pid_count] = pcntl_fork();\r\n        switch($pids[$pid_count]) {\r\n            case \"0\":\r\n                return execute_task($task);\r\n                break;\r\n            case \"-1\":\r\n                echo \"Unable to fork new process\\n\";\r\n                exit;\r\n                break;\r\n            default:\r\n                $pid_count++;\r\n                while( count( $pids ) &gt;= $max_fork ) {\r\n                    foreach( $pids as $key=&gt;$pid ) {\r\n                        $pid_status = \"\";\r\n                        if( -1 == pcntl_waitpid( $pid, $pid_status, WNOHANG ) ) {\r\n                            unset($pids[$key]);\r\n                            continue;\r\n                        }\r\n                    }\r\n                    usleep(200000);\r\n                }\r\n                break;\r\n        }\r\n    }\r\n\r\n    exit();\r\n}\r\n\r\nfunction execute_task($task_id) {\r\n  echo \"Starting task: ${task_id}\\n\";\r\n  \/\/ Simulate doing actual work with sleep().\r\n  $execution_time = rand(5, 10);\r\n  sleep($execution_time);\r\n  echo \"Completed task: ${task_id}. Took ${execution_time} seconds.\\n\";\r\n\r\n  exit();\r\n}\r\n\r\n\r\nrunProcessor();\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>function runProcessor() { $max_fork = 2; $tasks = [ &#8220;test1&#8221; =&gt; &#8220;process1&#8221;, &#8220;test2&#8221; =&gt; &#8220;process2&#8221;, &#8220;test3&#8221; =&gt; &#8220;process3&#8221;, &#8220;test4&#8221; =&gt; &#8220;process4&#8221;, &#8220;test5&#8221; =&gt; &#8220;process5&#8221;, &#8220;test6&#8221; =&gt; &#8220;process6&#8221;, &#8220;test7&#8221; =&gt; &#8220;process7&#8221;, &#8220;test8&#8221; =&gt; &#8220;process8&#8221;, &#8220;test9&#8221; =&gt; &#8220;process9&#8221;, &#8220;test10&#8221; =&gt; &#8220;process10&#8221;, ]; $pids = array(); $pid_count = 0; foreach ( $tasks as $seq =&gt; $task ) { [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":600,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"image","meta":{"footnotes":""},"categories":[4],"tags":[],"_links":{"self":[{"href":"https:\/\/info.juliusgoh.life\/index.php?rest_route=\/wp\/v2\/posts\/599"}],"collection":[{"href":"https:\/\/info.juliusgoh.life\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/info.juliusgoh.life\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/info.juliusgoh.life\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/info.juliusgoh.life\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=599"}],"version-history":[{"count":5,"href":"https:\/\/info.juliusgoh.life\/index.php?rest_route=\/wp\/v2\/posts\/599\/revisions"}],"predecessor-version":[{"id":611,"href":"https:\/\/info.juliusgoh.life\/index.php?rest_route=\/wp\/v2\/posts\/599\/revisions\/611"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/info.juliusgoh.life\/index.php?rest_route=\/wp\/v2\/media\/600"}],"wp:attachment":[{"href":"https:\/\/info.juliusgoh.life\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=599"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/info.juliusgoh.life\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=599"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/info.juliusgoh.life\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=599"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}