Skip to content

Commit

Permalink
Add and use asyncio fire_and_forget utility function (#164)
Browse files Browse the repository at this point in the history
  • Loading branch information
ccrisan authored Dec 17, 2023
1 parent e4a0a93 commit e177ef1
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 8 deletions.
3 changes: 2 additions & 1 deletion qtoggleserver/core/api/funcs/ports.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
)
from qtoggleserver.slaves import devices as slaves_devices
from qtoggleserver.slaves import ports as slaves_ports
from qtoggleserver.utils import asyncio as asyncio_utils
from qtoggleserver.utils import json as json_utils


Expand Down Expand Up @@ -132,7 +133,7 @@ async def set_attr(attr_name: str, attr_value: Attribute) -> None:

# If value is supplied among attrs, use it to update port value, but in background and ignoring any errors
if value is not _none and port.is_enabled():
asyncio.create_task(port.transform_and_write_value(value))
asyncio_utils.fire_and_forget(port.transform_and_write_value(value))

await port.save()

Expand Down
3 changes: 2 additions & 1 deletion qtoggleserver/core/ports.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
NullablePortValue,
PortValue,
)
from qtoggleserver.utils import asyncio as asyncio_utils
from qtoggleserver.utils import dynload as dynload_utils
from qtoggleserver.utils import json as json_utils
from qtoggleserver.utils import logging as logging_utils
Expand Down Expand Up @@ -885,7 +886,7 @@ async def set_sequence(self, values: list[PortValue], delays: list[int], repeat:
self._sequence.start()

def _transform_and_write_value_fire_and_forget(self, value: NullablePortValue) -> None:
asyncio.create_task(self.transform_and_write_value(value))
asyncio_utils.fire_and_forget(self.transform_and_write_value(value))

async def _on_sequence_finish(self) -> None:
self.debug('sequence finished')
Expand Down
1 change: 1 addition & 0 deletions qtoggleserver/core/reverse.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ def enable(self) -> None:
logger.debug('starting wait loop')

self._enabled = True
# TODO: properly handle session loop task
asyncio.create_task(self._session_loop())

def disable(self) -> None:
Expand Down
8 changes: 4 additions & 4 deletions qtoggleserver/slaves/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ async def update_cached_attrs(self, attrs: Attributes, partial: bool = False) ->
# Upon renaming, what be basically do is remove the existing device and add it from scratch.
# This is being taken care of by the following handle_rename() calls, respectively.
await self._handle_rename(name)
asyncio.create_task(_handle_rename(self, name))
asyncio_utils.fire_and_forget(_handle_rename(self, name))

raise exceptions.DeviceRenamed(self)
else:
Expand Down Expand Up @@ -284,7 +284,7 @@ async def enable(self) -> None:
# We can't await for the _load_ports() result, because we expect callers of enable() to generate
# slave-device-update events, and we want any port-related events generated by _load_ports() to come after.
if self.is_permanently_offline() and self._name:
asyncio.create_task(self._load_ports())
asyncio_utils.fire_and_forget(self._load_ports())

async def disable(self) -> None:
if not self._enabled:
Expand Down Expand Up @@ -329,7 +329,7 @@ def set_poll_interval(self, poll_interval: Optional[int]) -> None:
if self._online:
# Take offline
self._online = False
asyncio.create_task(self._handle_offline())
asyncio_utils.fire_and_forget(self._handle_offline())

def get_poll_interval(self) -> Optional[int]:
return self._poll_interval
Expand Down Expand Up @@ -357,7 +357,7 @@ def disable_listen(self) -> None:
if self._online:
# Take offline
self._online = False
asyncio.create_task(self._handle_offline())
asyncio_utils.fire_and_forget(self._handle_offline())

def is_listen_enabled(self) -> bool:
return self._listen_enabled
Expand Down
11 changes: 9 additions & 2 deletions qtoggleserver/utils/asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import queue
import sys
import threading
import weakref

from typing import Any, Awaitable, Callable, Optional, Union

Expand Down Expand Up @@ -152,6 +153,12 @@ async def stop(self) -> None:
await self._stopped_future


async def await_later(delay: float, aw: Awaitable, loop: asyncio.AbstractEventLoop = None) -> None:
await asyncio.sleep(delay, loop=loop)
async def await_later(delay: float, aw: Awaitable) -> None:
await asyncio.sleep(delay)
await aw


def fire_and_forget(aw: Awaitable) -> None:
task = asyncio.create_task(aw)
coro = task.get_coro()
weakref.finalize(task, coro.close)

0 comments on commit e177ef1

Please sign in to comment.