python fcntl 文件锁

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

文档地址:

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

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

多进程示例程序

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>‘

原文地址:https://blog.51cto.com/tchuairen/2435582

时间: 2024-08-02 22:31:38

python fcntl 文件锁的相关文章

python的文件锁使用

python的文件锁目前使用的是fcntl这个库,它实际上为 Unix上的ioctl,flock和fcntl 函数提供了一个接口. 1.fcntl库的简单使用 import fcntl import os, time FILE = "counter.txt" if not os.path.exists(FILE): # create the counter file if it doesn't exist file = open(FILE, "w") file.wr

fcntl文件锁操作

文件锁经常应用于两个方面:1.一是锁定文件中的临界数据,比如并发投票时文件记录的投票数2.二是利用具有互斥性质的写锁,实现进程的并发控制. /*使用文件锁*/<F5>#include <fcntl.h> fcntl(int fildes,int cmd,struct flock* arg);cmd:F_GETLK,F_SETLK,F_SETLKW获得或设置记录锁.如果出错,所有命令都返回-1. 在fcntl.h中的锁信息结构struct flock{    /*锁类型*/    s

unix/linux 进程间文件锁

转自 http://www.cnblogs.com/hjslovewcl/archive/2011/03/14/2314333.html 有三种不同的文件锁,这三种都是“咨询性”的,也就是说它们依靠程序之间的合作,所以一个项目中的所有程序封锁政策的一致是非常重要的,当你的程序需要和第三方软件共享文件时应该格外地小心. 有 些程序利用诸如 FIlENAME.lock 的文件锁文件,然后简单地测试此类文件是否存在.这种方法显然不太好,因为当产生文件的进程被杀后,锁文件依然存在,这样文件也许会被永久锁

linux fcntl函数详解

转自:http://www.cnblogs.com/lonelycatcher/archive/2011/12/22/2297349.html 功能描述:根据文件描述词来操作文件的特性. #include <unistd.h>#include <fcntl.h> int fcntl(int fd, int cmd); int fcntl(int fd, int cmd, long arg); int fcntl(int fd, int cmd, struct flock *lock

linux fcntl函数

linux fcntl函数 #include <unistd.h>#include <fcntl.h>int fcntl(int fd, int cmd);int fcntl(int fd, int cmd, long arg);int fcntl(int fd, int cmd, struct flock *lock); [描述]fcntl()针对(文件)描述符提供控制.参数fd是被参数cmd操作(如下面的描述)的描述符.针对cmd的值,fcntl能够接受第三个参数int arg

喜羊羊系列之fcntl

功能描述:根据文件描述词来操作文件的特性. #include <unistd.h> #include <fcntl.h> int fcntl(int fd, int cmd); int fcntl(int fd, int cmd, long arg); int fcntl(int fd, int cmd, struct flock *lock); [描述] fcntl()针对(文件)描述符提供控制.参数fd是被参数cmd操作(如下面的描述)的描述符.针对cmd的值,fcntl能够接

SQLite语法

一.建立数据库 sqlite3.exe test.db 二.双击sqlite-3_6_16目录下的程序sqlite3.exe,即可运行 三.退出 .exit 或者 .quit 四.SQLite支持如下5种数据类型 1.NULL:空值.2.INTEGER:带符号的整型,具体取决有存入数字的范围大小.3.REAL:浮点数字,存储为8-byte IEEE浮点数.4.TEXT:字符串文本.5.BLOB:二进制对象. 五.联系人表格结构如下 create table contact(id integer

sqlite3常用指令

一.建立数据库 sqlite3.exe test.db 二.双击sqlite-3_6_16目录下的程序sqlite3.exe,即可运行 三.退出 .exit 或者 .quit 四.SQLite支持如下5种数据类型 1.NULL:空值.2.INTEGER:带符号的整型,具体取决有存入数字的范围大小.3.REAL:浮点数字,存储为8-byte IEEE浮点数.4.TEXT:字符串文本.5.BLOB:二进制对象. 五.联系人表格结构如下 create table contact(id integer 

SQLite的使用详解

一.SQLite简介   SQLite是一款轻型的数据库,是遵守ACID的关联式数据库管理系统,它的设计目标是嵌入式的,而且目前已经在很多嵌入式产品中使用了它,它占用资源非常的低,在嵌入式设备中,可能只需要几百K的内存就够了.它能够支持Windows/Linux/Unix等等主流的操作系统,同时能够跟很多程序语言相结合,比如Tcl.PHP.Java等,还有ODBC接口,同样比起Mysql.PostgreSQL这两款开源世界著名的数据库管理系统来讲,它的处理速度比他们都快. 下面就是SQLite的