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 (); "
- as python style
PHP Read command-line arguments:
-
- #!/usr/bin/php-q
- Echo "Test Arguments:";
- echo$_server["ARGC"]. "";
- echo$_server["argv"][0]. "";
- ?>
PHP command line interface standard input and output:
-
- #!/usr/bin/php-q
- /* Define STDIN in case if it isn't already defined by PHP for some reason */
- if (!defined ("STDIN")) {
- define ("STDIN", fopen(' php://stdin ', R '))
- }
-
- Echo "hello! What is your name (enter below): ";
- $strName = fread (STDIN,); //Read up to characters or a newline
- Echo ' Hello ', $strName , "";
- ?>
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 status 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.
http://www.bkjia.com/PHPjc/486090.html www.bkjia.com true http://www.bkjia.com/PHPjc/486090.html 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 ...