欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

线程的锁函数:threading.Lock() 返回锁对象 Lock

程序员文章站 2022-05-04 20:53:42
...

1. threading.Lock()

返回锁对象。用于生成原始锁对象的工厂函数。
一旦某个线程获得了这个锁,其他的线程要想获得他就必须阻塞,直到锁被释放。

A factory function that returns a new primitive lock object. Once a thread has acquired it, subsequent attempts to acquire it block, until it is released; any thread may release it. See Lock Objects.

2. Lock Objects

Lock Objects是最低级别的同步原语,由线程的扩展模块threading实现。

A primitive lock is a synchronization primitive that is not owned by a particular thread when locked. In Python, it is currently the lowest level synchronization primitive available, implemented directly by the thread extension module.

他有两种状态,“锁定”或“失锁”。创建时未加锁,锁对象有两个方法:acquire() and release().

A primitive lock is in one of two states, “locked” or “unlocked”. It is created in the unlocked state. It has two basic methods, acquire() and release().

acquire():
- When the state is unlocked, acquire() changes the state to locked and returns immediately.
- When the state is locked, acquire() blocks until a call to release() in another thread changes it to unlocked, then the acquire() call resets it to locked and returns.

release():

  • The release() method should only be called in the locked state; it changes the state to unlocked and returns immediately. If an attempt is made to release an unlocked lock, a RuntimeError will be raised.

When more than one thread is blocked in acquire() waiting for the state to turn to unlocked, only one thread proceeds when a release() call resets the state to unlocked; which one of the waiting threads proceeds is not defined, and may vary across implementations.

All methods are executed atomically.

  • 初始化锁和线程
    线程的锁函数:threading.Lock() 返回锁对象 Lock

  • 所有线程抢锁,但只有一个能抢上
    线程的锁函数:threading.Lock() 返回锁对象 Lock

  • 待抢上锁的线程用完释放后,其他线程再竞争锁
    线程的锁函数:threading.Lock() 返回锁对象 Lock

3.Lock.acquire([blocking=1])

以阻塞或非阻塞的方式获得锁。

Acquire a lock, blocking or non-blocking.

没有参数或参数设为ture时,阻塞方式获得锁,即要等到锁释放后方能加锁,而后返回Ture。

When invoked without arguments, block until the lock is unlocked, then set it to locked, and return true.

When invoked with the blocking argument set to true, do the same thing as when called without arguments, and return true.

当有参数,且参数设为false时,即为非阻塞方式获得锁。具体来说,如果已被锁定,直接返回false,不等待;如果未被锁定,则返回ture。

When invoked with the blocking argument set to false, do not block. If a call without an argument would block, return false immediately; otherwise, do the same thing as when called without arguments, and return true.

比如,如下这段代码:

def buy():
    lock.acquire()
    print 'Buying candy...'
    if candytray.acquire(False):
        print 'OK'
    else:
        print 'empty, skipping'
    lock.release()

我们在获得锁时,不希望它等到锁释放再获得,而是希望马上给出反馈, 到底有没有资源,没有就说没有了’empty, skipping’,有就打印’OK’。故用了candytray.acquire(False)这种非阻塞模式。

4.Lock.release()

Release a lock.

When the lock is locked, reset it to unlocked, and return. If any other threads are blocked waiting for the lock to become unlocked, allow exactly one of them to proceed.

Do not call this method when the lock is unlocked.

There is no return value.

示例:
线程的锁函数:threading.Lock() 返回锁对象 Lock