当前位置:网站首页>聊聊swoole或者php cli 进程如何热重启
聊聊swoole或者php cli 进程如何热重启
2022-06-25 22:05:00 【ndrandy】
在讨论这个话题之前,需要了解一下linux的信号,在linux中发起一个信号最常用的函数莫过于kill了,如 kill -SIGUSR1 pid、kill -9 pid 、kill -SIGTERM pid 等等。这些都属于软中断
进程在收到这些信号之后,默认的处理行为,就是不管三七二十一直接退出进程, 这时候如果进程正在处理业务数据,那么不好意思,直接就没了。所以该怎么处理呢?
核心步骤
- 注册信号处理函数,去拦截监听进程收到的信号,改变进程默认的处理行为
- 在业务层代码,做一个标记状态,当状态为可退出时,直接exit
下面用php模拟实现
<?php
$running = true;
pcntl_signal(SIGTERM, function () use (&$running) {
//收到stop命令, 将开关标记为可退出
$running = false;
//这里并没有直接exit
});
while ($running){
//这里处理业务数据逻辑
//do step.1
//do step.2
//do step.3
//do step.4
//do step.5
//do step.6
}
/**
* 当进程收到SIGTERM信号时(即:stop),代码逻辑可能运行到step.1 ~ step.6之间的任何一处。收到 信
* 号之后立即改变$running= false,此时不直接exit,保证本轮while循环跑完,到下一轮while的时候
* $running= false,while循环退出, 进程就安全退出了
*/
边栏推荐
- 史上最简单的录屏转gif小工具LICEcap,要求不高可以试试
- Kylin
- Leetcode-1528- rearrange string - hash table - string
- Extraction system apk
- Anaconda一文入门笔记
- QComboBox下拉菜单中有分隔符Separator时的样式设置
- Analysis and comprehensive summary of full type equivalent judgment in go
- YUV444、YUV422、YUV420、YUV420P、YUV420SP、YV12、YU12、NV12、NV21
- CSDN原力值
- Uniapp - call payment function: Alipay
猜你喜欢

二叉排序树

Hibernate architecture introduction and environment construction (very detailed)

Uniapp -- the use of document collation and push of unipush

When are the three tools used for interface testing?

CSDN添加页内跳转和页外指定段落跳转

Screen recording to GIF is an easy-to-use gadget, screentogif, which is free and easy to use!

My vscode

qtcreator 格式化代码

ACM. Hj16 shopping list ●●

后序线索二叉树
随机推荐
Uni app -- listen for the exit of the return key
.user.ini文件导致的php网站安装问题
Graduation trip | recommended 5-day trip to London
经典图像分割网络:Unet 支持libtorch部署推理【附代码】
流数据
解析产品开发失败的5个根本原因
二叉排序树
Qlabel text scrolling horizontally
Kylin
Classic image segmentation network: UNET supports libtorch deployment reasoning [with code]
BI-SQL丨存储过程(一)
音频基础知识以及PCM转WAV
录屏转gif的好用小工具ScreenToGif,免费又好用!
Mutual conversion between QT utf8 and Unicode encoding, and the Unicode encoding output format is &xxxxx
Once beego failed to find bee after passing the go get command Exe's pit
c_ uart_ interface_ Example and offboard modes
B. Box Fitting-CodeCraft-21 and Codeforces Round #711 (Div. 2)
QT custom implemented calendar control
18亿像素火星全景超高清NASA放出,非常震撼
Hibernate architecture introduction and environment construction (very detailed)