From 83a7f84497e6859af4effaf6d0f033e297ef34cf Mon Sep 17 00:00:00 2001 From: roks0n Date: Mon, 22 Jul 2024 16:06:50 +0200 Subject: [PATCH] feat: ability to call tasks directly w/o enqueue-ing them --- kolona/task.py | 17 ++++- requirements.txt | 153 +++++++++++++++++++++----------------- setup.cfg | 5 +- tests/test_integration.py | 9 +++ 4 files changed, 110 insertions(+), 74 deletions(-) diff --git a/kolona/task.py b/kolona/task.py index 93a8b2d..998e282 100644 --- a/kolona/task.py +++ b/kolona/task.py @@ -81,6 +81,9 @@ async def enqueue( await q.put(task_item) return task_item.id + async def __call__(self, *args, **kwargs): + return await self.func(*args, **kwargs) + class Task(GlobalTask): """ @@ -161,14 +164,22 @@ def task( """ def wrapper(func): - task = GlobalTask( + task_obj = GlobalTask( func, queue=queue, max_retries=max_retries, retry_intervals=retry_intervals, delay=delay, ) - update_wrapper(task, func) - return task + + async def callable_task(*args, **kwargs): + if kwargs.pop("enqueue", False): + return await task_obj.enqueue(*args, **kwargs) + else: + return await task_obj(*args, **kwargs) + + update_wrapper(callable_task, func) + callable_task.enqueue = task_obj.enqueue + return callable_task return wrapper diff --git a/requirements.txt b/requirements.txt index 10658d3..266d147 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,161 +1,176 @@ # -# This file is autogenerated by pip-compile with python 3.9 -# To update, run: +# This file is autogenerated by pip-compile with Python 3.8 +# by the following command: # # pip-compile requirements.in # -attrs==21.4.0 - # via pytest -black==22.3.0 - # via -r requirements.in -bleach==5.0.0 - # via readme-renderer -build==0.7.0 +backports-tarfile==1.2.0 + # via jaraco-context +black==24.4.2 # via -r requirements.in -certifi==2021.10.8 +build==1.2.1 + # via + # -r requirements.in + # pip-tools +certifi==2024.7.4 # via requests -cffi==1.15.0 +cffi==1.16.0 # via cryptography -charset-normalizer==2.0.12 +charset-normalizer==3.3.2 # via requests -click==8.1.3 +click==8.1.7 # via # black # pip-tools -commonmark==0.9.1 - # via rich -coverage[toml]==6.3.2 +coverage[toml]==7.6.0 # via pytest-cov -cryptography==37.0.2 +cryptography==43.0.0 # via secretstorage -docutils==0.18.1 +docutils==0.20.1 # via readme-renderer -flake8==4.0.1 +exceptiongroup==1.2.2 + # via pytest +flake8==7.1.0 # via # -r requirements.in # flake8-print # flake8-quotes -flake8-import-order==0.18.1 +flake8-import-order==0.18.2 # via -r requirements.in flake8-print==5.0.0 # via -r requirements.in -flake8-quotes==3.3.1 +flake8-quotes==3.4.0 # via -r requirements.in -idna==3.3 +idna==3.7 # via requests -importlib-metadata==4.11.3 +importlib-metadata==8.0.0 # via + # build # keyring # twine -iniconfig==1.1.1 +importlib-resources==6.4.0 + # via keyring +iniconfig==2.0.0 # via pytest -isort==5.10.1 +isort==5.13.2 # via -r requirements.in +jaraco-classes==3.4.0 + # via keyring +jaraco-context==5.3.0 + # via keyring +jaraco-functools==4.0.1 + # via keyring jeepney==0.8.0 # via # keyring # secretstorage -keyring==23.5.0 +keyring==25.2.1 # via twine -mccabe==0.6.1 +markdown-it-py==3.0.0 + # via rich +mccabe==0.7.0 # via flake8 -mypy==0.960 +mdurl==0.1.2 + # via markdown-it-py +more-itertools==10.3.0 + # via + # jaraco-classes + # jaraco-functools +mypy==1.11.0 # via -r requirements.in -mypy-extensions==0.4.3 +mypy-extensions==1.0.0 # via # black # mypy -packaging==21.3 +nh3==0.2.18 + # via readme-renderer +packaging==24.1 # via + # black # build # pytest -pathspec==0.9.0 +pathspec==0.12.1 # via black -pep517==0.12.0 - # via - # build - # pip-tools -pip-tools==6.6.0 +pip-tools==7.4.1 # via -r requirements.in -pkginfo==1.8.2 +pkginfo==1.10.0 # via twine -platformdirs==2.5.2 +platformdirs==4.2.2 # via black -pluggy==1.0.0 - # via pytest -py==1.11.0 +pluggy==1.5.0 # via pytest -pycodestyle==2.8.0 +pycodestyle==2.12.0 # via # flake8 # flake8-import-order # flake8-print -pycparser==2.21 +pycparser==2.22 # via cffi -pyflakes==2.4.0 +pyflakes==3.2.0 # via flake8 -pygments==2.12.0 +pygments==2.18.0 # via # readme-renderer # rich -pyparsing==3.0.8 - # via packaging -pytest==7.1.2 +pyproject-hooks==1.1.0 + # via + # build + # pip-tools +pytest==8.3.1 # via # -r requirements.in # pytest-asyncio # pytest-cov # pytest-mock -pytest-asyncio==0.18.3 +pytest-asyncio==0.23.8 # via -r requirements.in -pytest-cov==3.0.0 +pytest-cov==5.0.0 # via -r requirements.in -pytest-mock==3.7.0 +pytest-mock==3.14.0 # via -r requirements.in -readme-renderer==35.0 +readme-renderer==43.0 # via twine -requests==2.27.1 +requests==2.32.3 # via # requests-toolbelt # twine -requests-toolbelt==0.9.1 +requests-toolbelt==1.0.0 # via twine rfc3986==2.0.0 # via twine -rich==12.4.0 +rich==13.7.1 # via twine -secretstorage==3.3.2 +secretstorage==3.3.3 # via keyring -six==1.16.0 - # via bleach tomli==2.0.1 # via # black # build # coverage # mypy - # pep517 + # pip-tools # pytest -twine==4.0.0 +twine==5.1.1 # via -r requirements.in -types-docutils==0.18.3 +types-docutils==0.21.0.20240711 # via -r requirements.in -types-setuptools==57.4.17 +types-setuptools==71.0.0.20240722 # via -r requirements.in -typing-extensions==4.2.0 +typing-extensions==4.12.2 # via # black # mypy -urllib3==1.26.9 + # rich +urllib3==2.2.2 # via # requests # twine -webencodings==0.5.1 - # via bleach -wheel==0.37.1 +wheel==0.43.0 # via pip-tools -zipp==3.8.0 - # via importlib-metadata +zipp==3.19.2 + # via + # importlib-metadata + # importlib-resources # The following packages are considered to be unsafe in a requirements file: # pip diff --git a/setup.cfg b/setup.cfg index 63c58f8..d819237 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = kolona -version = 0.2.0 +version = 0.2.1 description = A minimalistic in-memory async Python Task queue long_description = file: README.md long_description_content_type = text/markdown @@ -35,7 +35,8 @@ version_scheme = "no-guess-dev" [flake8] max-line-length = 100 select = E,W,F,I,C -ignore = W503,E501,E203 # W503 is not PEP8 compliant and black doesnt always break lines well +# W503 is not PEP8 compliant and black doesnt always break lines well +ignore = W503,E501,E203 application-import-names = kolona, tests [isort] diff --git a/tests/test_integration.py b/tests/test_integration.py index 77d1da0..63cbdad 100644 --- a/tests/test_integration.py +++ b/tests/test_integration.py @@ -319,3 +319,12 @@ async def runner(): assert log_spy.warning.call_count == 1 assert queue.qsize() == 0 + + +async def test_calling_task_without_enqueing(): + @task() + async def random_task(): + return True + + result = await random_task() + assert result is True