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
Php-f test.php Php-r "echo time ();" Php-r as Python style
PHP Read command-line arguments:
PHP command line interface standard input and output:
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:
Process Control Extensions pcntl_fork () posix_setsid () posix_kill pcntl_wait pcntl_signal SIGHUP SIGTERM; System shutdown, kill SIGINT; sent by CTRL + C SIGKILL (uncatchable); unresponsive, kill-9 SIGCHLD; child s Tatus change SIGSTP; sent by Ctrl + Z 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.
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.