Official Address: https://docs.python.org/2/library/threading.html#thread-objects
The following section only interception condition, other Lock () and threading part see above link
16.2.4. Conditions Object
Condition variable is always associated with some sort of lock. You can pass, you can also create a default. (When a plurality of condition variables must share the same lock, a transmission input useful.)
Having a condition variable acquire()
and release()
method which invokes the corresponding method of the associated lock. It also has await()
method, notify()
and notifyAll()
methods. Only if the calling thread to obtain a lock must call these three objects, otherwise lead to a.RuntimeError
This wait()
method releases the lock, then blocked until a notify()
or notifyAll()
call the same condition variable in another thread wake . After the wake, it will re-acquires the lock and returns. You can also specify a timeout.
The notify()
method wake up one thread waiting on the condition variable (if any). ThisnotifyAll()
method wakes up all the threads waiting on the condition variable.
Note: notify()
and notifyAll()
method does not release the lock; this means that wakes up one or more threads will not wait()
immediately from its call to return , but only callingnotify()
or notifyAll()
eventually gave up ownership of the lock when the thread will not return .
Tip: Use condition variables typical programming style use locks to synchronize access to some shared state; a particular state of change of interest thread wait()
repeatedly calls until they see until the desired state; and calls on the state to be modified threadsnotify()
or notifyAll()
when they change state in some way (for a waiter, it may be the desired state), the thread repeatedly calls them. For example, the following code is the general producer with unlimited buffer size - consumer situation:
# Consume one item
cv.acquire()
while not an_item_is_available(): cv.wait() get_an_available_item() cv.release() # Produce one item cv.acquire() make_an_item_available() cv.notify() cv.release()
To notify()
and choose between notifyAll()
, consider whether one state change only one or more waiting threads meaningful. For example, in a typical producer - consumer under the circumstances, adding to the buffer a consumer need only wake a thread.
-
Class
threading.
Condition
( [ lock ] ) -
If the specified lock parameters, without specifying parameter, the parameter
None
must be a or Object, and used as a basis for the lock. Otherwise, create a new object and use it as the basis of the lock.Lock
RLock
RLock
-
acquire
(* args ) -
Gets the underlying lock. This method calls the appropriate methods on the basis of the lock; return value is the value returned by the method.
-
release
() -
Lock release basis. This method calls the appropriate methods on the basis of the lock; no return value.
-
wait
( [ Timeout ] ) -
Wait until notified or timeout occurs. If the calling thread when calling this method is not acquiring the lock, thrown a.
RuntimeError
This method frees the basis of the lock, and then blocked until a or call the same condition variable in another thread wake up , or until the optional timeout occurs. Once a wake or a timeout, it will re-acquires the lock and returns.
notify()
notifyAll()
If there is timeout parameter instead timeout parameter
None
, it should be a floating-point number, in seconds (or fractions) specified timeout operation.When the lock is too basic , it will not use its method of release , because when a recursive lock acquisition times, it might actually not be unlocked. But the use of such internal interface, even recursively acquired several times, it can really unlock it. After regaining lock, another internal interface will be used to restore the recursion level.
RLock
release()
RLock
-
notify
(n = 1 ) -
By default, wake up a thread waiting for this case (if any). If the calling thread when calling this method is not acquiring the lock, thrown a.
RuntimeError
The method wake up to the n-th waiting for the condition variable thread. If no threads are waiting, null operation.
If there are at least n number of threads are waiting , the current implementation will wake up exactly n number of threads. However, it is not safe to rely on this behavior. Optimized for the future sometimes wake up at least n threads.
Note: awakened thread until it can re-acquire the lock before the real from the call to return . Because not release the lock, so it should caller.
wait()
notify()
-
notify_all
() -
notifyAll
()¶ -
In this case all wake up waiting threads. The behavior of this method is similar , but wakes up all waiting threads instead of one. If the calling thread when calling this method is not acquiring the lock, thrown a.
notify()
RuntimeError
A change in version 2.6: Added
notify_all()
spelling.
-