Skip to content

Commit

Permalink
perf($async): simplify @async_function decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnymillergh committed May 16, 2023
1 parent 2e8f285 commit e46a9c1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 38 deletions.
32 changes: 6 additions & 26 deletions python_boilerplate/common/asynchronization.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,33 +57,13 @@ def async_function(func: Callable[..., R]) -> Callable[..., Future[R]]:

@functools.wraps(func)
def wrapped(*arg: Any, **kwarg: Any) -> Future[R]:
future = executor.submit(func, *arg, **kwarg)
future.add_done_callback(done_callback)
module = inspect.getmodule(func)
if arg and not kwarg:
submitted_future = executor.submit(func, *arg)
logger.debug(
f"Submitted future task to run function asynchronously: "
f"{module}.{func.__qualname__}(*arg)"
)
elif not arg and kwarg:
submitted_future = executor.submit(func, **kwarg)
logger.debug(
f"Submitted future task to run function asynchronously: "
f"{module}.{func.__qualname__}(**kwarg)"
)
elif arg and kwarg:
submitted_future = executor.submit(func, *arg, **kwarg)
logger.debug(
f"Submitted future task to run function asynchronously: "
f"{module}.{func.__qualname__}(*arg, **kwarg)"
)
else:
submitted_future = executor.submit(func)
logger.debug(
f"Submitted future task to run function asynchronously: "
f"{module}.{func.__qualname__}()"
)
submitted_future.add_done_callback(done_callback)
return submitted_future
logger.debug(
f"Submitted future task to run function asynchronously: {future}, {module}.{func.__qualname__}"
)
return future

return wrapped

Expand Down
30 changes: 18 additions & 12 deletions tests/common/test_asynchronization.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from concurrent.futures import Future
from time import sleep

import arrow
from loguru import logger

from python_boilerplate.common.asynchronization import async_function
Expand All @@ -21,37 +20,44 @@ def another_async_function_without_args() -> None:


def test_async_function_expected_no_errors() -> None:
a_future: Future[str] = an_async_function(
arrow.now().__str__(), arrow.now().shift(days=-1).__str__()
)
a_future: Future[str] = an_async_function("argument #1", "argument #2")
assert a_future is not None
assert type(a_future) is Future
result = a_future.result()
assert a_future.done() is True
assert len(result) > 0
assert result == "Hello, got param1=argument #1, param2=argument #2"
logger.info(f"Got future result: {result}")


def test_async_function_pass_kwarg_expected_no_errors() -> None:
a_future: Future[str] = an_async_function(
param1=arrow.now().__str__(), param2=arrow.now().shift(days=-1).__str__()
param1="argument #1", param2="argument #2"
)
assert a_future is not None
assert type(a_future) is Future
result = a_future.result()
assert a_future.done() is True
assert len(result) > 0
assert result == "Hello, got param1=argument #1, param2=argument #2"
logger.info(f"Got future result: {result}")


def test_async_function_pass_arg_kwarg_expected_no_errors() -> None:
a_future: Future[str] = an_async_function(
arrow.now().__str__(), param2=arrow.now().shift(days=-1).__str__()
)
a_future: Future[str] = an_async_function("argument #1", param2="argument #2")
assert a_future is not None
assert type(a_future) is Future
result = a_future.result()
assert a_future.done() is True
assert len(result) > 0
assert result == "Hello, got param1=argument #1, param2=argument #2"
logger.info(f"Got future result: {result}")


def test_another_async_function_without_args_expected_no_errors() -> None:
try:
another_async_function_without_args()
except Exception as ex:
assert False, f"{another_async_function_without_args} raised an exception {ex}"
a_future: Future[None] = another_async_function_without_args()
assert a_future is not None
assert type(a_future) is Future
result = a_future.result()
assert a_future.done() is True
assert result is None

0 comments on commit e46a9c1

Please sign in to comment.