Php multi-process demo

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.