Maison > Article > développement back-end > [PHP]swoole_server几个进程的分工,swoole_server分工_PHP教程
摘要:Swoole是一个PHP语言的高性能网络通信框架,提供了PHP语言的异步多线程服务器,异步TCP/UDP网络客户端,异步MySQL,数据库连接池,AsyncTask,消息队列,毫秒定时器,异步文件读写,异步DNS查询。强大的功能,由背后若干个分工明确的进程来实现,这里详细介绍下几个进程的分工,以便入门者更快速的理解Swoole框架。
Swoole官网
Swoole:PHP语言的高性能网络通信框架,提供了PHP语言的异步多线程服务器,异步TCP/UDP网络客户端,异步MySQL,数据库连接池,AsyncTask,消息队列,毫秒定时器,异步文件读写,异步DNS查询。 Swoole虽然是标准的PHP扩展,实际上与普通的扩展不同。普通的扩展只是提供一个库函数。而swoole扩展在运行后会接管PHP的控制权,进入事件循环。当IO事件发生后,swoole会自动回调指定的PHP函数。
<span class="x">$serv = new swoole_server("127.0.0.1", 9501);</span> <span class="x">$serv->set(array(</span> <span class="x"> 'worker_num' => 8, //工作进程数量</span> <span class="x"> 'daemonize' => true, //是否作为守护进程</span> <span class="x">));</span> <span class="x">$serv->on('connect', function ($serv, $fd){</span> <span class="x"> echo "Client:Connect.\n";</span> <span class="x">});</span> <span class="x">$serv->on('receive', function ($serv, $fd, $from_id, $data) {</span> <span class="x"> $serv->send($fd, 'Swoole: '.$data);</span> <span class="x"> $serv->close($fd);</span> <span class="x">});</span> <span class="x">$serv->on('close', function ($serv, $fd) {</span> <span class="x"> echo "Client: Close.\n";</span> <span class="x">});</span> <span class="x">$serv->start();</span>
<span class="x">$client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC);</span> <span class="x">//设置事件回调函数</span> <span class="x">$client->on("connect", function($cli) {</span> <span class="x"> $cli->send("hello world\n");</span> <span class="x">});</span> <span class="x">$client->on("receive", function($cli, $data){</span> <span class="x"> echo "Received: ".$data."\n";</span> <span class="x">});</span> <span class="x">$client->on("error", function($cli){</span> <span class="x"> echo "Connect failed\n";</span> <span class="x">});</span> <span class="x">$client->on("close", function($cli){</span> <span class="x"> echo "Connection close\n";</span> <span class="x">});</span> <span class="x">//发起网络连接</span> <span class="x">$client->connect('127.0.0.1', 9501, 0.5);</span>
更多代码片段请见swoole官网。
Master进程主要用来保证Swoole框架机制的运行。它会创建几个功能性的线程:
swoole中Worker/Task进程都是由Manager进程Fork并管理的。
<p>为什么不是Master进程呢,主要原因是Master进程是多线程的,不能安全的执行fork操作。</p>
<p>Swoole提供了完善的进程管理机制,当Worker进程异常退出,如发生PHP的致命错误、被其他程序误杀,或达到max_request次数之后正常退出。主进程会重新拉起新的Worker进程。 Worker进程内可以像普通的apache+php或者php-fpm中写代码。不需要像Node.js那样写异步回调的代码。</p>
Task进程的全称是task_worker进程,是一种特殊的worker进程。所以onWorkerStart
在task进程中也会被调用。当$worker_id >= $serv->setting['worker_num']
时表示这个进程是task_worker,否则,代表此进程是worker进程。
<span class="x">onStart</span> <span class="x">onShutdown</span> <span class="x">onMasterConnect</span> <span class="x">onMasterClose</span> <span class="x">onTimer</span>
<span class="x">onWorkerStart</span> <span class="x">onWorkerStop</span> <span class="x">onConnect</span> <span class="x">onClose</span> <span class="x">onReceive</span> <span class="x">onTimer</span> <span class="x">onFinish</span>
<span class="x">onTask</span> <span class="x">onWorkerStart</span>
<span class="x">onManagerStart</span> <span class="x">onManagerStop</span>