首页
下载
文档
社区
视频
捐赠
源代码
赞助商
AOT 编译器
AI 助理
商业产品
PHP AOT 原生编译器
Swoole-Compiler 代码加密器
CRMEB 新零售社交电商系统
登录
注册
全部
提问
分享
讨论
建议
公告
开发框架
CodeGalaxy
发表新帖
是否可以多次启动服务
无法多次启动服务,即使前一个服务已经退出。如果有这个特性可以在单元测试时构建 mock 服务。 ```php <?php start_stop(); start_stop(); function start_stop() { new_server(); sleep(1); kill_server(); sleep(3); } function new_server() { $port = mt_rand(20000, 60000); echo "Listening localhost:$port\n"; $process = new Swoole\Process(function () use($port) { start_server('localhost', $port); exit(0); }); $process->start(); } function kill_server() { posix_kill(file_get_contents('/tmp/serv.pid'), SIGTERM); $ret = Swoole\Process::wait(); if ($ret['code'] === 0) { echo "service exit successfully.\n"; } } function start_server($host, $port) { $server = new Swoole\Server($host, $port); $server->set(['daemonize', true]); $server->on('start', function($serv) { file_put_contents('/tmp/serv.pid', $serv->master_pid); }); $server->on('receive', function() { }); $server->start(); return $server; } ```
发布于8年前 · 0 次浏览 · 来自
提问
印
印度油神
无法多次启动服务,即使前一个服务已经退出。如果有这个特性可以在单元测试时构建 mock 服务。 ```php <?php start_stop(); start_stop(); function start_stop() { new_server(); sleep(1); kill_server(); sleep(3); } function new_server() { $port = mt_rand(20000, 60000); echo "Listening localhost:$port\n"; $process = new Swoole\Process(function () use($port) { start_server('localhost', $port); exit(0); }); $process->start(); } function kill_server() { posix_kill(file_get_contents('/tmp/serv.pid'), SIGTERM); $ret = Swoole\Process::wait(); if ($ret['code'] === 0) { echo "service exit successfully.\n"; } } function start_server($host, $port) { $server = new Swoole\Server($host, $port); $server->set(['daemonize', true]); $server->on('start', function($serv) { file_put_contents('/tmp/serv.pid', $serv->master_pid); }); $server->on('receive', function() { }); $server->start(); return $server; } ```
赞
0
收藏
提问
分享
讨论
建议
公告
开发框架
CodeGalaxy
登录
后参与评论
评论
2017-10-25
s
scott
不可以,使用外部进程来管理你的Server
赞
0
回复