Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement timed tasks in swoole

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "swoole how to achieve scheduled tasks", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "swoole how to achieve scheduled tasks" this article.

Methods: 1, use the "swoole_timer_after (function of execution)" statement to execute the task after the specified time; 2, use the "swoole_timer_tick (time, function of execution, callback function)" statement to set an interval clock timer to execute the task at a fixed time.

Operating environment of this tutorial: Windows10 system, Swoole4 version, DELL G3 computer

How to implement timed tasks in swoole

Swoole_timer_after

The function is executed after the specified time, and a later version of swoole-1.7.7 is required.

Swoole_timer_after (int $after_time_ms, mixed $callback_function)

The swoole_timer_after function is an one-time timer that is destroyed after execution. Unlike the sleep function provided by the PHP standard library, after is non-blocking. The call to sleep will cause the current process to enter blocking and will not be able to process new requests.

$after_time_ms specifies the time in milliseconds

Functions executed after the expiration of $callback_function time must be callable. The callback function does not accept any arguments

The maximum of $after_time_ms is 86400000.

Use the example

Swoole_timer_after (1000, function () {echo "timeout\ n";})

Swoole_timer_tick

Set an interval clock timer. Unlike the after timer, the tick timer will continue to fire until the call to swoole_timer_clear clears. Unlike swoole_timer_add, tick timers can have multiple timers with the same interval.

Int swoole_timer_tick (int $ms, mixed $callback, mixed $param = null)

$ms specifies the time in milliseconds

Functions executed after the expiration of $callback_function time must be callable. The callback function does not accept any arguments

$param callback parameter

The maximum of $ms is 86400000.

Tick timer is available in version 1.7.14 or later

Tick timer is about to replace swoole_timer_add

Callback function

The callback function triggered by the timer takes two parameters.

Function onTimer (int $timer_id, mixed $params = null)

The ID of the $timer_id timer, which can be used for swoole_timer_clear to clear this timer

User parameters passed in by $params from swoole_timer_tick

Use the example

Swoole_timer_tick (1000, function () {echo "timeout\ n";}); these are all the contents of the article "how swoole implements scheduled tasks". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report