I have custom build system written in PHP. It's similar to make tool, gathering source files with dependencies to compile and link them.
It uses system function to run G++, like this:
system("g++ -c $file -o $out");
It's working great. However I have a multi-core processor and I would like to use those extra cores to speed up the process (like make -j 8). How can I do it in PHP? I don't need to h开发者_如何学运维ave multithreading in PHP per se, I just need to spawn few child processes and wait for them to complete.
Since that is usually built-in for PHPs CLI version, you can use pcntl_fork()
. It duplicates the current PHP process into a child, which can then work separately from the parent process.
See the example on the manual page.
(You could also utilize PEAR System_Daemon which encapsulates that functionality; though it does a bit too much for your case.)
精彩评论