Multi-process application in PHP CLI mode

Source: Internet
Author: User
Tags exit php cli

PHP is often not suitable for resident shell processes, he does not have a dedicated GC routines, and there is no effective memory management path. So if you're using PHP as a resident shell, you'll always be abort and unhappy by memory exhaustion.

Moreover, if the input data is illegal, and the script is not detected, causing abort, it will also make you very unhappy.

That? What do we do?

Oh, don't worry, many processes to help you!

So, what is this for?

Advantages:

1. Using multiple processes, the kernel is responsible for reclaiming resources after the child process finishes

2. Using multiple processes, a subprocess exception exit does not cause the entire process thread to exit. The parent process also has the opportunity to rebuild the process.

3. A resident main process, which is only responsible for the distribution of tasks, has a clearer logic.

Then, how do you do it?

Next, we use the POSIX and PCNTL series functions provided by PHP to implement a PHP command parser that accepts user input and then executes the fork subprocess and is responsible for echoing the end state of the child process.

Code as follows, I added a note, if there is no understanding of the place, you can read the manual related functions, or reply to the message.

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.