PHP server-side multi-process programming Combat _php Tutorial

Source: Internet
Author: User
Tags php server php cli vars
Recently comparing PHP with Python, Erlang has found that PHP has a lot of features that people don't often use. Many good applications can be implemented with the PHP CLI. For example, a search engine crawler, long-running computing scripts, can completely replace other languages to do server operations. This is a powerful experience for people who are familiar with PHP.

Why is PHP multi-process good? The reason that most of the online game servers use multithreading instead of multiple processes is that the process is more stable than threads. and multithreading is suitable for the multi-core server application scenario, more capable of playing multi-core operations. Process maintenance can be used at many operating system level tools. The Message queue addresses much of the thread communication problem. Therefore, PHP multi-process is very suitable for server-side computing-intensive applications.

According to a Vietnam IT company, they successfully used the PHP background multi-process for the distribution of legal documents, processing the amount of bank accounts such as enterprise-class applications.

Using a background PHP process does not affect the server's request to process Web pages at the same time. This background process is more robust when it is easy to investigate the cause for recovery or remediation once a failure occurs. Different processes are isolated from each other, more efficient, and can be uniformly dispatched for each service process.

PHP is currently the most widely used web development language, so using PHP to do server-side applications can reduce costs. You can use existing people, existing configurations, and even code reuse. What kind of scene is more suitable for PHP background multi-process? such as the distribution of messages, the invocation of remote services, aggregation of data, scheduled tasks, and the caching of computed results. These do not need to be returned immediately.

PHP single process In some places completely can achieve the purpose, and easier to implement, regardless of the process synchronization problem, do not consider the data sharing problem. The PHP CLI (SAPI SERVER API) command line interface can be used to do cron scheduling tasks, graphical interface programs (using the GTK Library).

PHP CLI Example

  1. Php-f test.php
  2. Php-r " Echo Time (); "
  3. as python style

PHP Read command-line arguments:

  1. #!/usr/bin/php-q
  2. Echo "Test Arguments:";
  3. echo$_server["ARGC"]. "";
  4. echo$_server["argv"][0]. "";
  5. ?>

PHP command line interface standard input and output:

  1. #!/usr/bin/php-q
  2. /* Define STDIN in case if it isn't already defined by PHP for some reason */
  3. if (!defined ("STDIN")) {
  4. define ("STDIN", fopen(' php://stdin ', R '))
  5. }
  6. Echo "hello! What is your name (enter below): ";
  7. $strName = fread (STDIN,); //Read up to characters or a newline
  8. Echo ' Hello ', $strName , "";
  9. ?>

Cronjob can run certain tasks on a timed basis, but prevent them from running repeatedly. At the beginning, a lock file is created and deleted at the end. or use the PS command to handle it. The task queue can be implemented with MySQL, or key/value database, or message queue.

Process Control related functions:

  1. Process Control Extensions
  2. Pcntl_fork ()
  3. Posix_setsid ()
  4. Posix_kill
  5. Pcntl_wait
  6. Pcntl_signal
  8. SIGTERM; System shutdown, kill
  9. SIGINT; Sent by CTRL + C
  10. SIGKILL (uncatchable); unresponsive, kill-9
  11. SIGCHLD; Child status Change
  12. SIGSTP; Sent by Ctrl + Z
  13. Sigcont; Resume from Stop, FG

PHP can not throw exceptions to some errors, how to improve the fault tolerance of PHP multi-process applications?

Processes can be monitored, relying on process failures after reporting.

Implement the monitoring process with cronjob.

The monitored process PID is written as a file.

Check the PID file periodically for check Ps-o pid= or file_exists ('/proc/').

If the thread does not have a restart process.

Reviewing server-side services that were previously done in Java or Python can be implemented in PHP. A single language is easier to maintain. In the past, people's understanding of Web language is very one-sided, such as multithreading, transactions, these things can change the way to achieve the same purpose. true techarticle recently comparing PHP with Python, Erlang has found that PHP has a lot of features that people don't often use. Many good applications can be implemented with the PHP CLI. such as the search engine crawler, long-term operation ...

  • Related Article

    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: 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.