当前位置: 首页 > 工具软件 > php-timer > 使用案例 >

php定时器没起作用,swoole_timer_tick定时器创建成功,但是不运行

干稳
2023-12-01

### 问题描述

在进程池\Swoole\Process\Pool中使用swoole_timer_tick创建一个定时器,创建的位置为onWorkerStart回调中,创建后能得到定时器ID,并且调试信息为[2020-06-24 16:37:14 @15451.0]TRACEswTimer_add(:198): id=1, exec_msec=1000, msec=1000, round=0, exist=1,但是定时器并没有得到运行,并且没有抛出任何报错信息,请大神帮忙定位一下问题。

### Swoole版本,PHP版本,以及操作系统版本信息

swoole4.5.2

PHP 7.1.4

Ubuntu 16.04.6 LTS

### 相关代码

```php

namespace app\core\controller;

use Swoole\Server;

use think\Controller;

use \Swoole\Database\PDOConfig;

use \Swoole\Database\PDOPool;

use think\Db;

class Processtask extends Controller{

public $process_pool;

public $pair_id;

public function __construct(){

$this->process_pool = new \Swoole\Process\Pool(4);

$this->process_pool->on('workerStart',[$this,'onWorkerStart']);

$this->process_pool->on('WorkerStop',[$this,'onWorkerStop']);

$this->process_pool->set(["log_level"=>SWOOLE_LOG_DEBUG]);

swoole_set_process_name('coin_task');

$this->process_pool->start();

}

public function indexAction(){

var_dump("我是index");

}

public function onWorkerStart($pool, $workerId){

$running = true;

pcntl_signal(SIGTERM, function () use (&$running) {

$running = false;

});

switch ($workerId){

case 0:

swoole_set_process_name('coin_task_deposit_order_0');

var_dump("下单进程{$workerId}启动");

model("api/UserOrder")->depositOrderToOrder($running,$workerId);

break;

case 1:

swoole_set_process_name('coin_task_deposit_order_1');

var_dump("下单进程{$workerId}启动");

model("api/UserOrder")->depositOrderToOrder($running,$workerId);

break;

case 2:

cache("swoole_time_out",11,25);

swoole_set_process_name('coin_task_deposit_order_2');

var_dump("下单进程{$workerId}启动");

model("api/UserOrder")->depositOrderToOrder($running,$workerId);

break;

case 3:

swoole_set_process_name('coin_task_master_clear_3');

\Swoole\Timer::tick(1000,function($timer_id){

var_dump("定时器id为:");

});

//3000ms后执行此函数

swoole_timer_after(3000, function ($timer_id) {

file_put_contents(ROOT_PATH."runtime/log/".date("Ym")."/".date("d")."_start.log","定时器id为:".$timer_id,FILE_APPEND);

echo "after 3000ms.\n";

});

file_put_contents(ROOT_PATH."runtime/log/".date("Ym")."/".date("d")."_start.log","定时器id为:",FILE_APPEND);

$who_are_you = swoole_timer_tick(2000,[$this,"testTimers"]);

$pool->groupError_timer_id = $who_are_you;

var_dump("成团结算进程启动");

model("api/UserOrder")->groupClear($running,$workerId);

default:

break;

}

}

public function testTimers($timer_id){

var_dump("定时器id为:");

file_put_contents(ROOT_PATH."runtime/log/".date("Ym")."/".date("d")."_start.log","定时器id为:".$timer_id,FILE_APPEND);

}

public function onWorkerStop($pool, $workerId){

switch ($workerId){

case 0:

var_dump("下单进程{$workerId}停止");

break;

case 1:

var_dump("下单进程{$workerId}停止");

break;

case 2:

var_dump("下单进程{$workerId}停止");

break;

case 3:

$clear_status = \Swoole\Timer::clear($pool->groupError_timer_id);

var_dump("成团失败定时器".$pool->groupError_timer_id."结束,结束状态为:".$clear_status);

var_dump("成团结算进程停止");

break;

default:

break;

}

}

}

```

### 你期待的结果是什么?实际看到的错误信息又是什么?

期待结果为启动后能打印出定时器中的输出内容,结果为没有任何信息被打印出来,系统一切运行正常,打印出来的都为我正常的业务输出内容,如下:

[2020-06-24 17:07:06 @16003.0]TRACEphp_swoole_reactor_init(:254): init reactor

[2020-06-24 17:07:06 @16003.0]TRACEswTimer_add(:198): id=1, exec_msec=1000, msec=1000, round=0, exist=1

[2020-06-24 17:07:06 @16003.0]TRACEswTimer_add(:198): id=2, exec_msec=3000, msec=3000, round=0, exist=2

[2020-06-24 17:07:06 @16003.0]TRACEswTimer_add(:198): id=3, exec_msec=2000, msec=2000, round=0, exist=3

string(24) "成团结算进程启动"

string(19) "下单进程0启动"

string(19) "下单进程1启动"

string(19) "下单进程2启动"

string(31) "下单进程2没有取到数据"

string(36) "成团结算进程没有取到数据"

string(31) "下单进程1没有取到数据"

string(31) "下单进程0没有取到数据"

string(31) "下单进程2没有取到数据"

string(36) "成团结算进程没有取到数据"

string(31) "下单进程1没有取到数据"

string(31) "下单进程0没有取到数据"

 类似资料: