Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support tracking multiple tasks #148

Merged
merged 3 commits into from
Jul 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions rosys/analysis/tracking.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import asyncio
import uuid
from collections import defaultdict
from functools import wraps
from typing import Callable, ParamSpec, TypeVar

Expand All @@ -11,23 +13,28 @@
class Track:

def __init__(self) -> None:
self.stack: dict[int, str] = {}
self.stacks: defaultdict[int, dict[int, str]] = defaultdict(dict)

def __call__(self, f: Callable[_P, _T]) -> Callable[_P, _T]:
@wraps(f)
async def wrap(*args, **kwargs):
task_id = id(asyncio.current_task())
stack = self.stacks[task_id]
uid = uuid.uuid4().int
self.stack[uid] = f.__name__
stack[uid] = f.__name__
try:
return await f(*args, **kwargs)
finally:
del self.stack[uid]
del stack[uid]
if not stack:
del self.stacks[task_id]
return wrap

def ui(self) -> ui.label:
label = ui.label()
ui.timer(0.5, lambda: label.set_text(' → '.join(self.stack.values())))
return label
def ui(self) -> ui.markdown:
markdown = ui.markdown()
ui.timer(0.1, lambda: markdown.set_content('\n\n'.join(' → '.join(stack.values()).replace('_', r'\_')
for stack in self.stacks.values())))
return markdown


track = Track()
Loading