# SimpleFileLock **Repository Path**: initialneil/SimpleFileLock ## Basic Information - **Project Name**: SimpleFileLock - **Description**: SimpleFileLock forked originally from dmfrey/FileLock - **Primary Language**: Python - **License**: BSD-3-Clause - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2024-05-23 - **Last Updated**: 2024-05-23 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README By Neil Z. SHAO: - fix `__init__.py` - rename to SimpleFileLock to avoid conflict - install: ``` pip install git+https://gitee.com/initialneil/SimpleFileLock.git ``` - example 1: ``` from simple_filelock import SimpleFileLock lock_fn = ... # lock or skip lock_fn = lock_fn + '.lock' if os.name == 'nt' else lock_fn locker = SimpleFileLock(lock_fn, timeout=0.2) lock_file = locker.lock_file if os.name == 'nt' else locker.lockfile try: print(f'[Main] try lock {lock_file} ... ', end='') locker.acquire() print("success") except: print("failed") # continue or return return # do stuff... locker.release() ``` - example 2: ``` from simple_filelock import SimpleFileLock lock_fn = ... # lock or skip lock_fn = lock_fn + '.lock' if os.name == 'nt' else lock_fn try: with SimpleFileLock(lock_fn, timeout=0.2): print("Lock success") # do stuff... except: print("Lock failed") # continue or return return ``` ----- SimpleFileLock ======== 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. Originally posted at http://www.evanfosmark.com/2009/01/cross-platform-file-locking-support-in-python/