'yield from func() in async function python
I have function named get_session_ which simple yield session and after just close it.
Now i use decorator logger_decorator which wrap my get_session_ function but there is the problem if my get_session is async function i receive an TypeError: 'async_generator' object is not iterable
In case of get_session is sync function everything works fine. I try to solve it but i'm stuck with this.
Below are my functions:
@logger_decorator()
async def get_session_():
session_ = SessionLocal()
try:
yield session_
except Exception as e:
print('Session rollback because of exception: %s', e)
session_.rollback()
finally:
session_.close()
def logger_decorator(event_type=None, *args, **kwargs):
def wrapper(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
s_time = time.time()
ret = yield from func()
e_time = time.time()
duration = e_time - s_time
print('call function %s with duration %f' % (func.__name__, duration))
return ret
return wrapper
Solution 1:[1]
i solved it
async for i in func():
result = yield i
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dmitriy_kzn |
