本文整理匯總了PHP中swoole_timer_clear函數的典型用法代碼示例。如果您正苦於以下問題:PHP swoole_timer_clear函數的具體用法?PHP swoole_timer_clear怎麽用?PHP swoole_timer_clear使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了swoole_timer_clear函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: run
public function run($worker)
{
swoole_timer_tick(1000, function ($timer_id) {
static $index = 0;
$index = $index + 1;
$this->process->push("Hello");
var_dump($index);
if ($index == 10) {
swoole_timer_clear($timer_id);
}
});
}
示例2: clean
public function clean($conn)
{
if (isset($conn->timeoutId)) {
swoole_timer_clear($conn->timeoutId);
unset($conn->timeoutId);
}
}
示例3: timer
function timer($id)
{
echo "Exec: #{$id}\n";
//如果是After定時器,直接刪除即可
if (isset($afterTimers[$id])) {
unset($afterTimers[$id]);
}
$r = rand(0, 100);
//刪除After定時器,幾率:5%
if ($r > 95) {
$timerId = array_rand(G::$afterTimers);
G::log("ClearTick #{$timerId}");
swoole_timer_clear($timerId);
unset(G::$afterTimers[$timerId]);
} elseif ($r > 90) {
$timerId = array_rand(G::$tickTimers);
G::log("ClearTick #{$timerId}");
swoole_timer_clear($timerId);
unset(G::$tickTimers[$timerId]);
} elseif ($r > 88) {
G::newAfterTimer();
} elseif ($r > 86) {
G::newTickTimer();
}
}
示例4: send
public function send(callable $callback)
{
$client = new \swoole_client(SWOOLE_SOCK_UDP, SWOOLE_SOCK_ASYNC);
$client->on("connect", function ($cli) {
$cli->send($this->data);
});
$client->on('close', function ($cli) {
});
$client->on('error', function ($cli) use($callback) {
$cli->close();
call_user_func_array($callback, array('r' => 1, 'key' => $this->key, 'calltime' => $this->calltime, 'error_msg' => 'conncet error'));
});
$client->on("receive", function ($cli, $data) use($callback) {
$cli->close();
$this->calltime = microtime(true) - $this->calltime;
swoole_timer_clear($this->timer);
call_user_func_array($callback, array('r' => 0, 'key' => $this->key, 'calltime' => $this->calltime, 'data' => $data));
});
if ($client->connect($this->ip, $this->port, $this->timeout)) {
$this->calltime = microtime(true);
if (floatval($this->timeout) > 0) {
$this->timer = swoole_timer_after(floatval($this->timeout) * 1000, function () use($client, $callback) {
$client->close();
\SysLog::error(__METHOD__ . " TIMEOUT ", __CLASS__);
$this->calltime = microtime(true) - $this->calltime;
call_user_func_array($callback, array('r' => 2, 'key' => '', 'calltime' => $this->calltime, 'error_msg' => 'timeout'));
});
}
}
}
示例5: timeout
function timeout($tm)
{
echo time() . " Timeout #{$tm}\n";
if ($tm == 3) {
global $timer4;
swoole_timer_clear($timer4);
}
}
示例6: close
public function close()
{
foreach ($this->pool as $conn) {
if (isset($conn->timer)) {
swoole_timer_clear($conn->timer);
unset($conn->timer);
}
if ($conn->isConnected()) {
$conn->close();
}
}
}
示例7: sendUsers
private function sendUsers()
{
$users = $this->getAllUsers();
foreach ($users as $user) {
if (isset($this->getUpdateUsers[$user])) {
$getUpdateUsers = $this->getUpdateUsers[$user];
unset($this->getUpdateUsers[$user]);
swoole_timer_clear($getUpdateUsers->timer);
$getUpdateUsers->completer->complete($users);
}
}
}
示例8: run
public function run()
{
$this->current_num = $this->min_worker_num;
for ($i = 0; $i < $this->current_num; $i++) {
$process = new swoole_process(array($this, 'task_run'), false, 2);
$pid = $process->start();
$this->process_list[$pid] = $process;
$this->process_use[$pid] = 0;
}
foreach ($this->process_list as $process) {
swoole_event_add($process->pipe, function ($pipe) use($process) {
$data = $process->read();
var_dump($data);
$this->process_use[$data] = 0;
});
}
swoole_timer_tick(1000, function ($timer_id) {
static $index = 0;
$index = $index + 1;
$flag = true;
foreach ($this->process_use as $pid => $used) {
if ($used == 0) {
$flag = false;
$this->process_use[$pid] = 1;
$this->process_list[$pid]->write($index . "Hello");
break;
}
}
if ($flag && $this->current_num < $this->max_worker_num) {
$process = new swoole_process(array($this, 'task_run'), false, 2);
$pid = $process->start();
$this->process_list[$pid] = $process;
$this->process_use[$pid] = 1;
$this->process_list[$pid]->write($index . "Hello");
$this->current_num++;
}
var_dump($index);
if ($index == 10) {
foreach ($this->process_list as $process) {
$process->write("exit");
}
swoole_timer_clear($timer_id);
$this->process->exit();
}
});
}
示例9: work
public function work()
{
//是否有隊列任務,有的話給worker進程發消息
foreach ($this->workers as $pid => $worker) {
\Log::info("隊列worker{$pid}啟動", [], 'tube.work');
$data = $worker['tube'];
if ($data) {
$worker['process']->write($data);
}
}
swoole_timer_tick(5000, function ($timerId) {
if (!$this->pheanstalk->getConnection()->isServiceListening()) {
//現在是一旦隊列服務器崩潰的話,處理隊列的主進程將退出。當然可以設置成等待,知道隊列服務器恢複,隻要將下列代碼注釋
\Log::emergency("隊列服務器崩潰了!TubeTick監聽器退出", [], 'tube.tick');
swoole_timer_clear($timerId);
return;
}
});
}
示例10: run
public function run()
{
for ($i = 0; $i < $this->worker_num; $i++) {
$this->process_list[$i] = new swoole_process(array($this, 'task_run'), false, false);
$this->process_list[$i]->useQueue();
$this->process_list[$i]->start();
}
swoole_timer_tick(1000, function ($timer_id) {
static $index = 0;
$index = $index + 1;
$this->process->push($index . "Hello");
var_dump($index);
if ($index == 10) {
$this->process->push("exit");
$this->process->push("exit");
$this->process->push("exit");
swoole_timer_clear($timer_id);
}
});
}
示例11: loop
public static function loop($timer_id)
{
\SysLog::info(__METHOD__, __CLASS__);
/*
遍曆自己的數組,發現時間超過預定時間段,且該IO的狀態依然是未回包狀態,則走超時邏輯
*/
if (empty(self::$event)) {
\SysLog::info(__METHOD__ . " del event swoole_timer_clear == {$timer_id} ", __CLASS__);
swoole_timer_clear($timer_id);
}
foreach (self::$event as $socket => $e) {
$now = microtime(true);
\SysLog::debug(__METHOD__ . " key == {$socket} now == {$now} timeout == " . $e['timeout'], __CLASS__);
if ($now > $e['timeout']) {
self::del($socket);
$cli = $e['cli'];
$cli->close();
call_user_func_array($e['callback'], $e['params']);
}
}
}
示例12: onReceive
/**
* 對外提供接口
*
* @param $server swoole_server對象
* @param $fd TCP客戶端連接的文件描述符
* @param $from_id TCP連接所在的Reactor線程ID
* @param $data 收到的數據內容
*/
public function onReceive(swoole_server $server, $fd, $from_id, $data)
{
if (empty($data)) {
// 發送數據給客戶端,請求包錯誤
$data = array('code' => 500, 'msg' => '非法請求', 'data' => null);
$server->send($fd, json_encode($data));
}
//局域網管理
$udpClient = $server->connection_info($fd, $from_id);
if ($udpClient['server_port'] == '9504') {
echo $data . PHP_EOL;
switch ($data) {
case 'stop':
echo '服務器關閉: ' . date('Y-m-d H:i:s') . PHP_EOL;
$server->shutdown();
$server->send($fd, '服務器關閉成功');
break;
case 'reload':
echo 'Worker進程重啟: ' . date('Y-m-d H:i:s') . PHP_EOL;
$server->reload();
$server->send($fd, '服務器Worker重啟成功');
break;
default:
$server->send($fd, '非法請求');
break;
}
} else {
$data = json_decode($data, true);
//任務數據
$list = $data['list'];
//請求類型
$type = $data['type'];
switch ($type) {
//添加定時器
case 'add':
//添加定時器
$timeInterval = $list['s_interval'];
$taskId = $list['s_id'];
$timerId = $server->tick($timeInterval, array($this, 'onTimer'), $list);
//更新任務
Task::updateTimer($taskId, $timerId);
$data = array('code' => 200, 'msg' => '添加定時器成功', 'data' => null);
$server->send($fd, json_encode($data));
break;
//修改定時器
//修改定時器
case 'edit':
//修改url,定時器間隔時間
$data = array('code' => 200, 'msg' => '修改定時器成功', 'data' => null);
$server->send($fd, json_encode($data));
break;
//刪除定時器
//刪除定時器
case 'del':
echo '刪除定時器: ' . date('Y-m-d H:i:s') . PHP_EOL;
//任務ID
$taskId = $list['s_id'];
//定時器ID
$timerId = $list['s_timerId'];
//刪除定時器
swoole_timer_clear($timerId);
//刪除任務
Task::delTask($taskId);
$data = array('code' => 200, 'msg' => '刪除定時器成功', 'data' => null);
$server->send($fd, json_encode($data));
break;
default:
$data = array('code' => 500, 'msg' => '非法請求', 'data' => null);
$server->send($fd, json_encode($data));
break;
}
}
}
示例13: swoole_timer_tick
<?php
swoole_timer_tick(2000, function ($id) {
echo "tick-1 2000ms\n";
var_dump($id);
});
usleep(500000);
swoole_timer_tick(2000, function ($id, $params) {
echo "tick-2 2000ms\n";
var_dump($id, $params);
swoole_timer_clear($id);
}, 2);
示例14: asyncSendAndReceive
protected function asyncSendAndReceive($request, $use)
{
$self = $this;
$client = new \swoole_client($this->type, SWOOLE_SOCK_ASYNC);
$buffer = "";
$len = "";
$client->on("connect", function ($cli) use($self, $request, $use) {
if (!$self->send($cli, $request)) {
$self->sendAndReceiveCallback('', new \Exception(socket_strerror($cli->errCode)), $use);
}
});
$client->on("error", function ($cli) use($self, $use) {
$self->sendAndReceiveCallback('', new \Exception(socket_strerror($cli->errCode)), $use);
});
$client->on("receive", function ($cli, $data) use($self, &$buffer, &$len, $use) {
do {
if (count($buffer) == 0 || is_string($len)) {
$left = 4 - strlen($len);
if (strlen($data) < $left) {
$len .= $data;
return;
}
$len .= substr($data, 0, $left);
$len = unpack("N", $len);
$len = $len[1];
$n = strlen($data) - $left;
} else {
$left = 0;
$n = strlen($data);
}
if ($n == 0) {
$buffer = "";
return;
}
if ($len == $n) {
$response = $buffer . substr($data, $left);
$buffer = "";
$len = "";
try {
$self->sendAndReceiveCallback($response, null, $use);
} catch (\Exception $e) {
}
swoole_timer_clear($cli->timer);
$cli->close();
return;
}
if ($len > $n) {
$buffer .= substr($data, $left);
$len -= $n;
return;
}
$response = $buffer . substr($data, $left, $len);
$buffer = "";
$data = substr($data, $left + $len);
$len = "";
try {
$self->sendAndReceiveCallback($response, null, $use);
} catch (\Exception $e) {
}
} while (true);
});
$client->on("close", function ($cli) {
});
$client->connect($this->host, $this->port);
$client->timer = swoole_timer_after($this->timeout, function () use($client) {
$client->close();
});
}
示例15: clearTimer
public function clearTimer()
{
\swoole_timer_clear($this->_timerId);
}