Python:多进程下实现单例

简介: Python:多进程下实现单例

通过一个判断文件是否存在,判断实例是否存在

# -*- coding: utf-8 -*-
import atexit
import os
@atexit.register
def remove_lock_file():
    if os.path.exists('file.lock'):
        os.remove('file.lock')
def create_lock_file():
    if not os.path.exists('file.lock'):
        with open('file.lock', 'w') as f:
            pass
        return True
    else:
        return False
from multiprocessing import Process
def open_file():
    if create_lock_file():
        print('hello')
Process(target=open_file).start()
Process(target=open_file).start()

两个进程下,只会打印一次hello

相关文章
|
1月前
|
安全 Java 数据处理
Python网络编程基础(Socket编程)多线程/多进程服务器编程
【4月更文挑战第11天】在网络编程中,随着客户端数量的增加,服务器的处理能力成为了一个重要的考量因素。为了处理多个客户端的并发请求,我们通常需要采用多线程或多进程的方式。在本章中,我们将探讨多线程/多进程服务器编程的概念,并通过一个多线程服务器的示例来演示其实现。
|
1月前
|
Web App开发 监控 Unix
python 通过进程名查状态
【4月更文挑战第2天】
|
1月前
|
数据采集 数据库 C++
python并发编程:并发编程中是选择多线程呢?还是多进程呢?还是多协程呢?
python并发编程:并发编程中是选择多线程呢?还是多进程呢?还是多协程呢?
22 0
|
5天前
|
监控 Python
python过滤指定进程
python过滤指定进程
13 1
|
5天前
|
运维 监控 Ubuntu
Python实现ubuntu系统进程内存监控
Python实现ubuntu系统进程内存监控
12 1
|
5天前
|
开发者 Python
在Python中查询进程信息的实用指南
在Python中查询进程信息的实用指南
9 2
|
13天前
|
消息中间件 Linux 调度
Python的进程锁,进程队列
Python的进程锁,进程队列
122 3
|
13天前
|
数据采集 监控 调度
Python的进程,以及进程同步,守护进程详细解读
Python的进程,以及进程同步,守护进程详细解读
138 4
|
13天前
|
调度 Python 容器
【python】-详解进程与线程
【python】-详解进程与线程
|
17天前
|
运维 监控 Unix
第十五章 Python多进程与多线程
第十五章 Python多进程与多线程