本文整理汇总了PHP中swoole_server::task方法的典型用法代码示例。如果您正苦于以下问题:PHP swoole_server::task方法的具体用法?PHP swoole_server::task怎么用?PHP swoole_server::task使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类swoole_server
的用法示例。
在下文中一共展示了swoole_server::task方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: my_onReceive
function my_onReceive(swoole_server $serv, $fd, $from_id, $rdata)
{
$data = unserialize($rdata);
if (isset($data['cmd'])) {
switch ($data['cmd']) {
case 'get':
$s = microtime(true);
$res = $serv->taskwait($data, 0.5, 0);
echo "use " . (microtime(true) - $s) * 1000 . "ms\n";
$serv->send($fd, PHP_EOL . "get " . $res['key'] . ": " . $res['val']);
break;
case "set":
$serv->task($data, 0);
$serv->send($fd, "OK\n");
break;
case "del":
$serv->task($data, 0);
break;
case "reload":
break;
default:
echo "server:" . $data . PHP_EOL;
}
}
}
示例2: onReceive
public function onReceive(swoole_server $serv, $fd, $from_id, $data)
{
echo "Get Message from client {$fd}:{$data}:{$from_id}\n";
$params = array('fd' => $fd, 'file' => $data);
$id = $serv->task(json_encode($params));
echo "Continue Handle Worker {$id}\n";
}
示例3: onReceive
public function onReceive(swoole_server $serv, $fd, $from_id, $data)
{
echo "Get Message From Client {$fd}:{$data}\n";
$this->test = new Test();
var_dump($this->test);
$serv->task(serialize($this->test));
}
示例4: onReceive
public function onReceive(swoole_server $serv, $fd, $from_id, $data)
{
echo "Get Message From Client {$fd}:{$data}\n";
// send a task to task worker.
$param = array('fd' => $fd, 'source' => $data);
$serv->task(json_encode($param));
echo "Continue Handle Worker\n";
}
示例5: my_onReceive
function my_onReceive(swoole_server $serv, $fd, $from_id, $data)
{
if ($serv->exist($fd)) {
echo 'FD[' . $fd . '] exist' . PHP_EOL;
} else {
echo 'FD[' . $fd . '] not exist' . PHP_EOL;
}
$serv->task($data . '-' . $fd);
}
示例6: onReceive
public function onReceive(\swoole_server $serv, $fd, $from_id, $rdata)
{
$rdata = \Xz\Lib\Core\JsonProtocol::decode($rdata);
$data = array();
$data['data'] = $rdata;
$data['fd'] = $fd;
$serv->task($data);
return;
}
示例7: task
/**
* 投递一个任务
* @param \CatLib\Constraint\Network\IPacket $data 数据
* @param int $index 投递任务线程ID , -1随机
* @return bool
*/
public function task(IPacket $data, $index = -1)
{
if (isset($this->server->worker_id)) {
if (!$this->server->taskworker) {
$this->server->task($data->serialize(), $index);
return true;
}
}
return false;
}
示例8: onWorkerStart
public function onWorkerStart(\swoole_server $server, $worker_id)
{
if ($server->taskworker) {
swoole_set_process_name('switch_manage_task_worker');
$server->sendMessage('worker start.', $server->worker_id - 1);
} else {
swoole_set_process_name('switch_manage_event_worker');
if ($worker_id == 0) {
//防止tick被重复启动
$server->tick(self::INTERVAL_PING, function () use($server) {
$server->task('Ping');
});
}
}
}
示例9: onTimer
function onTimer(\swoole_server $serv, $interval)
{
$interfaces = \Swoole::$php->redis->sMembers(self::PREFIX);
if (!empty($interfaces)) {
foreach ($interfaces as $in) {
$key = self::PREFIX . "::" . $in;
$tmp = \Swoole::$php->redis->hGetAll($key);
// $this->log("{$this->worker_id} interfaces detials ".print_r($tmp,1));
if (!empty($tmp) and $tmp['enable_alert'] == 1 and !empty($tmp['alert_uids'])) {
$serv->task($tmp);
} else {
$this->log("{$this->worker_id} interface condition error " . var_export($tmp, 1));
}
}
}
}
示例10: runTask
/**
* 启动计划任务,支持异步处理
*/
public function runTask()
{
$oldErrorHandler = set_error_handler(array(__CLASS__, 'myErrorHandler'));
$config = DI()->config->get('app.Swoole.task');
$ip = isset($config['ip']) ? $config['ip'] : '127.0.0.1';
$port = isset($config['port']) ? $config['port'] : 9502;
$workerNum = isset($config['worker_num']) ? $config['worker_num'] : 4;
$serv = new swoole_server($ip, $port);
$serv->set(array('task_worker_num' => $workerNum));
$serv->on('Receive', function ($serv, $fd, $fromId, $data) {
$taskId = $serv->task($data);
DI()->logger->debug("asynctask({$taskId}) dispath in swoole", $data);
});
$serv->on('Task', function ($serv, $taskId, $fromId, $data) {
DI()->logger->debug("asynctask({$taskId}) start in swoole", $data);
$params = json_decode($data, TRUE);
if (!is_array($params)) {
$params = array();
}
DI()->request = new PhalApi_Request($params);
DI()->response = new Swoole_Response_Json();
try {
$phalapi = new PhalApi();
$rs = $phalapi->response();
$apiRs = $rs->getResult();
$serv->finish($rs->formatResult($apiRs));
} catch (Exception $ex) {
echo $ex->getTraceAsString();
DI()->logger->error("asynctask({$taskId}) exception in swoole", $ex->getMessage());
$serv->finish("Exception: " . $ex->getMessage());
//TODO 通知管理员
}
if (DI()->notorm) {
DI()->notorm->disconnect();
}
});
$serv->on('Finish', function ($serv, $taskId, $data) {
DI()->logger->debug("asynctask({$taskId}) finish in swoole", $data);
$serv->close($taskId);
});
$serv->start();
}
示例11: onReceive
/**
* 服务端接收数据
*
* @param $serv swoole_server对象
* @param $fd 连接的描述符
* @param $from_id reactor的id,无用
* @param $data 接收数据
*/
public function onReceive(swoole_server $serv, $fd, $from_id, $data)
{
//检测数据完整性
if (JsonProtocol::check($data) != 0) {
return;
}
$data = JsonProtocol::decode($data);
//接收参数
$class = $data['class'];
$method = $data['method'];
$params = $data['params'];
$startTime = $this->microtimeFloat();
// 判断类对应文件是否载入
if (!class_exists($class)) {
$include_file = ROOT_DIR . "Server/{$class}.php";
if (is_file($include_file)) {
require_once $include_file;
}
if (!class_exists($class)) {
$code = 404;
$msg = "class {$class} not found";
$result = array('code' => $code, 'msg' => $msg, 'data' => null);
$serv->send($fd, JsonProtocol::encode($result));
}
}
// 调用类的方法
try {
$ret = call_user_func_array(array(new $class(), $method), $params);
$code = $ret['code'];
$msg = $ret['msg'];
// 发送数据给客户端,调用成功,data下标对应的元素即为调用结果
$serv->send($fd, JsonProtocol::encode($ret));
} catch (Exception $e) {
// 发送数据给客户端,发生异常,调用失败
$code = $e->getCode() ? $e->getCode() : 500;
$msg = $e->getMessage();
$result = array('code' => $code, 'msg' => $msg, 'data' => $e);
$serv->send($fd, JsonProtocol::encode($result));
}
//请求数据统计,放在task执行
$executionTime = $this->microtimeFloat() - $startTime;
$report = array('class' => $class, 'method' => $method, 'params' => json_encode($params), 'code' => $code, 'msg' => $msg, 'execution' => $executionTime, 'time' => time());
$serv->task(json_encode($report));
}
示例12: onReceive
public function onReceive(swoole_server $serv, $fd, $from_id, $data)
{
$sql = array('sql' => 'Insert into Test values( pid = ?, name = ?)', 'param' => array(0, "'name'"), 'fd' => $fd);
$serv->task(json_encode($sql));
}
示例13: function
<?php
$serv = new swoole_server("127.0.0.1", 9501);
$serv->set(array('worker_num' => 1, 'task_worker_num' => 4));
$serv->on('Receive', function (swoole_server $serv, $fd, $from_id, $data) {
//AsyncTask
$data = trim($data);
//$data = str_repeat('A', 8192*100);
// if ($data == 'async')
//if(false)
if (true) {
$task_id = $serv->task($data, 0);
$serv->send($fd, "Dispath AsyncTask: id={$task_id}\n");
} else {
$res = $serv->taskwait($data, 10);
echo "Dispath SyncTask: result=" . $res . PHP_EOL;
}
//$serv->send($fd, "OK\n");
});
$serv->on('Task', function (swoole_server $serv, $task_id, $from_id, $data) {
echo "#{$serv->worker_id}\tonTask: [PID={$serv->worker_pid}]: task_id={$task_id}, data_len=" . strlen($data) . "." . PHP_EOL;
$serv->finish($data);
// return;
});
$serv->on('Finish', function (swoole_server $serv, $task_id, $data) {
echo "Task#{$task_id} finished, data_len=" . strlen($data) . PHP_EOL;
});
$serv->on('workerStart', function ($serv, $worker_id) {
global $argv;
if ($worker_id >= $serv->setting['worker_num']) {
swoole_set_process_name("php {$argv[0]}: task_worker");
示例14: function
<?php
$serv = new swoole_server("127.0.0.1", 9501);
$serv->set(array('worker_num' => 1, 'task_worker_num' => 2));
$serv->on('Receive', function (swoole_server $serv, $fd, $from_id, $data) {
//AsyncTask
$data = intval($data);
for ($i = 0; $i < $data; $i++) {
$tid = mt_rand(0, 1);
echo "data:{$i} to task: {$tid} " . PHP_EOL;
$serv->task($i, $tid);
}
});
$serv->on('Task', function (swoole_server $serv, $task_id, $from_id, $data) {
echo "onTask: [PID=" . posix_getpid() . "]: task_id={$task_id}, data_len=" . strlen($data) . "." . PHP_EOL;
sleep(10);
//$serv->finish($data);
echo 'finish' . PHP_EOL;
return;
});
$serv->on('Finish', function (swoole_server $serv, $task_id, $data) {
echo "Task#{$task_id} finished, data_len=" . strlen($data) . PHP_EOL;
});
$serv->on('Timer', function (swoole_server $serv, $time) {
echo "{$time} call" . PHP_EOL;
print_r($serv->stats());
});
$serv->on('workerStart', function ($serv, $worker_id) {
global $argv;
if ($worker_id >= $serv->setting['worker_num']) {
swoole_set_process_name("php {$argv[0]}: task_worker");
示例15: function
<?php
date_default_timezone_set('Asia/Shanghai');
$serv = new swoole_server('0.0.0.0', 9094, SWOOLE_PROCESS, SWOOLE_SOCK_UDP);
$serv->set(['worker_num' => 1, 'task_worker_num' => 4, 'daemonize' => false, 'log_file' => '/tmp/swoole_udp_server.log']);
$serv->on('receive', function (swoole_server $serv, $fd, $reactor_id, $data) {
$task_id = $serv->task($data);
$ymdhi = date('YmdHi');
swoole_async_write('/tmp/' . $ymdhi . '.txt', $data . "\n", -1);
});
$serv->on('task', function ($serv, $task_id, $from_id, $data) {
var_dump($serv->taskworker, $serv->worker_id, $serv->worker_pid);
\ran\Ran::run($serv->worker_id, $data);
return true;
});
$serv->on('finish', function () {
});
$serv->on('start', function ($serv) {
swoole_async_writefile('/tmp/swoole_master_pid.log', $serv->master_pid, function ($filename) {
});
// swoole_set_process_name("php swoole udp server master".$serv->master_pid);
});
$serv->on('workerStart', function ($serv, $worker_id) {
if ($worker_id >= $serv->setting['worker_num']) {
//超过worker_num,表示这是一个task进程
// swoole_set_process_name("php swoole udp server tasker ".$worker_id);
echo "{$worker_id} tasker start\n";
require './vendor/autoload.php';
} else {
echo "{$worker_id} worker start\n";
// swoole_set_process_name("php swoole udp server worker ".$worker_id);