Skip to main content

Python全局进程锁,文件锁!

Project description

ProcessLock

Python全局进程锁,文件锁!

安装

pip install ProcessLock

使用

from ProcessLock import ProcessLock

p_lock = ProcessLock()

p_lock.acquire()
try:
    # doing..
    print(p_lock.locked())
finally:
    p_lock.release()

用法和普通的锁一样,acquirereleaselocked,唯一的区别是这个锁不需要传参,可以通知设置锁的id来在同一机器上获取到同一把锁

进程1

    from ProcessLock import ProcessLock

    p_lock = ProcessLock(id="lock_name")

    p_lock.acquire()

进程2

    from ProcessLock import ProcessLock

    p_lock = ProcessLock(id="lock_name")

    # True
    print(p_lock.locked())

    # is wait
    p_lock.acquire()

在两个进程中因为都指定了锁的id为 lock_name,所以这两个变量其实是同一把锁,其中一把抢到锁,另外一个会无限等待,直到对方释放锁或者对方所在进程结束.

进程1

    from ProcessLock import ProcessLock

    p_lock = ProcessLock(id="lock_name")

    p_lock.acquire()

进程2

    from ProcessLock import ProcessLock

    p_nowait_lock = ProcessLock(id="lock_name", wait=False)

    # True
    print(p_nowait_lock.locked())

    # is raise AlreadyLocked Err
    p_nowait_lock.acquire()

如果把锁的参数 wait 设置为 False 表示获取锁的时候不进行等待,如果锁不可用直接抛出 AlreadyLocked 异常

这个锁除了是传统意义上的锁的作用,也是文件锁,用途之一是防止不同的程序同时操作同一文件.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ProcessLock-1.0.0.tar.gz (9.7 kB view details)

Uploaded Source

Built Distribution

ProcessLock-1.0.0-py3-none-any.whl (10.4 kB view details)

Uploaded Python 3

File details

Details for the file ProcessLock-1.0.0.tar.gz.

File metadata

  • Download URL: ProcessLock-1.0.0.tar.gz
  • Upload date:
  • Size: 9.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.11.1 pkginfo/1.7.1 requests/2.23.0 requests-toolbelt/0.9.1 tqdm/4.44.1 CPython/3.7.9

File hashes

Hashes for ProcessLock-1.0.0.tar.gz
Algorithm Hash digest
SHA256 21376d05d699a1d8eb4caa706162dda4cfa834401524e9576eb0e6d26e50715d
MD5 30275b477f122655015e2d3916782613
BLAKE2b-256 1861bb37a3dd878b766f46d7b76302748015b0b60b9e5eebf36a9d26bb26b918

See more details on using hashes here.

File details

Details for the file ProcessLock-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: ProcessLock-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 10.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.11.1 pkginfo/1.7.1 requests/2.23.0 requests-toolbelt/0.9.1 tqdm/4.44.1 CPython/3.7.9

File hashes

Hashes for ProcessLock-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 74fffd3d0204f93a6cf6fc8dadc5d268423da7094e634385c0b920c1c1f1bb7e
MD5 ea5a6fac7a800d33e13eb0b1a58c35b6
BLAKE2b-256 91b29c3f105a9fc26c9ed51f8ffb0a3bf09d524e7047e7b2cd8244ba968c912d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page