python fcntl 文件锁

此模块只有在 unix 系统上才有,windows 没有。html

文档地址:python

https://docs.python.org/3.7/library/fcntl.htmlflask

https://www.docs4dev.com/docs/zh/python/3.7.2rc1/all/library-fcntl.htmlwindows

多进程示例程序

import fcntl
import os
import time
from multiprocessing import Pool

def worker():
    print('task: %s' % os.getpid())
    try:
        f = open('scheduler.lock', 'wb')
        '''加锁,同步锁,其余进程获取不到需等待'''
        fcntl.flock(f, fcntl.LOCK_EX)
        print('I am get file %s' % os.getpid())
        time.sleep(10)
        '''解锁'''
        fcntl.flock(f, fcntl.LOCK_UN)
        f.close()
    except Exception as e:
        print('file is already locked: %s' % os.getpid())

if __name__ == '__main__':
    p = Pool(4)
    for i in range(5):
        p.apply_async(worker)
    print('Waiting for all subprocesses done...')
    p.close()
    p.join()
    print('All subprocesses done.')

fcntl 详细参数

'''f 需传入文件对象,operator 传入加锁方式'''
fcntl.flock(f, operator)

fcntl.LOCK_SH    '共享锁'
fcntl.LOCK_EX    '排他锁'
fcntl.LOCK_NB    '非阻塞锁——若是指定此参数,函数不能得到文件锁就当即返回,不然,函数会等待得到文件锁。LOCK_NB能够同LOCK_SH或LOCK_NB进行按位或(|)运算操做。 fcntl.flock (f,fcntl.LOCK_EX|fcntl.LOCK_NB)'
fcntl.LOCK_UN    '解锁'

解决 gunicorn flask-apscheduler 重复执行问题

from flask import Flask
from service.extensions import scheduler
import logging
from logging.handlers import RotatingFileHandler
import os
import fcntl, atexit

basedir = os.path.abspath('')

def create_app():
    app = Flask(__name__)

    f = open("scheduler.lock", "wb")
    try:
        '''使用非阻塞锁'''
        fcntl.flock(f, fcntl.LOCK_EX | fcntl.LOCK_NB)
        register_apscheduler(app)
    except Exception as e:
        pass

    def unlock():
        fcntl.flock(f, fcntl.LOCK_UN)
        f.close()

    '''注册一个退出回调函数,用于在程序退出时进行一些清理工做,关闭文件,解除锁'''
    atexit.register(unlock)

    return app

def register_apscheduler(app):
    scheduler.init_app(app)
    from service import aliyuncron
    scheduler.start()

app = create_app()

@app.route('/')
def index():
    return '<h1>Hello World!</h1>'
相关文章
相关标签/搜索