Patterns for PHP multi processes?

后端 未结 7 896
囚心锁ツ
囚心锁ツ 2020-12-06 02:54

Which design pattern exist to realize the execution of some PHP processes and the collection of the results in one PHP process?

Background:
I do

7条回答
  •  离开以前
    2020-12-06 03:26

    From your php script, you could launch another script (using exec) to do the processing. Save status updates in a text file, which could then be read periodically by the parent thread.

    Note: to avoid php waiting for the exec'd script to complete, pipe the output to a file:

    exec('/path/to/file.php | output.log');
    

    Alternatively, you can fork a script using the PCNTL functions. This uses one php script, which when forked can detect whether it is the parent or the child and operate accordingly. There are functions to send/receive signals for the purpose of communicating between parent/child, or you have the child log to a file and the parent read from that file.

    From the pcntl_fork manual page:

    $pid = pcntl_fork();
    if ($pid == -1) {
         die('could not fork');
    } else if ($pid) {
         // we are the parent
         pcntl_wait($status); //Protect against Zombie children
    } else {
         // we are the child
    }
    

提交回复
热议问题