美文网首页py
2020-12-01python文件锁有什么作用 如何实现 文件

2020-12-01python文件锁有什么作用 如何实现 文件

作者: 217760757146 | 来源:发表于2020-12-01 11:55 被阅读0次
文件锁可以构建自己的分布式队列 现在只能实现在低速情况下 依赖文件的读取文件锁实现主要依赖一个大神开源的 FileLock类 非常强悍
import os
import time
import errno
 
class FileLockException(Exception):
    pass
 
class FileLock(object):
    """ A file locking mechanism that has context-manager support so 
        you can use it in a with statement. This should be relatively cross
        compatible as it doesn't rely on msvcrt or fcntl for the locking.
    """
 
    def __init__(self, file_name, timeout=10, delay=.05):
        """ Prepare the file locker. Specify the file to lock and optionally
            the maximum timeout and the delay between each attempt to lock.
        """
        self.is_locked = False
        self.lockfile = os.path.join(os.getcwd(), "%s.lock" % file_name)
        self.file_name = file_name
        self.timeout = timeout
        self.delay = delay
 
 
    def acquire(self):
        """ Acquire the lock, if possible. If the lock is in use, it check again
            every `wait` seconds. It does this until it either gets the lock or
            exceeds `timeout` number of seconds, in which case it throws 
            an exception.
        """
        start_time = time.time()
        while True:
            try:
                #独占式打开文件
                self.fd = os.open(self.lockfile, os.O_CREAT|os.O_EXCL|os.O_RDWR)
                break;
            except OSError as e:
                if e.errno != errno.EEXIST:
                    raise 
                if (time.time() - start_time) >= self.timeout:
                    raise FileLockException("Timeout occured.")
                time.sleep(self.delay)
        self.is_locked = True
 
 
    def release(self):
        """ Get rid of the lock by deleting the lockfile. 
            When working in a `with` statement, this gets automatically 
            called at the end.
        """
        #关闭文件,删除文件
        if self.is_locked:
            os.close(self.fd)
            os.unlink(self.lockfile)
            self.is_locked = False
 
 
    def __enter__(self):
        """ Activated when used in the with statement. 
            Should automatically acquire a lock to be used in the with block.
        """
        if not self.is_locked:
            self.acquire()
        return self
 
 
    def __exit__(self, type, value, traceback):
        """ Activated at the end of the with statement.
            It automatically releases the lock if it isn't locked.
        """
        if self.is_locked:
            self.release()
 
 
    def __del__(self):
        """ Make sure that the FileLock instance doesn't leave a lockfile
            lying around.
        """
        self.release()

"""
#use as:
from filelock import FileLock
with FileLock("myfile.txt"):
    # work with the file as it is now locked
    print("Lock acquired.")
"""

在我的应用场景下需要多个程序去读一个文件的数据执行 且不能重复执行 虽然速度较慢 也算是一个分布式应用
实现原理是借助一个csv文件将执行index写入文件 每次执行读取index执行 读取写入过程中对文件加锁

import csv
import time
from filelock import FileLock
def filelock(filename):
    with FileLock(filename):#加锁只需要这一步
        with open(filename,mode='r+',encoding='utf-8-sig') as f:
            rowlist= []
            for row in csv.reader(f):
                rowlist.append(row)
            #rowlist读取文件中的index
            if rowlist !=[]:
                start_flags= int(rowlist[0][0])
            else:
                start_flags = 0
            f.seek(0)
            #加一之后再写入文件
            f.write(str(start_flags+1))
    #完成之后等待解锁
    time.sleep(2)     
    return start_flags

目前执行过程中发现需要注意的问题 完成之后一定要做等待 文件的关闭需要一些时间
如果一个程序在写入过程中出问题 会导致后续程序都卡死 待优化

通过这个功能的初步完成 也对文件的操作权限有了全面一点的了解
r+可读内容可写内容调整指针可覆盖 w+先清空文件在覆盖写可读写入后的内容 a+追加写入不可读
f.seek(0)#将指针调整到开头 在写入就覆盖

文件锁详解(https://www.xuebuyuan.com/2068825.html

python文件操作权限及指针(https://www.cnblogs.com/hbsygfz/p/4542220.html

相关文章

  • 2020-12-01python文件锁有什么作用 如何实现 文件

    文件锁可以构建自己的分布式队列 现在只能实现在低速情况下 依赖文件的读取文件锁实现主要依赖一个大神开源的 File...

  • Git使用(以及可用于解决问题的思路)

    1.什么是Git? 2.Git中区域分为哪几块?有什么作用? 3.在一个文件中,如何实现对文件的管理? 4.Git...

  • day14 - find查找

    为什么要有文件查找 2.windows如何实现文件查找? 3.linux如何实现文件查找? 4.find命令查找语...

  • 2019-07-23工作总结

    疑问1: MDL写锁, MDL读锁, 和S锁, X锁有啥区别? 疑问2: flush文件有啥作用? 答: fl...

  • JAVA NIO 文件锁FileLock

    文件锁可以是shared(共享锁)或者exclusive(排他锁)。不是所有的平台都以同一种方式实现文件锁,不同的...

  • Host文件是什么?host文件有什么作用?

    Hosts是一个没有扩展名的系统文件,其基本作用就是将一些常用的网址域名与其对应的IP地址建立一个关联“数据库”,...

  • 10.文件锁

    pa文件,对文件加读锁 pb文件,对文件加写锁 pc文件

  • CocoaPod高级篇

    目录 xcconfig文件 CocoaPod的实现pod install的过程 xcconfig文件 作用:工程的...

  • 文件

    目标 文件操作的作用 文件的基本操作打开读写关闭 文件备份 文件和文件夹的操作 一. 文件操作的作用 思考:什么是...

  • iOS应用安全:一:class-dump的安装和使用

    class-dump的作用 class-dump用于导出App的头文件,并不会导出实现文件,虽然看不到实现文件只看...

网友评论

    本文标题:2020-12-01python文件锁有什么作用 如何实现 文件

    本文链接:https://www.haomeiwen.com/subject/lgxswktx.html