babakbay/non-blocking-timer
Composer 安装命令:
composer require babakbay/non-blocking-timer
包简介
Non-blocking JavaScript-like setTimeout/setInterval for PHP.
README 文档
README
This timer uses a combination of a forked process (or a thread if Swoole v6+ is detected) together with POSIX signals to invoke a callback at a specified interval with millisecond-level accuracy — even when the PHP interpreter is suspended (e.g. during database or network queries, or system calls such as sleep(), curl_exec(), fread(), etc).
Unlike timers implemented using event loops or coroutines, this approach does not depend on the PHP event loop being "active". The timer continues to run independently in its own process or thread, ensuring precise timing under blocking conditions.
This brings setTimeout() and setInterval() style functionality to PHP!
Requirements
| Requirement | Minimum Version |
|---|---|
| PHP | 8.1+ |
| POSIX extension | Enabled |
pcntl extension |
Enabled |
(Optional) swoole |
v6.0+ for thread backend |
| OS | Linux / macOS (not supported on Windows) |
Usage
One-shot timeout (setTimeout)
use BabakBay\NonBlockingTimer\Timer; $timer = new Timer(); $timer->setTimeout(function() { echo "Executed after 1 second\n"; }, 1000); // Timer continues even during blocking operations while (true) { sleep(2); }
Repeating interval (setInterval)
$timer = new Timer(); $counter = 0; $timer->setInterval(function() use (&$counter) { echo "Tick " . ++$counter . "\n"; }, 500); while (true) { sleep(2); }
Clear timer
To stop and clear an active timer, use the clear() method.
$timer->clear();
Critical sections
You can disable callbacks during critical sections of your code using the callback() method.
$timer = new Timer(); $timer->setInterval(function() { echo "Timer callback\n"; }, 100); // Disable callbacks during critical section. By default once callbacks // are re-enabled, the callback will be invoked if there were any callback // invokations blocked. $timer->callbacks(false); performCriticalOperation(); $timer->callbacks(true); // Re-enable ca;;nacls and invoke any pending callback // If you do not want to invoke any pending callbacks, pass true as the // second argument. $timer->callbacks(false, true); // Do not pend callbacks performCriticalOperation(); $timer->callbacks(true); // Re-enable callbacks
Signal Masking
If you want to block timer signals from being received during a critical section of your code, use the signals() method. Any pending signals will be triggered once signals are re-enabled.
$timer = new Timer(); $timer->setInterval(function() { echo "Timer callback\n"; }, 100); // Block signals during system call $timer->signals(false); $result = curl_exec($ch); $timer->signals(true);
babakbay/non-blocking-timer 适用场景与选型建议
babakbay/non-blocking-timer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 11 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 babakbay/non-blocking-timer 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 babakbay/non-blocking-timer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 34
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2025-11-10