PHP implements multi-process parallel operation (can be used as a daemon process)

WBOY
Release: 2016-07-25 09:01:30
Original
919 people have browsed it
PHP simply implements multi-process parallel processing. The child process is responsible for processing things. The parent process can control the number of child processes and is implemented with pcntl extension (Note: This effect cannot be achieved under Windows)                                                      
                                                                                                                                                                                                                                                                                                    



/**
* Entry function
* Save this file as ProcessOpera.php
    * Run /usr/local/php/bin/php ProcessOpera.php &
  1. in terminal*/
  2. ProcessOpera("runCode", array(), 8);
  3. /**
  4. * run Code
  5. */
  6. function runCode($opt = array()) {
  7. //Code that needs to be run in the daemon process
  8. }
  9. /**
  10. * $func is the function name of the child process to perform specific things
  11. * $opt is the parameter of $func in array form
  12. * $pNum is the number of child processes for fork
  13. */
  14. function ProcessOpera($func, $opts = array(), $pNum = 1) {
  15. while(true) {
  16. $pid = pcntl_fork();
  17. if($pid == -1) {
  18. exit("pid fork error");
  19. if($pid) {
  20. static $execute = 0;
  21.               $execute++;
  22.                                                                                        ($execute > while(true) {
  23.                                                                                                                                                                                           1),
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template