Locks:
Semaphores:
asyncio lock API was designed to be close to classes of the
threading
模块 (
Lock
,
Event
,
Condition
,
Semaphore
,
BoundedSemaphore
), but it has no
timeout
parameter. The
asyncio.wait_for()
function can be used to cancel a task after a timeout.
asyncio.
锁
(
*
,
loop=None
)
¶
Primitive lock objects.
A primitive lock is a synchronization primitive that is not owned by a particular coroutine when locked. 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()
. 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 coroutine changes it to unlocked, then the acquire() call resets it to locked and returns. 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
会被引发。
When more than one coroutine is blocked in acquire() waiting for the state to turn to unlocked, only one coroutine proceeds when a release() call resets the state to unlocked; first coroutine which is blocked in acquire() is being processed.
acquire()
is a coroutine and should be called with
yield from
.
Locks also support the context management protocol.
(yield from lock)
should be used as context manager expression.
此类是 非线程安全 .
用法:
lock = Lock() ... yield from lock try: ... finally: lock.release()
Context manager usage:
lock = Lock() ... with (yield from lock): ...
Lock objects can be tested for locking state:
if not lock.locked(): yield from lock else: # lock is acquired ...
locked
(
)
¶
返回
True
若获得锁。
acquire
(
)
¶
Acquire a lock.
This method blocks until the lock is unlocked, then sets it to locked and returns
True
.
此方法是 协程 .
release
(
)
¶
Release a lock.
When the lock is locked, reset it to unlocked, and return. If any other coroutines are blocked waiting for the lock to become unlocked, allow exactly one of them to proceed.
当在解锁锁援引时,
RuntimeError
被引发。
没有返回值。
asyncio.
事件
(
*
,
loop=None
)
¶
An Event implementation, asynchronous equivalent to
threading.Event
.
实现事件对象的类。可以将事件管理标志设为 True 采用
set()
方法和重置为 False 采用
clear()
方法。
wait()
方法阻塞,直到标志为 True。标志最初为 False。
此类是 非线程安全 .
clear
(
)
¶
Reset the internal flag to false. Subsequently, coroutines calling
wait()
将阻塞直到
set()
被调用,再次将内部标志设为 True。
is_set
(
)
¶
返回
True
当且仅当内部标志为 True 时。
set
(
)
¶
Set the internal flag to true. All coroutines waiting for it to become true are awakened. Coroutine that call
wait()
一旦标志为 True 将根本不阻塞。
wait
(
)
¶
Block until the internal flag is true.
If the internal flag is true on entry, return
True
immediately. Otherwise, block until another coroutine calls
set()
to set the flag to true, then return
True
.
此方法是 协程 .
asyncio.
条件
(
lock=None
,
*
,
loop=None
)
¶
A Condition implementation, asynchronous equivalent to
threading.Condition
.
This class implements condition variable objects. A condition variable allows one or more coroutines to wait until they are notified by another coroutine.
若
lock
自变量有给定且不是
None
,它必须是
Lock
对象,且它被用作底层锁。否则,新的
Lock
对象被创建并用作底层锁。
此类是 非线程安全 .
acquire
(
)
¶
Acquire the underlying lock.
This method blocks until the lock is unlocked, then sets it to locked and returns
True
.
此方法是 协程 .
notify
(
n=1
)
¶
By default, wake up one coroutine waiting on this condition, if any. If the calling coroutine has not acquired the lock when this method is called, a
RuntimeError
被引发。
此方法最多唤醒 n of the coroutines waiting for the condition variable; it is a no-op if no coroutines are waiting.
注意
An awakened coroutine does not actually return from its
wait()
调用直到它可以重新获得锁。由于
notify()
不释放锁,其调用者应释放锁。
locked
(
)
¶
返回
True
if the underlying lock is acquired.
notify_all
(
)
¶
Wake up all coroutines waiting on this condition. This method acts like
notify()
, but wakes up all waiting coroutines instead of one. If the calling coroutine has not acquired the lock when this method is called, a
RuntimeError
被引发。
release
(
)
¶
Release the underlying lock.
When the lock is locked, reset it to unlocked, and return. If any other coroutines are blocked waiting for the lock to become unlocked, allow exactly one of them to proceed.
当在解锁锁援引时,
RuntimeError
被引发。
没有返回值。
wait
(
)
¶
Wait until notified.
If the calling coroutine has not acquired the lock when this method is called, a
RuntimeError
被引发。
此方法释放底层锁,然后阻塞直到被唤醒由
notify()
or
notify_all()
call for the same condition variable in another coroutine. Once awakened, it re-acquires the lock and returns
True
.
此方法是 协程 .
wait_for
(
predicate
)
¶
Wait until a predicate becomes true.
The predicate should be a callable which result will be interpreted as a boolean value. The final predicate value is the return value.
此方法是 协程 .
asyncio.
信号量
(
value=1
,
*
,
loop=None
)
¶
A Semaphore implementation.
信号量管理的内部计数器的递减是通过每
acquire()
调用和递增是通过每
release()
调用。计数器可以从不低于 0;当
acquire()
finds that it is zero, it blocks, waiting until some other coroutine calls
release()
.
Semaphores also support the context management protocol.
The optional argument gives the initial value for the internal counter; it defaults to
1
. If the value given is less than
0
,
ValueError
被引发。
此类是 非线程安全 .
acquire
(
)
¶
获得信号量。
若内部计数器 > 0 在进入时,递减 1 并返回
True
immediately. If it is zero on entry, block, waiting until some other coroutine has called
release()
to make it larger than
0
, and then return
True
.
此方法是 协程 .
locked
(
)
¶
返回
True
if semaphore can not be acquired immediately.
release
(
)
¶
Release a semaphore, incrementing the internal counter by one. When it was zero on entry and another coroutine is waiting for it to become larger than zero again, wake up that coroutine.
asyncio.
BoundedSemaphore
(
value=1
,
*
,
loop=None
)
¶
A bounded semaphore implementation. Inherit from
Semaphore
.
This raises
ValueError
in
release()
if it would increase the value above the initial value.