Home > PHP Framework > Swoole > Can swoole set multiple timers?

Can swoole set multiple timers?

(*-*)浩
Release: 2019-12-16 13:52:17
Original
2309 people have browsed it

Can swoole set multiple timers?

First of all, in various business systems, the server is often required to scan relevant data in the background and trigger corresponding statistics, notifications and other operations.

For example, for a project management system, it is necessary to count the execution and expiration of each task, the progress of the entire project, etc. within a specific time every day, and make corresponding notifications based on the statistics; (Recommended learning: swoole video tutorial)

How to program such a scenario?

Using ordinary programming methods, automatic triggering and statistics cannot be achieved. Of course, the simple idea is beneficial to the cron job mechanism of the system. However, this method requires more human operation factors in terms of configuration and reliability.

Swoole is a PHP extension developed using C. It can implement a high-performance web server through PHP. At the same time, it also has built-in timer and task queue Task features. In this way, based on swoole, you can control the implementation at the program level and reduce dependence on external tools - independent message queue servers, scheduled task management tools, etc.

The power of swoole lies in the design of its process model, which not only solves asynchronous problems but also solves parallelism.

Usage is as follows:

swoole_server_addtimer($serv, 10);
Copy after login

The second parameter is the interval time of the timer, in seconds. The minimum granularity of the swoole timer is 1 second. Supports multiple timers. Note that there cannot be two timers with the same interval. After adding the timer, you need to write a callback function.

The specific code is as follows:

swoole_server_handler($serv, 'onTimer', my_OnTimer);
function my_OnTimer($serv, $interval)
{
  echo "Timer[$interval] is call\n";
}
Copy after login

Task module is used to do some asynchronous slow tasks, such as broadcasting in webim. Similar to node.js, if there are 100,000 connections and a broadcast is to be sent, it will loop 100,000 times. At this time, the program cannot do anything, cannot accept new connections, and cannot receive or send packets.

But swoole is different. After being thrown to the task process, your reactor and worker will still run. After the task is completed, the worker process will be notified asynchronously to tell it that the task has been completed.

The above is the detailed content of Can swoole set multiple timers?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template