【centos】【python】程序单例

需求:

避免同一个程序在不同时刻、终端并行执行;不是绝对不能,是两个同样的程序尽量不要一块执行;

技术:

在程序执行时将pid写入到一个文件里,结束时再删除pid文件,下一个程序先检查pid文件是否存在再决定是否提前终止;

python 复制代码
import datetime
import shutil
from pathlib import Path
import logging
import os
# import fcntl
import psutil
from logging.handlers import RotatingFileHandler


LOG_PATH_PARENT = "./"
LOG_PATH = LOG_PATH_PARENT + "/log"


logger = logging.getLogger()
logger.setLevel(logging.INFO)

formatter = logging.Formatter('%(asctime)s [%(levelname)s] %(message)s')
log_abs_file = os.path.join(LOG_PATH_PARENT, 'suri_logrotate.log')

handler = RotatingFileHandler(log_abs_file, maxBytes=1024 * 1024 * 10, backupCount=10)  # 大小10MB , 10个
handler.setFormatter(formatter)
logger.addHandler(handler)

console = logging.StreamHandler()
console.setFormatter(formatter)
logger.addHandler(console)



class SingletonProgress:

    def _get_lock(self):
        """开启进程占用锁,确保其他同名称进程不再重复运行"""
        # file_name = os.path.basename(__file__)
        # lock_file_name = f"/var/run/{file_name}.pid"
        # self.fd = open(self.lock_file_name, "w")
        if not os.path.exists(self.lock_file_name):
            with open(self.lock_file_name, "w", encoding='utf-8') as f:
                f.write(str(os.getpid()))
        else:
            if os.path.exists(self.lock_file_name):
                print(self.lock_file_name)
                with open(self.lock_file_name, 'r', encoding='utf8') as f:
                    pid = f.read().strip()
                if not pid:
                    logger.error("Program abnormal exit,no pid, Please try again.")
                    os.remove(self.lock_file_name)
                elif pid and pid.isdigit() and int(pid) not in psutil.pids():
                    logger.error("Program abnormal exit,pid err, Please try again.")
                    os.remove(self.lock_file_name)
                elif self.file_name not in psutil.Process(int(pid)).cmdline():
                    logger.error("Program abnormal exit,pid not, Please try again.")
                    os.remove(self.lock_file_name)
                else:
                    logger.info(f"{self.file_name} have another instance running.")
                    self.remove_lock_file = False

                exit(1)

    def __init__(self):
        self.file_name = os.path.basename(__file__)
        self.lock_file_name = os.path.join(LOG_PATH_PARENT, self.file_name+'.pid')
        self.remove_lock_file = True
        self.err_delete_lock_file_name = False
        self._get_lock()

    def __del__(self):
        # 占用锁的进程被关闭后,释放锁
        if self.remove_lock_file:
            os.remove(self.lock_file_name)


def method():
    import time
    while 1:
        time.sleep(1)
        logger.info(str(os.getpid()))


if __name__ == '__main__':

    keep_lock = SingletonProgress()
    method()
    # del keep_lock
相关推荐
Kratzdisteln几秒前
【MVCD 6】
python
子夜江寒2 分钟前
OpenCV图像处理部分基础操作
图像处理·python·opencv
last demo5 分钟前
高可用Keepalived
linux·运维·网络·智能路由器
阿豪只会阿巴8 分钟前
【多喝热水系列】从零开始的ROS2之旅——Day5
c++·笔记·python·ubuntu·ros2
叫我:松哥10 分钟前
基于Spark智能推荐算法的农业作物推荐系统,推荐算法使用Spark ML风格推荐引擎
大数据·python·机器学习·spark-ml·spark·flask·推荐算法
2501_9418752811 分钟前
从日志语义到可观测性的互联网工程表达升级与多语言实践分享随笔
java·前端·python
郝学胜-神的一滴13 分钟前
Linux线程使用注意事项:骈文技术指南
linux·服务器·开发语言·数据结构·c++·程序人生
叫我:松哥13 分钟前
基于 Flask 的音乐推荐与可视化分析系统,包含用户、创作者、管理员三种角色,集成 ECharts 进行数据可视化,采用混合推荐算法
开发语言·python·信息可视化·flask·echarts·pandas·推荐算法
迷茫运维路16 分钟前
【K8S集群漏洞扫描】kube-proxy进程所监听的443端口证书过期问题分析与解决
linux·容器·kubernetes·漏洞处理
此剑之势丶愈斩愈烈17 分钟前
mybatis-plus乐观锁
开发语言·python·mybatis