本文主要是介绍swoole http服务器task投递异步任务,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
官网的task案例代码是在tcp服务器中写的,本人在想,http服务器也是server服务器中的一种,应该也可以投递task任务。
一个简单的http服务器代码:
$server = new Swoole\Http\Server('127.0.0.1',8888);$server->on('request',function($request, $response) use($server){if(strpos($request->server['request_uri'],'.ico') !== false){$response->end("");}else{$content = $request->server;$response->end("<h1>Hello Swoole. #".rand(1000, 9999).$content['server_port']."</h1>");}});echo "服务器启动!".PHP_EOL;
$server->start();
-------------------------------------------------------------------------------------------------------------------------
官网api中已经说明 :"使用Task
功能,必须先设置 task_worker_num,并且必须设置Server
的onTask
和onFinish
事件回调函数"
那么代码修改如下:
$server = new Swoole\Http\Server('127.0.0.1',8888);$server->set(['task_worker_num' =>4
]);$server->on('request',function($request, $response) use($server){if(strpos($request->server['request_uri'],'.ico') !== false){$response->end("");}else{$content = $request->server;$response->end("<h1>Hello Swoole. #".rand(1000, 9999).$content['server_port']."</h1>");}});$server->on("Task",function (Swoole\Server $serv, $task_id, $from_id, $data){echo "异步任务Data:".$data.PHP_EOL;$serv->finish($data);});$server->on("Finish",function (Swoole\Server $serv, $task_id, $data){echo "异步任务结束:".PHP_EOL;echo "异步任务结束,数据:".$data.PHP_EOL;});echo "服务器启动!".PHP_EOL;
$server->start();
上述代码怎么投递任务呢,既然http服务器,那就能使用server中的一些方法,尝试使用connect方法:
$server = new Swoole\Http\Server('127.0.0.1',8888);$server->set(['task_worker_num' =>4
]);$server->on("connect",function ($serv, $fd){echo "链接服务器:".$fd.PHP_EOL;$serv->task('task');
});$server->on('request',function($request, $response) use($server){if(strpos($request->server['request_uri'],'.ico') !== false){$response->end("");}else{$content = $request->server;$response->end("<h1>Hello Swoole. #".rand(1000, 9999).$content['server_port']."</h1>");}
});$server->on("Task",function (Swoole\Server $serv, $task_id, $from_id, $data){echo "异步任务Data:".$data.PHP_EOL;$serv->finish($data);
});$server->on("Finish",function (Swoole\Server $serv, $task_id, $data){echo "异步任务结束:".PHP_EOL;echo "异步任务结束,数据:".$data.PHP_EOL;
});echo "服务器启动!".PHP_EOL;
$server->start();
浏览器访问 http://127.0.0.1:8888/?id=1 的结果:
很明显 connect 方法回调了两次,官网也说明了http服务器在浏览器中方法会有两次请求。说明放在connect方法中投递任务虽然可以但是会有问题。看代码结构 只有在onrequest中回调方法中投递任务是最理想的,可以根据业务来判断是否投递任务,不知道回调方法能不能引用server 变量。代码如下:
$server = new Swoole\Http\Server('127.0.0.1',8888);$server->set(['task_worker_num' =>4
]);$server->on('request',function($request, $response) use($server){if(strpos($request->server['request_uri'],'.ico') !== false){$response->end("");}else{$content = $request->server;$server->task('data');$response->end("<h1>Hello Swoole. #".rand(1000, 9999).$content['server_port']."</h1>");}
});$server->on("Task",function (Swoole\Server $serv, $task_id, $from_id, $data){echo "异步任务Data:".$data.PHP_EOL;$serv->finish($data);
});$server->on("Finish",function (Swoole\Server $serv, $task_id, $data){echo "异步任务结束:".PHP_EOL;echo "异步任务结束,数据:".$data.PHP_EOL;
});echo "服务器启动!".PHP_EOL;
$server->start();
执行文件,浏览器访问结果:
、
程序没有报错,而且task任务只投递了一次。
这篇关于swoole http服务器task投递异步任务的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!