您的位置:首页技术文章
文章详情页

php使用goto实现自动重启swoole、reactphp、workerman服务的代码

【字号: 日期:2022-09-10 18:34:28浏览:40作者:猪猪

在平时使用swoole进行开发中,常常遇到这种问题,改了代码之后,手动ctrl+c中断服务,再敲命令重启服务。频繁地重启,感觉心很累。

php提供了inotify扩展,调用linux的inotify系统调用,监控文件的变化.

这时候就产生了一个想法,我开一个主进程监控文件变化,再开一个子进程运行swoole服务。主进程监听到文件变化之后,干掉子进程,然后再开一个子进程运行swoole服务. 子进程如果想优雅地退出,安装个信号处理器,在退出之前做一些操作。

<?php//index.phprequire ’./vendor/autoload.php’;Restart:$pid = pcntl_fork();if ($pid > 0) { $fd = inotify_init(); $watch_descriptor = inotify_add_watch($fd, ’./src/’, IN_MODIFY); $events = inotify_read($fd); posix_kill($pid, SIGTERM); fclose($fd); pcntl_wait($status); goto Restart;} elseif ($pid == 0) { ChurchApplication::run();} else { exit(0);}

<?phpnamespace Church;/**use PsrHttpMessageServerRequestInterface;use ReactHttpResponse;use ReactHttpServer;**/class Application{ public static function run() { /** $loop = ReactEventLoopFactory::create(); $loop->addSignal(SIGTERM, function() use ($loop) { $loop->stop(); }); $server = new Server(function (ServerRequestInterface $request) { return new Response(200,array( ’Content-Type’ => ’text/plain’),'Hello World1!n' ); }); $socket = new ReactSocketServer(8080, $loop); $server->listen($socket); $loop->run(); **/ //高性能HTTP服务器 $http = new SwooleHttpServer('127.0.0.1', 9501); $http->on('start', function ($server) { echo 'Swoole http server is started at http://127.0.0.1:9501n'; }); $http->on('request', function ($request, $response) { $response->header('Content-Type', 'text/plain'); $response->end('Hello World1n'); }); $http->start(); }}

个人觉得这里最优雅的实现方式应该是用GOTO了。

到此这篇关于php使用goto实现自动重启swoole、reactphp、workerman服务的代码的文章就介绍到这了,更多相关php自动重启swoole、reactphp、workerman服务内容请搜索好吧啦网以前的文章或继续浏览下面的相关文章希望大家以后多多支持好吧啦网!

标签: PHP
相关文章: