本文整理汇总了PHP中swoole_server::finish方法的典型用法代码示例。如果您正苦于以下问题:PHP swoole_server::finish方法的具体用法?PHP swoole_server::finish怎么用?PHP swoole_server::finish使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类swoole_server
的用法示例。
在下文中一共展示了swoole_server::finish方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: my_onTask
function my_onTask(swoole_server $serv, $task_id, $from_id, $data)
{
static $datas = array();
if (isset($data['cmd'])) {
switch ($data['cmd']) {
case 'get':
$key = $data['key'];
$val = isset($datas[$key]) ? $datas[$key] : "";
$serv->finish(array('key' => $key, 'val' => $val));
break;
case "set":
$key = $data['key'];
$val = $data['val'] . "_" . $from_id;
$datas[$key] = $val;
return;
break;
case "del":
$key = $data['key'];
if (isset($datas[$key])) {
unset($datas[$key]);
}
break;
case "task":
$key = $data['key'];
echo "Do task " . $key . PHP_EOL;
break;
}
}
echo "AsyncTask[PID=" . posix_getpid() . "]: task_id={$task_id}." . PHP_EOL;
// $serv->finish("OK");
}
示例2: process
public function process(swoole_server $serv, $taskId, $fromId, $data)
{
// model load
$this->defaultModel->sayHello();
// library load
$this->mcurl->isEnable();
$serv->finish('sync ok!');
}
示例3: __construct
public function __construct()
{
$serv = new swoole_server("127.0.0.1", 9504);
$serv->set(array('worker_num' => 1, 'task_worker_num' => 8, 'db_uri' => 'mysql:host=127.0.0.1;dbname=test', 'db_user' => 'root', 'db_passwd' => 'root', 'daemonize' => true));
$serv->on('Receive', function ($serv, $fd, $from_id, $data) {
$result = $serv->taskwait($data);
if ($result !== false) {
list($status, $db_res) = explode(':', $result, 2);
if ($status == 'OK') {
$serv->send($fd, var_export(unserialize($db_res), true) . "\n");
} else {
$serv->send($fd, $db_res);
}
} else {
$serv->send($fd, "Error. Task timeout\n");
}
});
$serv->on('Task', function ($serv, $task_id, $from_id, $sql) {
static $link = null;
if ($link == null) {
$link = new PDO($serv->setting['db_uri'], $serv->setting['db_user'], $serv->setting['db_passwd']);
if (!$link) {
$link = null;
$serv->finish("ER: connect database failed.");
}
}
$result = $link->query($sql);
if (!$result) {
$serv->finish("ER: query error");
}
$data = $result->fetchAll();
$serv->finish(serialize($data));
});
$serv->on('Finish', function ($serv, $data) {
echo "AsyncTask[{$task_id}] Finish: {$data}" . PHP_EOL;
});
$serv->start();
}
示例4: OnTask
/**
* TaskWorker收到任务时触发
*
* @param \swoole_server $server
* @param int $task_id
* @param int $from_id
* @param mixed $param
*/
public function OnTask(\swoole_server $server, $task_id, $from_id, $param)
{
//Log::Debug("Task accept task_id[$task_id] from_id[$from_id]");
Event::Listen('TaskReceived', [&$server, &$task_id, &$from_id, &$param]);
//if($server->worker_id % 2 == 0)
//{
// sleep(2);
// if(rand(0, 100) > 50)
// {
// //return;
// }
//}
Event::Listen('TaskFinished', [&$server, &$task_id, &$from_id, &$param]);
$server->finish('finish');
}
示例5: trim
//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");
} else {
swoole_set_process_name("php {$argv[0]}: worker");
}
});
$serv->start();
示例6: my_onTask
function my_onTask(swoole_server $serv, $task_id, $from_id, $data)
{
static $datas = array();
$data = json_decode($data, true);
if (isset($data['cmd'])) {
switch ($data['cmd']) {
case 'get':
$key = $data['key'];
$result = isset($datas[$key]) ? $datas[$key] : "";
$serv->finish($result);
break;
case "set":
$key = $data['key'];
$val = $data['val'] . "_" . $from_id;
$datas[$key] = $val;
return;
break;
case "del":
$key = $data['key'];
if (isset($datas[$key])) {
unset($datas[$key]);
}
break;
}
}
//echo "AsyncTask[PID=".posix_getpid()."]: task_id=$task_id.".PHP_EOL;
//$serv->finish("OK");
}
示例7: my_onTask
function my_onTask(swoole_server $serv, $task_id, $from_id, $data)
{
echo "AsyncTask[PID=" . posix_getpid() . "]: task_id={$task_id}." . PHP_EOL;
$serv->finish("OK");
}
示例8: switch
switch ($cmd) {
case 'fetch':
$crawler_server->task($data, 0);
$crawler_server->send($fd, "OK\n");
break;
default:
echo "error cmd \n";
}
});
$crawler_server->on('task', function (swoole_server $crawler_server, $task_id, $from_id, $data) {
echo 'task start time--' . date('Y-m-d H:i:s') . "\n";
echo 'tast_id :' . $task_id . "\n";
echo 'client--from_id:' . $from_id . "\n";
$driver = isset($data['driver']) ? $data['driver'] : '';
$method = isset($data['method']) ? $data['method'] : 'fetch';
$object = isset($data['object']) ? unserialize($data['object']) : '';
$class = isset($data['class']) ? $data['class'] : 'Crawl';
if (!$class && !$object || !$method) {
$crawler_server->finish("error callback\n");
}
if ($object) {
call_user_func_array(array($object, $method), array($data['data'], array('driver' => $driver)));
} else {
call_user_func_array("{$class}::{$method}", array($data['data'], array('driver' => $driver)));
}
$crawler_server->finish("OK\n");
});
//添加监控进程
$crawler_server->addProcess($crawler_monitor);
//server开始
$crawler_server->start();
示例9: onTask
/**
* 执行任务
*
* @access public
* @return void
**/
public function onTask(swoole_server $server, $task_id, $from_id, $data)
{
$object = isset($data['object']) && $data['object'] ? unserialize($data['object']) : $this;
$method = isset($data['method']) && $data['method'] ? $data['method'] : 'callback';
$class = isset($data['class']) && $data['class'] ? $data['class'] : '';
if (isset($data['cmd'])) {
switch ($data['cmd']) {
case "send":
case "tick":
if ($object) {
call_user_func_array(array($object, $method), array());
} elseif ($class) {
call_user_func_array("{$class}::{$method}", $data['args']);
}
break;
default:
$server->finish("ERROR CMD");
}
}
echo "AsyncTask[PID=" . posix_getpid() . "]: task_id={$task_id}." . PHP_EOL;
$server->finish("OK");
}
示例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: switch
switch ($cmd) {
case 'fetch':
$parser_server->task($data, 0);
$parser_server->send($fd, "OK\n");
break;
default:
echo "error cmd \n";
}
});
$parser_server->on('task', function (swoole_server $parser_server, $task_id, $from_id, $data) {
echo 'task start time--' . date('Y-m-d H:i:s') . "\n";
echo 'tast_id :' . $task_id . "\n";
echo 'client--from_id:' . $from_id . "\n";
$driver = isset($data['driver']) ? $data['driver'] : '';
$method = isset($data['method']) ? $data['method'] : 'fetch';
$object = isset($data['object']) ? unserialize($data['object']) : '';
$class = isset($data['class']) ? $data['class'] : 'Parse';
if (!$class && !$object || !$method) {
$crawler_server->finish("error callback\n");
}
if ($object) {
call_user_func_array(array($object, $method), array($data['data'], array('driver' => $driver)));
} else {
call_user_func_array("{$class}::{$method}", array($data['data'], array('driver' => $driver)));
}
$parser_server->finish("OK\n");
});
//添加监控进程
$parser_server->addProcess($parser_monitor);
//server启动
$parser_server->start();
示例12: function
* 执行完成后调用$serv->finish()返回结果。
* finish操作是可选的,也可以不返回任何结果
*
*/
$serv = new swoole_server("127.0.0.1", 9501);
//设置异步任务的工作进程数量
$serv->set(array('task_worker_num' => 4));
//监听连接进入事件
$serv->on('connect', function ($serv, $fd) {
echo "Client: Connect.\n";
});
$serv->on('receive', function ($serv, $fd, $from_id, $data) {
//投递异步任务
$task_id = $serv->task($data);
echo "Dispath AsyncTask: id={$task_id}\n";
});
//处理异步任务
$serv->on('task', function ($serv, $task_id, $from_id, $data) {
echo "New AsyncTask[id={$task_id}]" . PHP_EOL;
//返回任务执行的结果
$serv->finish("{$data} -> OK");
});
//处理异步任务的结果
$serv->on('finish', function ($serv, $task_id, $data) {
echo "AsyncTask[{$task_id}] Finish: {$data}" . PHP_EOL;
});
//监听连接关闭事件
$serv->on('close', function ($serv, $fd) {
echo "Client: Close.\n";
});
$serv->start();