7
7
from contextvars import ContextVar
8
8
from typing import Any
9
9
10
- # context for the currently running Automation; used by @atomic
10
+ # context for the currently running Automation; used by @uninterruptible
11
11
_CURRENT_AUTOMATION : ContextVar [Automation | None ] = ContextVar ('rosys_automation_current' , default = None )
12
12
13
13
14
- def atomic (func : Callable ):
14
+ def uninterruptible (func : Callable ):
15
15
"""Decorator to make an async function uninterruptible until it exits."""
16
16
@functools .wraps (func )
17
17
async def _wrapped (* args , ** kwargs ):
18
18
automation = _CURRENT_AUTOMATION .get ()
19
19
try :
20
20
if automation is not None :
21
- automation ._atomic_depth += 1
21
+ automation ._uninterruptible_depth += 1
22
22
result = await func (* args , ** kwargs )
23
23
finally :
24
24
if automation is not None :
25
- automation ._atomic_depth -= 1
25
+ automation ._uninterruptible_depth -= 1
26
26
await asyncio .sleep (0 )
27
27
return result
28
28
return _wrapped
@@ -47,7 +47,7 @@ def __init__(self,
47
47
self ._can_run .set ()
48
48
self ._stop = False
49
49
self ._is_waited = False
50
- self ._atomic_depth = 0 # >0 while inside an @atomic section
50
+ self ._uninterruptible_depth = 0 # >0 while inside an @uninterruptible section
51
51
52
52
@property
53
53
def is_running (self ) -> bool :
@@ -72,14 +72,14 @@ def __await__(self) -> Generator[Any, None, Any | None]:
72
72
iter_send , iter_throw = coro_iter .send , coro_iter .throw
73
73
send : Callable = iter_send
74
74
message : Any = None
75
- while not (self ._atomic_depth == 0 and self ._stop ):
75
+ while not (self ._uninterruptible_depth == 0 and self ._stop ):
76
76
try :
77
- while self ._atomic_depth == 0 and not self ._can_run .is_set () and not self ._stop :
77
+ while self ._uninterruptible_depth == 0 and not self ._can_run .is_set () and not self ._stop :
78
78
yield from self ._can_run .wait ().__await__ () # pylint: disable=no-member
79
79
except BaseException as err :
80
80
send , message = iter_throw , err
81
81
82
- if self ._atomic_depth == 0 and self ._stop :
82
+ if self ._uninterruptible_depth == 0 and self ._stop :
83
83
return None
84
84
85
85
try :
0 commit comments