php使用goto實現(xiàn)自動重啟swoole、reactphp、workerman服務的代碼
在平時使用swoole進行開發(fā)中,常常遇到這種問題,改了代碼之后,手動ctrl+c中斷服務,再敲命令重啟服務。頻繁地重啟,感覺心很累。
php提供了inotify擴展,調用linux的inotify系統(tǒng)調用,監(jiān)控文件的變化.
這時候就產生了一個想法,我開一個主進程監(jiān)控文件變化,再開一個子進程運行swoole服務。主進程監(jiān)聽到文件變化之后,干掉子進程,然后再開一個子進程運行swoole服務. 子進程如果想優(yōu)雅地退出,安裝個信號處理器,在退出之前做一些操作。
<?php
//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);
}
<?php
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();
}
}
個人覺得這里最優(yōu)雅的實現(xiàn)方式應該是用GOTO了。
到此這篇關于php使用goto實現(xiàn)自動重啟swoole、reactphp、workerman服務的代碼的文章就介紹到這了,更多相關php自動重啟swoole、reactphp、workerman服務內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
layui框架實現(xiàn)文件上傳及TP3.2.3(thinkPHP)對上傳文件進行后臺處理操作示例
這篇文章主要介紹了layui框架實現(xiàn)文件上傳及TP3.2.3對上傳文件進行后臺處理操作,結合實例形式分析了layui框架結合thinkPHP進行文件上傳與處理操作相關實現(xiàn)技巧,需要的朋友可以參考下2018-05-05
Yii使用ajax驗證顯示錯誤messagebox的解決方法
這篇文章主要介紹了Yii使用ajax驗證顯示錯誤messagebox的解決方法,可以自行設置Ajax提示信息的方式,是非常實用的技巧,需要的朋友可以參考下2014-12-12
利用PHP腳本在Linux下用md5函數(shù)加密字符串的方法
這篇文章主要介紹了利用PHP腳本在Linux下用md5函數(shù)加密字符串的方法,只需Linux系統(tǒng)中安裝過PHP然后在命令行中操作文中示例即可,需要的朋友可以參考下2015-06-06
使用PHP+MySql+Ajax+jQuery實現(xiàn)省市區(qū)三級聯(lián)動功能示例
下面小編就為大家?guī)硪黄褂肞HP+MySql+Ajax+jQuery實現(xiàn)省市區(qū)三級聯(lián)動功能示例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09
thinkphp在php7環(huán)境下提示Cannot use ‘String’ as class name as it is
這篇文章主要介紹了thinkphp在php7環(huán)境下提示Cannot use ‘String’ as class name as it is reserved的解決方法,涉及thinkPHP針對php7關鍵字判定的相關底層代碼修改技巧,需要的朋友可以參考下2016-09-09
Laravel5.1 框架模型工廠ModelFactory用法實例分析
這篇文章主要介紹了Laravel5.1 框架模型工廠ModelFactory用法,結合實例形式分析了laravel5.1框架模型工廠ModelFactory基本功能、定義與使用方法,需要的朋友可以參考下2020-01-01

