Home  >  Article  >  Backend Development  >  Why doesn't php support timers?

Why doesn't php support timers?

藏色散人
藏色散人Original
2019-05-22 10:57:004336browse

There are two common timers: one is executed periodically, for example, a report is issued at three o'clock in the morning every day; the other is executed after a specified time (once), for example, a daily login is issued five minutes after a member logs into the system. award. The two situations correspond to the cron and at commands in the shell, which are similar to the setInterval and setTimeout functions in JavaScript (strictly speaking, setInterval is executed periodically, and execution at a specified time point needs to be handled by itself).

Why doesn't php support timers?

PHP programmers who do web development should be familiar with the two timer functions in JavaScript. Returning to the PHP level is a bit dumbfounded:

There is sleep in PHP, but no (built-in) timer function is available. The sleep function can barely do it, but it will cause the process to be blocked and cannot do other things (or become unresponsive) during this period. Why doesn't PHP provide the timer function?

Reason

Personally believe that the essential reason why PHP cannot use timers in web development is the lack of a controllable resident memory operating environment. Two key points: first, resident memory, and second, controllable. In CGI mode, the process exits directly after executing the script and cannot be expected to run the task at a specified time; in PHP-FPM mode, the process (mostly) resides in memory but is uncontrollable.

Uncontrollable means that the process executing PHP is not affected by the PHP code, and the entry point and exit timing of the process are controlled by additional programs. For example, in FPM mode, the exit and die functions in the PHP script only interrupt the execution of the script and will not have any special impact on the process of executing the script (except for memory leaks). The script written by PHP developers is the execution body of the process. After execution, it is unloaded from the execution context of the process. In this case, the timing of executing the PHP script is still driven by the outside. If there is no external request, the PHP code will lie quietly on the hard disk, doing nothing, and it will be a scheduled task.

Since PHP is mainly oriented to web development, the execution mode of PHP is stable and reliable, and the development efficiency is fast. For example, omitting the resource release step can avoid a lot of workload and pitfalls in development. Think about some third-party library codes that change the time zone, character encoding, etc. and still not restore them. In a resident memory running environment, it will almost certainly cause problems with subsequent requests. However, in FPM mode, this pitfall is unintentionally smoothed out, saving a lot of debugging time and making a considerable contribution to programmers' ability to keep their hairline.

The above is the detailed content of Why doesn't php support timers?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn