You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There is a pattern that seems to come more and more, and is that when an actor (or background service) runs, it needs to allocate some resources, that should be cleaned up if the actor stops (either normally or due to an exception).
The most reliable way to do this is to use context managers to ensure proper cleanup, but sometimes this is not done, or when done it implies adding some extra indentation to the run method.
This also happens to background services, so it can be generalized and implemented there instead, somehow, because background service don't one one_run() method, but could have many tasks.
Possible solutions
Any solution should probably involve a AsyncExitStack, so users can easily register cleaning up stuff (context managers or cleanup functions) that will be called automatically when the _run() method ends.
For example, in the Actor implementation:
classActor:
asyncdef_run_loop(self) ->None:
"""Run the actor's task continuously, managing restarts, cancellation, and termination. This method handles the execution of the actor's task, including restarts for unhandled exceptions, cancellation, or normal termination. Raises: asyncio.CancelledError: If the actor's `_run()` method is cancelled. Exception: If the actor's `_run()` method raises any other exception. BaseException: If the actor's `_run()` method raises any base exception. """_logger.info("Actor %s: Started.", self)
n_restarts=0whileTrue:
try:
awaitself._delay_if_restart(n_restarts)
asyncwithAsyncExitStack() asexit_stack:
awaitself._run(exit_stack)
_logger.info("Actor %s: _run() returned without error.", self)
exceptasyncio.CancelledError:
_logger.info("Actor %s: Cancelled.", self)
raiseexceptException: # pylint: disable=broad-except_logger.exception("Actor %s: Raised an unhandled exception.", self)
limit_str="∞"ifself._restart_limitisNoneelseself._restart_limitlimit_str=f"({n_restarts}/{limit_str})"ifself._restart_limitisNoneorn_restarts<self._restart_limit:
n_restarts+=1_logger.info("Actor %s: Restarting %s...", self, limit_str)
continue_logger.info(
"Actor %s: Maximum restarts attempted %s, bailing out...",
self,
limit_str,
)
raiseexceptBaseException: # pylint: disable=broad-except_logger.exception("Actor %s: Raised a BaseException.", self)
raisebreak_logger.info("Actor %s: Stopped.", self)
This helps avoiding the extra indentation, but also by requiring to take an exit stack, we are probably reminding users that they need to take care of cleanup.
type:enhancementNew feature or enhancement visitble to userspart:actorAffects an actor ot the actors utilities (decorator, etc.)
1 participant
Heading
Bold
Italic
Quote
Code
Link
Numbered list
Unordered list
Task list
Attach files
Mention
Reference
Menu
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
The issue
There is a pattern that seems to come more and more, and is that when an actor (or background service) runs, it needs to allocate some resources, that should be cleaned up if the actor stops (either normally or due to an exception).
The most reliable way to do this is to use context managers to ensure proper cleanup, but sometimes this is not done, or when done it implies adding some extra indentation to the run method.
This also happens to background services, so it can be generalized and implemented there instead, somehow, because background service don't one one
_run()
method, but could have many tasks.Possible solutions
Any solution should probably involve a
AsyncExitStack
, so users can easily register cleaning up stuff (context managers or cleanup functions) that will be called automatically when the_run()
method ends.For example, in the
Actor
implementation:Then some actor implementation can do:
This helps avoiding the extra indentation, but also by requiring to take an exit stack, we are probably reminding users that they need to take care of cleanup.
Beta Was this translation helpful? Give feedback.
All reactions