search
HomePHP FrameworkSwooleCan swoole set multiple timers?

Can swoole set multiple timers?

Dec 16, 2019 pm 01:52 PM
swoole

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);

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";
}

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment