简体   繁体   中英

Spawning multiple processes from PHP command-line script

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 have 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.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM