Skip to content
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

Use iscoroutinefunction from inspect not asyncio #359

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 6 additions & 12 deletions cached_property.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
__license__ = "BSD"

from functools import wraps
from inspect import iscoroutinefunction
from time import time
import threading
import asyncio
Expand All @@ -24,21 +25,14 @@ def __get__(self, obj, cls):
if obj is None:
return self

if asyncio.iscoroutinefunction(self.func):
return self._wrap_in_coroutine(obj)
if iscoroutinefunction(self.func):
value = asyncio.ensure_future(self.func(obj))
else:
value = self.func(obj)

value = obj.__dict__[self.func.__name__] = self.func(obj)
obj.__dict__[self.func.__name__] = value
return value

def _wrap_in_coroutine(self, obj):
@wraps(obj)
def wrapper():
future = asyncio.ensure_future(self.func(obj))
obj.__dict__[self.func.__name__] = future
return future

return wrapper()


class threaded_cached_property:
"""
Expand Down
Loading