浏览代码

allow decorator routes to be async, always running on main thread

Falko Schindler 3 年之前
父节点
当前提交
af1939df8f
共有 1 个文件被更改,包括 2 次插入2 次删除
  1. 2 2
      nicegui/routes.py

+ 2 - 2
nicegui/routes.py

@@ -24,7 +24,7 @@ def get(self, path: str):
 
     def decorator(func):
         @wraps(func)
-        def decorated(request: requests.Request):
+        async def decorated(request: requests.Request):
             args = {name: converter.convert(request.path_params.get(name)) for name, converter in converters.items()}
             parameters = inspect.signature(func).parameters
             for key in parameters:
@@ -38,7 +38,7 @@ def get(self, path: str):
                     args[key] = complex(args[key])
             if 'request' in parameters and 'request' not in args:
                 args['request'] = request
-            return func(**args)
+            return await func(**args) if inspect.iscoroutinefunction(func) else func(**args)
         self.add_route(routing.Route(path, decorated))
         return decorated
     return decorator