forked from goodboy/tractor
Make info object a mapping type
Make the info object a `Mapping` to play nicer with static type checking. Simplify the task or actor context method lookup using a dict.log_task_context
parent
52efbfc2cd
commit
cf73283586
|
@ -2,6 +2,7 @@
|
||||||
Per process state
|
Per process state
|
||||||
"""
|
"""
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
from collections import Mapping
|
||||||
|
|
||||||
import trio
|
import trio
|
||||||
|
|
||||||
|
@ -16,21 +17,22 @@ def current_actor() -> 'Actor': # type: ignore
|
||||||
return _current_actor
|
return _current_actor
|
||||||
|
|
||||||
|
|
||||||
class ActorContextInfo:
|
class ActorContextInfo(Mapping):
|
||||||
"Dyanmic lookup for local actor and task names"
|
"Dyanmic lookup for local actor and task names"
|
||||||
|
_context_keys = ('task', 'actor')
|
||||||
|
|
||||||
|
def __len__(self):
|
||||||
|
return len(self._context_keys)
|
||||||
|
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
return iter(('task', 'actor'))
|
return iter(self._context_keys)
|
||||||
|
|
||||||
def __getitem__(self, key: str):
|
def __getitem__(self, key: str):
|
||||||
if key == 'task':
|
try:
|
||||||
try:
|
return {
|
||||||
return trio._core.current_task().name
|
'task': trio.hazmat.current_task,
|
||||||
except RuntimeError:
|
'actor': current_actor
|
||||||
# not inside `trio.run()` yet
|
}[key]().name
|
||||||
return 'no task context'
|
except RuntimeError:
|
||||||
elif key == 'actor':
|
# no local actor/task context initialized yet
|
||||||
try:
|
return f'no {key} context'
|
||||||
return current_actor().name
|
|
||||||
except RuntimeError:
|
|
||||||
# no local actor initialize yet
|
|
||||||
return 'no actor context'
|
|
||||||
|
|
Loading…
Reference in New Issue