Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use a simple Event wrapper instead of a full Event class implementation
- Loading branch information
Showing
4 changed files
with
23 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,77 +1,25 @@ | ||
import asyncio | ||
import collections | ||
|
||
from .helpers import create_future | ||
|
||
|
||
class Event: | ||
class ErrorfulOneShotEvent: | ||
""" | ||
Adhoc Event class mainly copied from the official asyncio.locks.Event, but | ||
modifying the `set` method. It allows to pass an exception to wake | ||
the waiters with an exception. | ||
This class wrappers the Event asyncio lock allowing either awake the | ||
locked Tasks without any error or raising an exception. | ||
This is used when the event creator cant accommplish the requirements | ||
due to an exception, instead of try to built a sophisticated solution | ||
the same exeption is passed to the waiters. | ||
thanks to @vorpalsmith for the simple design. | ||
""" | ||
|
||
def __init__(self, *, loop=None): | ||
self._waiters = collections.deque() | ||
self._value = False | ||
if loop is not None: | ||
self._loop = loop | ||
else: | ||
self._loop = asyncio.get_event_loop() | ||
|
||
def __repr__(self): | ||
res = super().__repr__() | ||
extra = 'set' if self._value else 'unset' | ||
if self._waiters: | ||
extra = '{},waiters:{}'.format(extra, len(self._waiters)) | ||
return '<{} [{}]>'.format(res[1:-1], extra) | ||
|
||
def is_set(self): | ||
"""Return True if and only if the internal flag is true.""" | ||
return self._value | ||
self._event = asyncio.Event(loop=loop) | ||
self._exc = None | ||
|
||
def set(self, exc=None): | ||
"""Set the internal flag to true. All coroutines waiting for it to | ||
become true are awakened. Coroutine that call wait() once the flag is | ||
true will not block at all. | ||
If `exc` is different than None the `future.set_exception` is called | ||
""" | ||
if not self._value: | ||
self._value = True | ||
|
||
for fut in self._waiters: | ||
if not fut.done(): | ||
if not exc: | ||
fut.set_result(True) | ||
else: | ||
fut.set_exception(exc) | ||
|
||
def clear(self): | ||
"""Reset the internal flag to false. Subsequently, coroutines calling | ||
wait() will block until set() is called to set the internal flag | ||
to true again.""" | ||
self._value = False | ||
self._exc = exc | ||
self._event.set() | ||
|
||
@asyncio.coroutine | ||
def wait(self): | ||
"""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. | ||
""" | ||
if self._value: | ||
return True | ||
val = yield from self._event.wait() | ||
if self._exc is not None: | ||
raise self._exc | ||
|
||
fut = create_future(self._loop) | ||
self._waiters.append(fut) | ||
try: | ||
yield from fut | ||
return True | ||
finally: | ||
self._waiters.remove(fut) | ||
return val |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ aiohttpdemo | |
aiopg | ||
alives | ||
api | ||
api’s | ||
app | ||
app’s | ||
arg | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters