Skip to content

Commit

Permalink
async_helpers: add way to associate a coroutine to an urwid signal
Browse files Browse the repository at this point in the history
Signed-off-by: Olivier Gayot <[email protected]>
  • Loading branch information
ogayot committed Jun 19, 2024
1 parent 30b54da commit ad11418
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions subiquitycore/async_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
import asyncio
import concurrent.futures
import logging
from functools import partial

from urwid import connect_signal

log = logging.getLogger("subiquitycore.async_helpers")

Expand Down Expand Up @@ -45,6 +48,14 @@ def schedule_task(coro, propagate_errors=True):
background_tasks = set()


def connect_async_signal(obj, name, callback, **kwargs):
connect_signal(obj, name, partial(async_signal_callback, callback), **kwargs)


def async_signal_callback(callback, *args, **kwargs):
run_bg_task(callback(*args, **kwargs))


def run_bg_task(coro, **kwargs) -> None:
"""Run a background task in a fire-and-forget style."""
task = asyncio.create_task(coro, **kwargs)
Expand Down

0 comments on commit ad11418

Please sign in to comment.