& Lt ;? Php #! Usrbinphp-q * DefineSTDINincaseifitisnotalreadydefinedbyPHPforsomereason * if (! Defined (& ldquo; STDIN & rdquo;) {define (& ldquo; STDIN & r
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.
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
php -f test.php php -r “echo time();” php -R as python style
PHP reads command line parameters:
PHP command line interface standard input and output:
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:
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 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.