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

该博客介绍了如何利用PHP的inotify扩展和GOTO语句来实现实时监控文件变化并自动重启Swoole和ReactPHP服务。通过创建主进程监控文件,一旦检测到变更,便优雅地终止并重启子进程,从而避免手动重启的繁琐操作。
摘要由CSDN通过智能技术生成

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

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

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

//index.php

require './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) {

\Church\Application::run();

} else {

exit(0);

}

namespace Church;

/**

use Psr\Http\Message\ServerRequestInterface;

use React\Http\Response;

use React\Http\Server;

**/

class Application

{

public static function run()

{

/**

$loop = \React\EventLoop\Factory::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 \React\Socket\Server(8080, $loop);

$server->listen($socket);

$loop->run();

**/

//高性能HTTP服务器

$http = new \Swoole\Http\Server("127.0.0.1", 9501);

$http->on("start", function ($server) {

echo "Swoole http server is started at http://127.0.0.1:9501\n";

});

$http->on("request", function ($request, $response) {

$response->header("Content-Type", "text/plain");

$response->end("Hello World1\n");

});

$http->start();

}

}

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值