详细带你了解什么是swoole 热重启?
1. swoole 服务端文件:swoole_reload_server.php
<?php
/**
* Example for swoole reload php code without shutdowning server.
*
* @author Lancelot https://github.com/LinkedDestiny
*/
class Server
<?php
/**
* Example for swoole reload php code without shutdowning server.
*
* @author Lancelot https://github.com/LinkedDestiny
*/
class Server
{
private $serv;
public function __construct() {
$this->serv = new swoole_server("0.0.0.0", 9501);
$this->serv->set(array(
'worker_num' => 8,
'daemonize' => false,
'max_request' => 10000,
'dispatch_mode' => 2,
'debug_mode'=> 1,
));
$this->serv->on('Start', array($this, 'onStart'));
$this->serv->on('WorkerStart', array($this, 'onWorkerStart'));
$this->serv->on('Connect', array($this, 'onConnect'));
$this->serv->on('Receive', array($this, 'onReceive'));
$this->serv->on('Close', array($this, 'onClose'));
$this->serv->start();
}
public function onStart( $serv ) {
echo "Start\n";
cli_set_process_title("reload_master");
}
public function onWorkerStart( $serv , $worker_id) {
require_once "reload_page.php";
Test();
}
public function onConnect( $serv, $fd, $from_id ) {
echo "Client {$fd} connect\n";
}
public function onReceive( swoole_server $serv, $fd, $from_id, $data ) {
echo "Get Message From Client {$fd}:{$data}\n";
}
public function onClose( $serv, $fd, $from_id ) {
echo "Client {$fd} close connection\n";
}
}
new Server();
2.重启服务脚本:reload.sh
echo "Reloading..."
cmd=$(pidof reload_master)
kill -USR1 $cmd
echo "Reloaded"
3.重启测试页:reload_page.php
<?php
/**
* If you change this function and want
* swoole_server to use the new function,
* just run 'reload.sh' to send a restart
* signal to swoole_server.
*/
function Test() {
echo "This is not a php file\n";
}
操作流程如下
这个时候 reload_page.php 函数还是
<?php
/**
* If you change this function and want
* swoole_server to use the new function,
* just run 'reload.sh' to send a restart
* signal to swoole_server.
*/
function Test() {
echo "This is not a php file\n";
}
那么我们开始修改 再执行.sh脚本 看看是否有变化
尝试修改为
然后执行脚本呢 脚本 记得 chmod a+x 授权一下
发现这个原来的sever改变了
变成了 i‘m fun 这里没加换行 大概就这样实现了热重启
求一键三连:点赞、转发、在看
赞赏码


非学,无以致疑;非问,无以广识
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App
· 张高兴的大模型开发实战:(一)使用 Selenium 进行网页爬虫
2018-01-26 phpstudy php5.4以上版本伪静态设置 thinkphp
2018-01-26 phpstudy php5.4以上版本伪静态设置 thinkphp
2018-01-26 GitHub上传文件不能超过100M的解决办法
2018-01-26 GitHub上传文件不能超过100M的解决办法
2018-01-26 用thinkphp开启伪静态,用wamp开启很快搞定;但是用phpstudy总是开启失败,为什么?
2018-01-26 用thinkphp开启伪静态,用wamp开启很快搞定;但是用phpstudy总是开启失败,为什么?...