浏览代码

code review

Falko Schindler 2 年之前
父节点
当前提交
af3aa94459
共有 5 个文件被更改,包括 15 次插入13 次删除
  1. 1 1
      fly.toml
  2. 2 5
      nicegui/events.py
  3. 3 3
      nicegui/functions/refreshable.py
  4. 2 2
      nicegui/functions/timer.py
  5. 7 2
      nicegui/helpers.py

+ 1 - 1
fly.toml

@@ -25,7 +25,7 @@ strategy = "rolling"
   protocol = "tcp"
   script_checks = []
   [services.concurrency]
-    hard_limit = 20
+    hard_limit = 30
     soft_limit = 12
     type = "connections"
 

+ 2 - 5
nicegui/events.py

@@ -2,10 +2,8 @@ from dataclasses import dataclass
 from inspect import Parameter, signature
 from typing import TYPE_CHECKING, Any, Awaitable, BinaryIO, Callable, Dict, List, Optional, Union
 
-from nicegui.slot import Slot
-
 from . import background_tasks, globals
-from .helpers import KWONLY_SLOTS, is_coroutine
+from .helpers import KWONLY_SLOTS
 
 if TYPE_CHECKING:
     from .client import Client
@@ -281,7 +279,7 @@ def handle_event(handler: Optional[Callable[..., Any]],
         assert sender is not None and sender.parent_slot is not None
         with sender.parent_slot:
             result = handler() if no_arguments else handler(arguments)
-        if is_coroutine(result):
+        if isinstance(result, Awaitable):
             async def wait_for_result():
                 with sender.parent_slot:
                     await result
@@ -289,6 +287,5 @@ def handle_event(handler: Optional[Callable[..., Any]],
                 background_tasks.create(wait_for_result(), name=str(handler))
             else:
                 globals.app.on_startup(wait_for_result())
-
     except Exception as e:
         globals.handle_exception(e)

+ 3 - 3
nicegui/functions/refreshable.py

@@ -6,7 +6,7 @@ from typing_extensions import Self
 from .. import background_tasks, globals
 from ..dependencies import register_component
 from ..element import Element
-from ..helpers import KWONLY_SLOTS, is_coroutine
+from ..helpers import KWONLY_SLOTS, is_coroutine_function
 
 register_component('refreshable', __file__, 'refreshable.js')
 
@@ -19,7 +19,7 @@ class RefreshableTarget:
     kwargs: Dict[str, Any]
 
     def run(self, func: Callable[..., Any]) -> Union[None, Awaitable]:
-        if is_coroutine(func):
+        if is_coroutine_function(func):
             async def wait_for_result() -> None:
                 with self.container:
                     if self.instance is None:
@@ -65,7 +65,7 @@ class refreshable:
                 continue
             target.container.clear()
             result = target.run(self.func)
-            if is_coroutine(self.func):
+            if is_coroutine_function(self.func):
                 assert result is not None
                 if globals.loop and globals.loop.is_running():
                     background_tasks.create(result)

+ 2 - 2
nicegui/functions/timer.py

@@ -4,7 +4,7 @@ from typing import Any, Callable, Optional
 
 from .. import background_tasks, globals
 from ..binding import BindableProperty
-from ..helpers import is_coroutine
+from ..helpers import is_coroutine_function
 from ..slot import Slot
 
 
@@ -81,7 +81,7 @@ class Timer:
         try:
             assert self.callback is not None
             result = self.callback()
-            if is_coroutine(self.callback):
+            if is_coroutine_function(self.callback):
                 await result
         except Exception as e:
             globals.handle_exception(e)

+ 7 - 2
nicegui/helpers.py

@@ -21,10 +21,15 @@ if TYPE_CHECKING:
 KWONLY_SLOTS = {'kw_only': True, 'slots': True} if sys.version_info >= (3, 10) else {}
 
 
-def is_coroutine(object: Any) -> bool:
+def is_coroutine_function(object: Any) -> bool:
+    """Check if the object is a coroutine function.
+
+    This function is needed because functools.partial is not a coroutine function, but its func attribute is.
+    Note: It will return false for coroutine objects.
+    """
     while isinstance(object, functools.partial):
         object = object.func
-    return asyncio.iscoroutinefunction(object) or asyncio.iscoroutine(object)
+    return asyncio.iscoroutinefunction(object)
 
 
 def safe_invoke(func: Union[Callable[..., Any], Awaitable], client: Optional['Client'] = None) -> None: