Procházet zdrojové kódy

helping with asyncio debugging by provding proper names for tasks

Rodja Trappe před 3 roky
rodič
revize
aa0c781a80
2 změnil soubory, kde provedl 11 přidání a 8 odebrání
  1. 5 4
      nicegui/nicegui.py
  2. 6 4
      nicegui/timer.py

+ 5 - 4
nicegui/nicegui.py

@@ -1,4 +1,5 @@
 #!/usr/bin/env python3
+from collections import namedtuple
 from typing import Awaitable, Callable
 import asyncio
 
@@ -9,15 +10,15 @@ from . import globals
 from . import binding
 
 
-def create_task(coro) -> asyncio.tasks.Task:
+def create_task(coro, name: str) -> asyncio.tasks.Task:
     loop = asyncio.get_event_loop()
-    return loop.create_task(coro)
+    return loop.create_task(coro, name=name)
 
 @jp.app.on_event('startup')
 def startup():
-    globals.tasks.extend(create_task(t) for t in Timer.prepared_coroutines)
+    globals.tasks.extend(create_task(t.coro, name=t.name) for t in Timer.prepared_coroutines)
     Timer.prepared_coroutines.clear()
-    globals.tasks.extend(create_task(t) for t in Ui.startup_tasks if isinstance(t, Awaitable))
+    globals.tasks.extend(create_task(t, name='startup task') for t in Ui.startup_tasks if isinstance(t, Awaitable))
     [t() for t in Ui.startup_tasks if isinstance(t, Callable)]
     jp.run_task(binding.loop())
 

+ 6 - 4
nicegui/timer.py

@@ -1,13 +1,15 @@
 import asyncio
 import time
 import traceback
-from typing import Awaitable, Callable, Union
+from typing import Awaitable, Callable, List, Union
 
 from .binding import BindableProperty
 from .globals import tasks, view_stack
+from collections import namedtuple
 
+NamedCoroutine = namedtuple('NamedCoroutine', ['name', 'coro'])
 class Timer:
-    prepared_coroutines = []
+    prepared_coroutines: List[NamedCoroutine] = []
 
     active = BindableProperty()
 
@@ -60,6 +62,6 @@ class Timer:
         coroutine = timeout() if once else loop()
         event_loop = asyncio.get_event_loop()
         if not event_loop.is_running():
-            self.prepared_coroutines.append(coroutine)
+            self.prepared_coroutines.append(NamedCoroutine(str(callback), coroutine))
         else:
-            tasks.append(event_loop.create_task(coroutine))
+            tasks.append(event_loop.create_task(coroutine, name=str(callback)))