-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Signals refacoting #2480
Merged
Merged
Signals refacoting #2480
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Signal handlers (registered callbacks) should be coroutines. |
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Drop undocumented `app.on_pre_signal` and `app.on_post_signal`. Signal | ||
handlers should be coroutines, support for regular functions is | ||
dropped. |
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,95 +1,32 @@ | ||
import asyncio | ||
from itertools import count | ||
|
||
from aiohttp.frozenlist import FrozenList | ||
|
||
|
||
class BaseSignal(FrozenList): | ||
|
||
__slots__ = () | ||
|
||
async def _send(self, *args, **kwargs): | ||
for receiver in self: | ||
res = receiver(*args, **kwargs) | ||
if asyncio.iscoroutine(res) or asyncio.isfuture(res): | ||
await res | ||
|
||
|
||
class Signal(BaseSignal): | ||
class Signal(FrozenList): | ||
"""Coroutine-based signal implementation. | ||
|
||
To connect a callback to a signal, use any list method. | ||
|
||
Signals are fired using the :meth:`send` coroutine, which takes named | ||
Signals are fired using the send() coroutine, which takes named | ||
arguments. | ||
""" | ||
|
||
__slots__ = ('_app', '_name', '_pre', '_post') | ||
__slots__ = ('_owner',) | ||
|
||
def __init__(self, app): | ||
def __init__(self, owner): | ||
super().__init__() | ||
self._app = app | ||
klass = self.__class__ | ||
self._name = klass.__module__ + ':' + klass.__qualname__ | ||
self._pre = app.on_pre_signal | ||
self._post = app.on_post_signal | ||
self._owner = owner | ||
|
||
def __repr__(self): | ||
return '<Signal owner={}, frozen={}, {!r}>'.format(self._owner, | ||
self.frozen, | ||
list(self)) | ||
|
||
async def send(self, *args, **kwargs): | ||
""" | ||
Sends data to all registered receivers. | ||
""" | ||
if self: | ||
ordinal = None | ||
debug = self._app._debug | ||
if debug: | ||
ordinal = self._pre.ordinal() | ||
await self._pre.send( | ||
ordinal, self._name, *args, **kwargs) | ||
await self._send(*args, **kwargs) | ||
if debug: | ||
await self._post.send( | ||
ordinal, self._name, *args, **kwargs) | ||
|
||
if not self.frozen: | ||
raise RuntimeError("Cannot send non-frozen signal.") | ||
|
||
class FuncSignal(BaseSignal): | ||
"""Callback-based signal implementation. | ||
|
||
To connect a callback to a signal, use any list method. | ||
|
||
Signals are fired using the :meth:`send` method, which takes named | ||
arguments. | ||
""" | ||
|
||
__slots__ = () | ||
|
||
def send(self, *args, **kwargs): | ||
""" | ||
Sends data to all registered receivers. | ||
""" | ||
for receiver in self: | ||
receiver(*args, **kwargs) | ||
|
||
|
||
class DebugSignal(BaseSignal): | ||
|
||
__slots__ = () | ||
|
||
async def send(self, ordinal, name, *args, **kwargs): | ||
await self._send(ordinal, name, *args, **kwargs) | ||
|
||
|
||
class PreSignal(DebugSignal): | ||
|
||
__slots__ = ('_counter',) | ||
|
||
def __init__(self): | ||
super().__init__() | ||
self._counter = count(1) | ||
|
||
def ordinal(self): | ||
return next(self._counter) | ||
|
||
|
||
class PostSignal(DebugSignal): | ||
|
||
__slots__ = () | ||
await receiver(*args, **kwargs) | ||
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
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
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about to add more friendly exception if plain func is going to be registered as handler? I guess now such code will fail with cryptic
TypeError: object NoneType can't be used in 'await' expression
or similar.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you suggest catching
TypeError
and raise a newTypeError
with more informative exception message?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As an option we could check ll callbacks on freezing stage.
It prevents registering a regular function which returns a future but it's fine I guess.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes for TypeError and yes, better check this early. On append call if possible. The stacktrace will help to find that bad signal usage with easy.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FrozenList can be modified by too many ways,
.append
is not the only one.I think
.freeze()
is a good compromise.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a pity. Ok then if error message would contains function name which is not async.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, but I see you point.
Maybe we need specialized
CheckedFrozenList
class with registered callback? It could improve UX slightly.Please let me merge the PR as is and make a new issue.
I don't want to block @pfreixes with his client tracing PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok. Let's do that.