当前位置:网站首页>聊聊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循环退出, 进程就安全退出了
*/
边栏推荐
- Kotlin空指针Bug
- 24class static member
- String对象(常量)池
- Informatics Orsay all in one 1353: expression bracket matching | Luogu p1739 expression bracket matching
- Binary, hexadecimal, big end and small end
- CSDN添加页内跳转和页外指定段落跳转
- Go language escape analysis complete record
- Solving typeerror: Unicode objects must be encoded before hashing
- 森林的先序和中序遍历
- C. Fibonacci Words-April Fools Day Contest 2021
猜你喜欢
excel如何实现中文单词自动翻译成英文?这个公式教你了
Screen recording to GIF is an easy-to-use gadget, screentogif, which is free and easy to use!
When are the three tools used for interface testing?
QT Chinese and English use different fonts respectively
CSDN添加页内跳转和页外指定段落跳转
Uniapp -- the use of document collation and push of unipush
1.8 billion pixel Mars panorama Ultra HD released by NASA, very shocking
The simplest screen recording to GIF gadget in history, licecap, can be tried if the requirements are not high
实例:用C#.NET手把手教你做微信公众号开发(21)--使用微信支付线上收款:H5方式
Database - mongodb
随机推荐
24class static member
DPVS-FullNAT模式管理篇
Record the ideas and precautions for QT to output a small number of pictures to mp4
解决‘tuple‘ object has no attribute ‘lower‘
平衡二叉树AVL
解决TypeError: Unicode-objects must be encoded before hashing
QT custom implemented calendar control
Analysis and comprehensive summary of full type equivalent judgment in go
phoenix索引
1.8 billion pixel Mars panorama Ultra HD released by NASA, very shocking
Spark日志分析
proxy
(serial port Lora module) centrida rf-al42uh private protocol test at instruction test communication process
C2. k-LCM (hard version)-Codeforces Round #708 (Div. 2)
C. Fibonacci Words-April Fools Day Contest 2021
A. Balance the Bits--Codeforces Round #712 (Div. 1)
录屏转gif的好用小工具ScreenToGif,免费又好用!
CSDN原力值
Kylin
对卡巴斯基发现的一个将shellcode写入evenlog的植入物的复现