Server-side PHP multi-process programming practices

Source: Internet
Author: User
Tags php cli sapi
Recently, comparing the features of PHP, Python, and Erlang found that PHP has many features that are not commonly used. PHPCLI can be used to implement many good applications. For example, search engine crawlers and long-running computing scripts can replace other languages for server O & M. This is familiar

Recently, comparing the features of PHP, Python, and Erlang found that PHP has many features that are not commonly used. Php cli can be used to implement many good applications. For example, search engine crawlers and long-running computing scripts can replace other languages for server O & M. This is even more powerful for people familiar with PHP.

Recommended bitsCN: Top 10 tips for PHP developers to get twice the result with half the effort

Why is PHP multi-process very good? The reason why most online game servers use multithreading rather than multi-process is that the process is more stable than the thread. In addition, multithreading is suitable for the application scenarios of multi-core servers, and can make full use of the multi-core computing capabilities. Process maintenance can use many operating system-level tools. Message Queue solves many thread communication problems. Therefore, PHP multi-process is suitable for computing-intensive applications on the server.

According to a Vietnamese IT company, they have successfully used PHP background multi-process in enterprise-level applications such as legal document distribution and bank account amount processing.

Using the background PHP process does not affect the server's simultaneous processing of web page requests. Once a backend process fails, it is easy to investigate the cause for recovery or remedy, so it is more robust. Different processes are isolated from each other, which is more efficient and can be used to dispatch various service processes in a unified manner.

PHP is currently the most widely used Web development language, so using PHP for server applications can reduce costs. You can use existing personnel, existing configurations, or even code reuse. What kind of scenarios are more suitable for using PHP background multi-process? For example, the mail distribution, remote service calls, data aggregation, scheduled tasks, and computing result cache do not need to be returned immediately.

PHP single process can achieve the goal in some places, and it is easier to implement. you do not need to consider the synchronization of the process or the data sharing issue. The php cli (sapi server api) command line interface can be used for CRON scheduled tasks and graphic interface programs (using the GTK Library ).

Php cli example

 
 
  1. php -f test.php  
  2. php -r “echo time();”  
  3. php -R as python style 

PHP reads command line parameters:

 
 
  1.  
  2. #! /Usr/bin/php-q
  3. Echo "Test Arguments: \ n ";
  4. Echo

    Recently, comparing the features of PHP, Python, and Erlang found that PHP has many features that are not commonly used. Php cli can be used to implement many good applications. For example, search engine crawlers and long-running computing scripts can replace other languages for server O & M. This is even more powerful for people familiar with PHP.

    Recommended bitsCN: Top 10 tips for PHP developers to get twice the result with half the effort

    Why is PHP multi-process very good? The reason why most online game servers use multithreading rather than multi-process is that the process is more stable than the thread. In addition, multithreading is suitable for the application scenarios of multi-core servers, and can make full use of the multi-core computing capabilities. Process maintenance can use many operating system-level tools. Message Queue solves many thread communication problems. Therefore, PHP multi-process is suitable for computing-intensive applications on the server.

    According to a Vietnamese IT company, they have successfully used PHP background multi-process in enterprise-level applications such as legal document distribution and bank account amount processing.

    Using the background PHP process does not affect the server's simultaneous processing of web page requests. Once a backend process fails, it is easy to investigate the cause for recovery or remedy, so it is more robust. Different processes are isolated from each other, which is more efficient and can be used to dispatch various service processes in a unified manner.

    PHP is currently the most widely used Web development language, so using PHP for server applications can reduce costs. You can use existing personnel, existing configurations, or even code reuse. What kind of scenarios are more suitable for using PHP background multi-process? For example, the mail distribution, remote service calls, data aggregation, scheduled tasks, and computing result cache do not need to be returned immediately.

    PHP single process can achieve the goal in some places, and it is easier to implement. you do not need to consider the synchronization of the process or the data sharing issue. The php cli (sapi server api) command line interface can be used for CRON scheduled tasks and graphic interface programs (using the GTK Library ).

    Php cli example

         
         
    1. php -f test.php  
    2. php -r “echo time();”  
    3. php -R as python style 

    PHP reads command line parameters:

    ___FCKpd___1

    PHP command line interface standard input and output:

         
         
    1.  
    2. #!/usr/bin/php -q  
    3. /* Define STDIN in case if it is not already defined by PHP for some reason */  
    4. if(!defined(“STDIN”)) {  
    5. define(“STDIN”, fopen(‘php://stdin’,'r’))  
    6. }  
    7.  
    8. echo “Hello! What is your name (enter below):\n”;  
    9. $strName = fread(STDIN, 80); // Read up to 80 characters or a newline  
    10. echo ‘Hello ‘ , $strName , “\n”;  
    11. ?> 

    CRONJOB can run certain tasks on a regular basis, but it must prevent repeated operations. Create a lock file at the beginning and delete it at the end. Or use the ps command. Task queue can be implemented using MySQL, Key/VALUE database, or message queue.

    Process control functions:

         
         
    1. Process Control Extensions  
    2. pcntl_fork()  
    3. posix_setsid()  
    4. posix_kill  
    5. pcntl_wait  
    6. pcntl_signal  
    7.  
    8. SIGHUP  
    9. SIGTERM; system shutdown, kill  
    10. SIGINT; sent by Ctrl+c  
    11. SIGKILL (uncatchable); unresponsive, kill -9  
    12. SIGCHLD; child status change  
    13. SIGSTP; sent by Ctrl+z  
    14. SIGCONT; resume from stop, fg 

    PHP cannot throw exceptions to certain errors. how can we improve the fault tolerance of PHP multi-process applications?

    ◆ Processes can be monitored and reported after the dependent processes fail.

    ◆ Use CRONJOB to implement the monitoring process.

    ◆ Write the PID of the monitored process as a file.

    ◆ Regularly check whether the PID file exists. check ps-o pid = or file_exists ('/proc /').

    ◆ If the thread does not exist, restart the process.

    Let's review the previous server-side services using Java or Python can all be implemented using PHP. A single language is easier to maintain. In the past, people had a one-sided understanding of the Web language. for example, multithreading and affairs can all change the way to achieve the same purpose.

    Link: http://blog.eood.cn/server-side-php-progress-program-best-practice

    SERVER ["argc"]. "\ n ";
  5. Echo

    Recently, comparing the features of PHP, Python, and Erlang found that PHP has many features that are not commonly used. Php cli can be used to implement many good applications. For example, search engine crawlers and long-running computing scripts can replace other languages for server O & M. This is even more powerful for people familiar with PHP.

    Recommended bitsCN: Top 10 tips for PHP developers to get twice the result with half the effort

    Why is PHP multi-process very good? The reason why most online game servers use multithreading rather than multi-process is that the process is more stable than the thread. In addition, multithreading is suitable for the application scenarios of multi-core servers, and can make full use of the multi-core computing capabilities. Process maintenance can use many operating system-level tools. Message Queue solves many thread communication problems. Therefore, PHP multi-process is suitable for computing-intensive applications on the server.

    According to a Vietnamese IT company, they have successfully used PHP background multi-process in enterprise-level applications such as legal document distribution and bank account amount processing.

    Using the background PHP process does not affect the server's simultaneous processing of web page requests. Once a backend process fails, it is easy to investigate the cause for recovery or remedy, so it is more robust. Different processes are isolated from each other, which is more efficient and can be used to dispatch various service processes in a unified manner.

    PHP is currently the most widely used Web development language, so using PHP for server applications can reduce costs. You can use existing personnel, existing configurations, or even code reuse. What kind of scenarios are more suitable for using PHP background multi-process? For example, the mail distribution, remote service calls, data aggregation, scheduled tasks, and computing result cache do not need to be returned immediately.

    PHP single process can achieve the goal in some places, and it is easier to implement. you do not need to consider the synchronization of the process or the data sharing issue. The php cli (sapi server api) command line interface can be used for CRON scheduled tasks and graphic interface programs (using the GTK Library ).

    Php cli example

          
          
    1. php -f test.php  
    2. php -r “echo time();”  
    3. php -R as python style 

    PHP reads command line parameters:

    ___FCKpd___1

    PHP command line interface standard input and output:

    ___FCKpd___2

    CRONJOB can run certain tasks on a regular basis, but it must prevent repeated operations. Create a lock file at the beginning and delete it at the end. Or use the ps command. Task queue can be implemented using MySQL, Key/VALUE database, or message queue.

    Process control functions:

    ___FCKpd___3

    PHP cannot throw exceptions to certain errors. how can we improve the fault tolerance of PHP multi-process applications?

    ◆ Processes can be monitored and reported after the dependent processes fail.

    ◆ Use CRONJOB to implement the monitoring process.

    ◆ Write the PID of the monitored process as a file.

    ◆ Regularly check whether the PID file exists. check ps-o pid = or file_exists ('/proc /').

    ◆ If the thread does not exist, restart the process.

    Let's review the previous server-side services using Java or Python can all be implemented using PHP. A single language is easier to maintain. In the past, people had a one-sided understanding of the Web language. for example, multithreading and affairs can all change the way to achieve the same purpose.

    Link: http://blog.eood.cn/server-side-php-progress-program-best-practice

    SERVER ["argv"] [0]. "\ n ";
  6. ?> 

PHP command line interface standard input and output:

___FCKpd___2

CRONJOB can run certain tasks on a regular basis, but it must prevent repeated operations. Create a lock file at the beginning and delete it at the end. Or use the ps command. Task queue can be implemented using MySQL, Key/VALUE database, or message queue.

Process control functions:

___FCKpd___3

PHP cannot throw exceptions to certain errors. how can we improve the fault tolerance of PHP multi-process applications?

◆ Processes can be monitored and reported after the dependent processes fail.

◆ Use CRONJOB to implement the monitoring process.

◆ Write the PID of the monitored process as a file.

◆ Regularly check whether the PID file exists. check ps-o pid = or file_exists ('/proc /').

◆ If the thread does not exist, restart the process.

Let's review the previous server-side services using Java or Python can all be implemented using PHP. A single language is easier to maintain. In the past, people had a one-sided understanding of the Web language. for example, multithreading and affairs can all change the way to achieve the same purpose.

Link: http://blog.eood.cn/server-side-php-progress-program-best-practice

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.