The implementation of php multi-process depends on the pcntl extension. When compiling PHP, you can add enable-pcntl or compile it separately. Note: 1. the sub-process does not execute the code before fork, but just copies the memory status of the parent process to a new one. Therefore, the personalized settings of the sub-process must be set separately. 2. Output redirection.
The implementation of php multi-process depends on the pcntl extension. When compiling PHP, you can add enable-pcntl or compile it separately. Note: 1. the sub-process does not execute the code before fork, but just copies the memory status of the parent process to a new one. Therefore, the personalized settings of the sub-process must be set separately. 2. Output redirection.
The implementation of php multi-process depends on the pcntl extension. During PHP compilation, you can add '-enable-pcntl' or compile it separately.
Pay attention to the following points:
1. The sub-process does not execute the code before fork, but just copies the memory status of the parent process to a new one. Therefore, the personalized settings of the sub-process must be set separately.
2. Output redirection. Using echo in a program may cause command line confusion and affect resolution. You can use ob_start to redirect to the log file. Of course, it is better to use log directly. The log files in this instance are grouped by the process pid.
3. If no code is executed for the parent process, the child process may exit early and become an orphan process.
Demo acceptance:
Use 10 sub-processes to process output tasks. The total number of tasks is 1000. Then, the tasks are evenly distributed to 10 sub-processes.
0) {}} // task function do_task ($ task_num) {global $ total; $ start = $ total/10 * $ task_num; $ end = $ total/10 * ($ task_num + 1); for (; $ start <$ end; $ start ++) {echo $ task_num. "". $ start. "\ n";} // The sub-process is terminated after the task is executed. Of course, you can return to the Code part of the main process for relevant operations. Exit ();}
Original article address: php multi-process demo. Thank you for sharing it.