-
-
Notifications
You must be signed in to change notification settings - Fork 34.9k
bpo-46622: Async support for cached_property #31314
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -963,6 +963,30 @@ def __isabstractmethod__(self): | |
| _NOT_FOUND = object() | ||
|
|
||
|
|
||
| class _CachedAwaitable: | ||
| """Wrapper to return if @cached_proeprty is used on a coroutine function. | ||
|
|
||
| A coroutine shouldn't simply be cached since it can only be awaited once. | ||
| This wrapper is cached instead, which awaits the underlying coroutine at | ||
| most once and cache the result for subsequent calls. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "and caches" |
||
| """ | ||
|
|
||
| def __init__(self, coro): | ||
| self.coro = coro | ||
| self.lock = RLock() | ||
| self.cache = _NOT_FOUND | ||
|
|
||
| def __await__(self): | ||
| if self.cache is not _NOT_FOUND: | ||
| return self.cache | ||
| with self.lock: | ||
| # check if another thread got the result while we awaited lock | ||
| if self.cache is not _NOT_FOUND: | ||
| return self.cache | ||
| self.cache = yield from self.coro.__await__() | ||
| return self.cache | ||
|
|
||
|
|
||
| class cached_property: | ||
| def __init__(self, func): | ||
| self.func = func | ||
|
|
@@ -980,6 +1004,8 @@ def __set_name__(self, owner, name): | |
| ) | ||
|
|
||
| def __get__(self, instance, owner=None): | ||
| import inspect | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please move the import just before usage, when the attribute name is not in cache yet. |
||
|
|
||
| if instance is None: | ||
| return self | ||
| if self.attrname is None: | ||
|
|
@@ -1000,6 +1026,8 @@ def __get__(self, instance, owner=None): | |
| val = cache.get(self.attrname, _NOT_FOUND) | ||
| if val is _NOT_FOUND: | ||
| val = self.func(instance) | ||
| if inspect.iscoroutinefunction(self.func): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would change the behaviour of class MyAwaitable:
def __await__(self):
print("called")
class Foo:
@cached_property
def get_awaitable(self):
return MyAwaitable()Currently this would cache the IMO |
||
| val = _CachedAwaitable(val) | ||
| try: | ||
| cache[self.attrname] = val | ||
| except TypeError: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import asyncio | ||
| import functools | ||
|
|
||
|
|
||
| def async_test(func): | ||
| """Decorator to turn an async function into a test case.""" | ||
| @functools.wraps(func) | ||
| def wrapper(*args, **kwargs): | ||
| coro = func(*args, **kwargs) | ||
| loop = asyncio.new_event_loop() | ||
| asyncio.set_event_loop(loop) | ||
| try: | ||
| return loop.run_until_complete(coro) | ||
| finally: | ||
| loop.close() | ||
| asyncio.set_event_loop_policy(None) | ||
| return wrapper |
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.
spelling: cached_property