Skip to content

Commit f97e4fb

Browse files
committed
fix: make retry wrapper async
1 parent f61c918 commit f97e4fb

1 file changed

Lines changed: 5 additions & 2 deletions

File tree

tion_btle/tion.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import abc
22
import asyncio
3+
import inspect
34
import logging
45
import time
56
from typing import Callable, List, final
@@ -17,20 +18,22 @@ class MaxTriesExceededError(Exception):
1718

1819
def retry(retries: int = 2, delay: int = 0):
1920
def decor(f: Callable):
20-
def wrapper(*args, **kwargs):
21+
async def wrapper(*args, **kwargs):
2122
last_info_exception = None
2223
last_warning_exception = None
2324
for i in range(retries+1):
2425
try:
2526
_LOGGER.debug("Trying %d/%d: %s(args=%s,kwargs=%s)", i, retries, f.__name__, args, kwargs)
27+
if inspect.iscoroutinefunction(f):
28+
return await f(*args, **kwargs)
2629
return f(*args, **kwargs)
2730
except exc.BleakError as _e:
2831
next_message = "Will try again" if i < retries else "Will not try again"
2932
_LOGGER.warning("Got exception: %s. %s", str(_e), next_message)
3033
last_warning_exception = _e
3134
pass
3235
if delay > 0:
33-
time.sleep(delay)
36+
await asyncio.sleep(delay)
3437
else:
3538
_LOGGER.critical("Retry limit (%d) exceeded for %s(%s, %s)", retries, f.__name__, args, kwargs)
3639
if _LOGGER.level > logging.INFO and last_info_exception is not None:

0 commit comments

Comments
 (0)