Python thread condition

Source: Internet
Author: User

Cond = Threading. Condition ()

# similar to Lock.acquire ()

Cond.acquire ()

# similar to Lock.release ()

Cond.release ()

# waits for the specified trigger, while releasing the acquisition of the lock until the notify is re-occupied.

Cond.wait ()

# send specified, trigger execution

Cond.notify ()

ImportThreading,timecond=Threading. Condition ()#Cond.acquire ()#cond.release ()#cond.wait ()#cond.notify ()defAA ():Print('Thread_aa Get') Cond.acquire () Time.sleep (5)    Print('Thread_aa finished first job') cond.wait ()#release control of the lock so that other processes acquire to               #block at the same time, after the first cond.notify of the process that is waiting to get locked (cond.wait or Cond.release)    Print('Thread_aa Get again') Time.sleep (5)    Print('Thread_aa finished all work') cond.notify ()#tell me about the process of releasing the lock, wait for me to wait or release .Cond.release ()#Release LockdefBB (): Cond.acquire ()Print('THREAD_BB Get') cond.notify ()#tell me about the process of releasing the lock, wait for me to wait or release .    Print('still is my BB in run') Time.sleep (5)    Print('Thread_bb finished first job') cond.wait ()#release the control of the lock, then the blocked AA can execute the            #While blocking this, wait for the first cond.notify of the lock AA (cond.wait or cond.release)    Print('Thread_aa Get again')        Print('Thread_bb finished all works') Cond.release () a=threading. Thread (target=aa) A.start () Time.sleep (2) b=threading. Thread (target=BB) B.start ()

Python thread condition

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.