Locking a file in Python

后端 未结 13 2318
悲&欢浪女
悲&欢浪女 2020-11-22 03:00

I need to lock a file for writing in Python. It will be accessed from multiple Python processes at once. I have found some solutions online, but most fail for my purposes as

相关标签:
13条回答
  • 2020-11-22 04:03

    Locking a file is usually a platform-specific operation, so you may need to allow for the possibility of running on different operating systems. For example:

    import os
    
    def my_lock(f):
        if os.name == "posix":
            # Unix or OS X specific locking here
        elif os.name == "nt":
            # Windows specific locking here
        else:
            print "Unknown operating system, lock unavailable"
    
    0 讨论(0)
提交回复
热议问题