本文整理汇总了PHP中swoole_server::taskwait方法的典型用法代码示例。如果您正苦于以下问题:PHP swoole_server::taskwait方法的具体用法?PHP swoole_server::taskwait怎么用?PHP swoole_server::taskwait使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类swoole_server
的用法示例。
在下文中一共展示了swoole_server::taskwait方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: __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();
}
示例3: my_onReceive
function my_onReceive(swoole_server $serv, $fd, $from_id, $data)
{
my_log("Worker#{$serv->worker_pid} Client[{$fd}@{$from_id}]: received: {$data}");
$cmd = trim($data);
if ($cmd == "reload") {
$serv->reload();
} elseif ($cmd == "task") {
$task_id = $serv->task("task-" . $fd);
echo "Dispath AsyncTask: id={$task_id}\n";
} elseif ($cmd == "taskwait") {
$result = $serv->taskwait("taskwait");
if ($result) {
$serv->send($fd, "taskwaitok");
}
echo "SyncTask: result=" . var_export($result, true) . "\n";
} elseif ($cmd == "hellotask") {
$serv->task("hellotask");
} elseif ($cmd == "sendto") {
$serv->sendto("127.0.0.1", 9999, "hello world");
} elseif ($cmd == "close") {
$serv->send($fd, "close connection\n");
$result = $serv->close($fd);
} elseif ($cmd == "info") {
$info = $serv->connection_info(strval($fd), $from_id);
var_dump($info["remote_ip"]);
$serv->send($fd, 'Info: ' . var_export($info, true) . PHP_EOL);
} elseif ($cmd == 'proxy') {
$serv->send(1, "hello world\n");
} elseif ($cmd == 'sleep') {
sleep(10);
} elseif ($cmd == 'foreach') {
foreach ($serv->connections as $fd) {
echo "conn : {$fd}\n";
}
return;
} elseif ($cmd == 'tick') {
$serv->tick(2000, function ($id) {
echo "tick #{$id}\n";
});
} elseif ($cmd == 'addtimer') {
$serv->addtimer(3000);
} elseif ($cmd == "list") {
$start_fd = 0;
echo "broadcast\n";
while (true) {
$conn_list = $serv->connection_list($start_fd, 10);
if (empty($conn_list)) {
echo "iterates finished\n";
break;
}
$start_fd = end($conn_list);
var_dump($conn_list);
}
} elseif ($cmd == "list2") {
foreach ($serv->connections as $con) {
var_dump($serv->connection_info($con));
}
} elseif ($cmd == "stats") {
$serv_stats = $serv->stats();
$serv->send($fd, 'Stats: ' . var_export($serv_stats, true) . PHP_EOL);
} elseif ($cmd == "broadcast") {
broadcast($serv, $fd, "hello from {$fd}\n");
} elseif ($cmd == "error") {
hello_no_exists();
} elseif ($cmd == "exit") {
exit("worker php exit.\n");
} elseif (substr($cmd, 0, 5) == "close") {
$close_fd = substr($cmd, 6);
$serv->close($close_fd);
} elseif ($cmd == "shutdown") {
$serv->shutdown();
} elseif ($cmd == 'sendbuffer') {
$buffer = G::getBuffer($fd);
$buffer->append("hello\n");
$serv->send($fd, $buffer);
} else {
$ret = $serv->send($fd, 'Swoole: ' . $data, $from_id);
var_dump($ret);
//$serv->close($fd);
}
//echo "Client:Data. fd=$fd|from_id=$from_id|data=$data";
// $serv->after(
// 800, function () {
// echo "hello";
// }
// );
//swoole_server_send($serv, $other_fd, "Server: $data", $other_from_id);
}
示例4: 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");
示例5: function
<?php
$serv = new swoole_server("127.0.0.1", 9501);
$serv->set(['worker_num' => 1, 'task_worker_num' => 1]);
$serv->on('connect', function (swoole_server $serv, $fd) {
echo "Client:Connect.\n";
$data = str_repeat("A", 8000000);
$res = $serv->taskwait($data);
echo "task finish, result length =" . strlen($res[0]) . "\n";
});
$serv->on('receive', function ($serv, $fd, $from_id, $data) {
//echo "recv n=".strlen($data)."\n";
//$serv->send($fd, 'Swoole: hello');
});
$serv->on('close', function ($serv, $fd) {
echo "Client: Close.\n";
});
$serv->on('task', function ($serv, $task_id, $from_id, $data) {
echo "task length=" . strlen($data) . "\n";
return array($data);
});
$serv->on('finish', function ($serv, $task_id, $data) {
//echo "Client: Close.\n";
});
$serv->start();
示例6: my_onReceive
function my_onReceive(swoole_server $serv, $fd, $from_id, $data)
{
my_log("Worker#{$serv->worker_pid} Client[{$fd}@{$from_id}]: received: {$data}");
$cmd = trim($data);
if ($cmd == "reload") {
$serv->reload();
} elseif ($cmd == "task") {
$serv->task("task-" . $fd);
} elseif ($cmd == "taskwait") {
$result = $serv->taskwait("taskwait");
if ($result) {
$serv->send($fd, "taskwaitok");
}
} elseif ($cmd == "taskWaitMulti") {
$tasks = array('taskWaitMulti', 'taskWaitMulti');
$result = $serv->taskWaitMulti($tasks);
if (is_array($result)) {
$serv->send($fd, "taskWaitMulti OK");
} else {
$serv->send($fd, "taskWaitMulti error");
}
} elseif ($cmd == "hellotask") {
$serv->task("hellotask");
} elseif ($cmd == "sendto") {
$serv->sendto("127.0.0.1", 9999, "hello world");
} elseif ($cmd == "close") {
$serv->close($fd);
} elseif ($cmd == "info") {
$info = $serv->connection_info(strval($fd), $from_id);
$serv->send($fd, serialize($info));
} elseif ($cmd == 'proxy') {
$serv->send(1, "hello world\n");
} elseif ($cmd == 'sleep') {
sleep(10);
} elseif ($cmd == 'sendfile') {
$serv->sendfile($fd, __DIR__ . '/test.jpg');
} elseif ($cmd == 'foreach') {
foreach ($serv->connections as $fd) {
echo "conn : {$fd}\n";
}
return;
} elseif ($cmd == 'tick') {
$serv->tick(1000, function ($id) use($serv, $fd) {
$serv->send($fd, "tick");
$serv->clearTimer($id);
});
} elseif ($cmd == 'after') {
$serv->after(1000, function ($id) use($serv, $fd) {
$serv->send($fd, "after");
});
} elseif ($cmd == "list") {
$start_fd = 0;
echo "broadcast\n";
while (true) {
$conn_list = $serv->connection_list($start_fd, 10);
if (empty($conn_list)) {
echo "iterates finished\n";
break;
}
$start_fd = end($conn_list);
var_dump($conn_list);
}
} elseif ($cmd == "list2") {
foreach ($serv->connections as $con) {
var_dump($serv->connection_info($con));
}
} elseif ($cmd == "stats") {
$serv_stats = $serv->stats();
$serv->send($fd, 'Stats: ' . var_export($serv_stats, true) . "\ncount=" . count($serv->connections) . PHP_EOL);
} elseif ($cmd == "broadcast") {
broadcast($serv, $fd, "hello from {$fd}\n");
} elseif ($cmd == "error") {
hello_no_exists();
} elseif ($cmd == "exit") {
exit("worker php exit.\n");
} elseif (substr($cmd, 0, 5) == "close") {
$close_fd = substr($cmd, 6);
$serv->close($close_fd);
} elseif ($cmd == "shutdown") {
$serv->shutdown();
} elseif ($cmd == "fatalerror") {
require __DIR__ . '/php/error.php';
} elseif ($cmd == 'sendbuffer') {
$buffer = G::getBuffer($fd);
$buffer->append("hello\n");
$serv->send($fd, $buffer);
} elseif ($cmd == 'defer') {
$serv->defer(function () use($fd, $serv) {
$serv->close($fd);
$serv->defer(function () {
echo "deferd\n";
});
});
$serv->send($fd, 'Swoole: ' . $data, $from_id);
} elseif ($cmd == 'big_resp') {
$serv->send($fd, str_repeat('A', 256 * 1024));
} elseif ($cmd == 'big_task') {
$serv->task($fd . ' ' . str_repeat('A', 256 * 1024 - 8));
} else {
$serv->send($fd, 'Swoole: ' . $data, $from_id);
//.........这里部分代码省略.........
示例7: onReceive
/**
* 接收客户端请求
*
* @access public
* @return void
**/
public function onReceive(swoole_server $serv, $fd, $from_id, $rdata)
{
$data = json_decode($rdata, true);
if (isset($data['cmd'])) {
switch ($data['cmd']) {
case 'sync_send':
$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;
//执行任务 调用onTask函数
//执行任务 调用onTask函数
case "send":
$serv->task($data, 0);
$serv->send($fd, "OK\n");
break;
case "tick":
$serv->tick(3000, function () use($serv, $fd, $data) {
$serv->task($data, 0);
});
break;
default:
echo "ERROR CMD \n";
}
}
}
示例8: my_onReceive
function my_onReceive(swoole_server $serv, $fd, $from_id, $data)
{
my_log("Worker#{$serv->worker_pid} Client[{$fd}@{$from_id}]: received: {$data}");
$cmd = trim($data);
if ($cmd == "reload") {
$serv->reload();
} elseif ($cmd == "task") {
$task_id = $serv->task("task " . $fd);
echo "Dispath AsyncTask: id={$task_id}\n";
} elseif ($cmd == "taskclose") {
$serv->task("close " . $fd);
echo "close the connection in taskworker\n";
} elseif ($cmd == "tasksend") {
$serv->task("send " . $fd);
} elseif ($cmd == "bigtask") {
$serv->task(str_repeat('A', 8192 * 5));
} elseif ($cmd == "taskwait") {
$result = $serv->taskwait("taskwait");
if ($result) {
$serv->send($fd, "taskwaitok");
}
echo "SyncTask: result=" . var_export($result, true) . "\n";
} elseif ($cmd == "taskWaitMulti") {
$result = $serv->taskWaitMulti(array(str_repeat('A', 8192 * 5), str_repeat('B', 8192 * 6), str_repeat('C', 8192 * 8)));
if ($result) {
$resp = "taskWaitMulti ok\n";
foreach ($result as $k => $v) {
$resp .= "result[{$k}] length=" . strlen($v) . "\n";
}
$serv->send($fd, $resp);
} else {
$serv->send($fd, "taskWaitMulti error\n");
}
} elseif ($cmd == "hellotask") {
$serv->task("hellotask");
} elseif ($cmd == "taskcallback") {
$serv->task("taskcallback", -1, function (swoole_server $serv, $task_id, $data) {
echo "Task Callback: ";
var_dump($task_id, $data);
});
} elseif ($cmd == "sendto") {
$serv->sendto("127.0.0.1", 9999, "hello world");
} elseif ($cmd == "close") {
$serv->send($fd, "close connection\n");
$result = $serv->close($fd);
} elseif ($cmd == "info") {
$info = $serv->connection_info(strval($fd), $from_id);
var_dump($info["remote_ip"]);
$serv->send($fd, 'Info: ' . var_export($info, true) . PHP_EOL);
} elseif ($cmd == 'proxy') {
$serv->send(1, "hello world\n");
} elseif ($cmd == 'sleep') {
sleep(10);
} elseif ($cmd == 'foreach') {
foreach ($serv->connections as $fd) {
echo "conn : {$fd}\n";
}
return;
} elseif ($cmd == 'tick') {
$serv->tick(2000, function ($id) {
echo "tick #{$id}\n";
});
} elseif ($cmd == 'addtimer') {
$serv->addtimer(3000);
} elseif ($cmd == "list") {
$start_fd = 0;
echo "broadcast\n";
while (true) {
$conn_list = $serv->connection_list($start_fd, 10);
if (empty($conn_list)) {
echo "iterates finished\n";
break;
}
$start_fd = end($conn_list);
var_dump($conn_list);
}
} elseif ($cmd == "list2") {
foreach ($serv->connections as $con) {
var_dump($serv->connection_info($con));
}
} elseif ($cmd == "stats") {
$serv_stats = $serv->stats();
$serv->send($fd, 'Stats: ' . var_export($serv_stats, true) . "\ncount=" . count($serv->connections) . PHP_EOL);
} elseif ($cmd == "broadcast") {
broadcast($serv, $fd, "hello from {$fd}\n");
} elseif ($cmd == "error") {
hello_no_exists();
} elseif ($cmd == "exit") {
exit("worker php exit.\n");
} elseif ($cmd == 'pause') {
echo "pause receive data. fd={$fd}\n";
$serv->pause($fd);
} elseif (substr($cmd, 0, 6) == "resume") {
$resume_fd = substr($cmd, 7);
$serv->resume($resume_fd);
} elseif (substr($cmd, 0, 5) == "close") {
$close_fd = substr($cmd, 6);
$serv->close($close_fd);
} elseif ($cmd == "shutdown") {
$serv->shutdown();
//.........这里部分代码省略.........