Skip to content

Commit

Permalink
Merge pull request #7 from huntflow/INT-368_add_async_run
Browse files Browse the repository at this point in the history
[INT-368] Add async run method
  • Loading branch information
KlochkovHF authored Oct 26, 2023
2 parents 879d35e + 5a8719e commit 00de4e9
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = yatq
version = 0.0.6
version = 0.0.7

[options]
packages = find:
Expand Down
2 changes: 1 addition & 1 deletion yatq/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .worker.cli import run
from .worker.cli import run, async_run
27 changes: 22 additions & 5 deletions yatq/worker/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def run_worker_cli(): # pragma: no cover
)


def run(
async def async_run(
worker_settings: Type[WorkerSettings],
queue_names: List[str],
logging_config: Optional[Dict] = None,
Expand All @@ -55,9 +55,9 @@ def run(

loop = asyncio.get_event_loop()

loop.run_until_complete(worker_settings.on_startup())
await worker_settings.on_startup()
try:
redis_client = loop.run_until_complete(worker_settings.redis_client())
redis_client = await worker_settings.redis_client()
worker = build_worker(
redis_client,
worker_settings.factory_cls,
Expand All @@ -72,6 +72,23 @@ def run(
for signum in stop_signals:
loop.add_signal_handler(signum, lambda: asyncio.create_task(worker.stop()))

loop.run_until_complete(worker.run())
await worker.run()
finally:
loop.run_until_complete(worker_settings.on_shutdown())
await worker_settings.on_shutdown()


def run(
worker_settings: Type[WorkerSettings],
queue_names: List[str],
logging_config: Optional[Dict] = None,
max_jobs: Optional[int] = None,
) -> None: # pragma: no cover
loop = asyncio.get_event_loop()
loop.run_until_complete(
async_run(
worker_settings=worker_settings,
queue_names=queue_names,
logging_config=logging_config,
max_jobs=max_jobs,
),
)

0 comments on commit 00de4e9

Please sign in to comment.