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.2.tar.gz (10.2 kB view details)

Uploaded Source

Built Distribution

ProcessLock-1.0.2-py3-none-any.whl (10.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: ProcessLock-1.0.2.tar.gz
  • Upload date:
  • Size: 10.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.7

File hashes

Hashes for ProcessLock-1.0.2.tar.gz
Algorithm Hash digest
SHA256 68f6aca91a0d74024b3378833209304ba3993f11bd79025ad0e65acca522ff4b
MD5 96c73926e9d2e65511d5e9ee5303d509
BLAKE2b-256 4d5d3c001b2cecc9530ddd462af66b38e9f141b45f0dfbcfe46acdcdd65b895f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ProcessLock-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 10.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.7

File hashes

Hashes for ProcessLock-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 9f6fe442f7e126a14ab501464b1c141ae6b9af90da64c02c6fe659b72f730322
MD5 b361b249e7302a3b28571737d21f1e0c
BLAKE2b-256 0c39e5cca7a8dea79fae72d8942f6eb230a50485d008a209d0298c69a01dd435

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