美文网首页Python语言与信息数据获取和机器学习Python 运维
线程的锁函数:threading.Lock() 返回锁对象 Lo

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

作者: rebirth_2017 | 来源:发表于2017-05-23 22:44 被阅读333次

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

相关文章

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

    1. threading.Lock() 返回锁对象。用于生成原始锁对象的工厂函数。一旦某个线程获得了这个锁,其他的...

  • Java16-2 多线程同步函数及区别

    多线程函数引用的锁为this 同步函数:函数上加同步修饰关键字同步函数使用的锁:函数需要被对象调用,哪个对象不确定...

  • 线程 -- 互斥锁

    互斥锁的使用步骤: # 创建锁mutex = threading.Lock()# 上锁mutex.acquire(...

  • synchronized和lock简单理解

    synchronized(同步锁) 思考:锁什么?锁对象 可能锁对象包括:this,临界资源对象(所有线程可能访问...

  • sychronized的偏向锁,轻量级锁和重量级锁

    1.偏向锁:在锁对象的对象头中记录一下当前获取到该锁的线程ID,该线程如果再次获取该对象的的锁就可以直接获取锁(锁...

  • 多线程同步

    多线程的同步依靠的是对象锁机制 加错锁(this)但是每个线程都持有this对象的对象锁 正确加锁外部创建共享资源...

  • java内置锁synchronized的可重入性

    当线程请求一个由其它线程持有的对象锁时,该线程会阻塞,而当线程请求由自己持有的对象锁时,如果该锁是重入锁,请求就会...

  • JUC之可重入锁

    可重入锁 可重入锁又叫递归锁,指的是同一线程外层函数获得锁之后,内存递归函数仍然能获取该锁的代码,同一个线程在外层...

  • wait() notify() notifyAll()讲解

    1.锁池 等待池 每个对象都内置了锁池跟等待池。锁池: 某个线程拥有了该对象的锁,其他线程执行时,遇到该对象修饰的...

  • 线程运用---等待通知的范式wait()、notify()

    1.等待线程获取到对象的锁,调用wait()方法,放弃锁,进入等待队列2.通知线程获取到对象的锁,调用对象的not...

网友评论

    本文标题:线程的锁函数:threading.Lock() 返回锁对象 Lo

    本文链接:https://www.haomeiwen.com/subject/qyqgxxtx.html