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