[~] Refactor
This commit is contained in:
parent
081e49f3a5
commit
c99948eb95
@ -6,23 +6,54 @@ import kombu.utils.json
|
||||
from typing import (
|
||||
Any,
|
||||
Optional,
|
||||
Callable,
|
||||
)
|
||||
|
||||
def shared_task(*args: Any, **kwargs: Any) -> Any:
|
||||
return celery.shared_task(
|
||||
*args,
|
||||
base=Task,
|
||||
**kwargs
|
||||
)
|
||||
def shared_task(func: Optional[Callable[Any, Any]]=None, **kwargs: Any) -> Any:
|
||||
#@celery.shared_task(
|
||||
# base=Task,
|
||||
# **kwargs,
|
||||
#)
|
||||
#def decorator2(*args, **kwargs):
|
||||
# res = func(*args, **kwargs)
|
||||
#
|
||||
# if inspect.isawaitable(res):
|
||||
# return asyncio.run(res)
|
||||
# else:
|
||||
# return res
|
||||
#
|
||||
#def decorator(func2: Callable[Any, Any]):
|
||||
# nonlocal func
|
||||
#
|
||||
# if func is None:
|
||||
# func = func2
|
||||
#
|
||||
# return decorator2
|
||||
#
|
||||
#if func is None:
|
||||
# return decorator
|
||||
#else:
|
||||
# return decorator2
|
||||
return celery.shared_task(base=Task, **kwargs)
|
||||
|
||||
def is_async() -> bool:
|
||||
try:
|
||||
asyncio.get_running_loop()
|
||||
return True
|
||||
except RuntimeError:
|
||||
return False
|
||||
|
||||
class Task(celery.app.task.Task):
|
||||
def __call__(self, *args, **kwargs) -> Any:
|
||||
res = super().__call__(*args, **kwargs)
|
||||
if inspect.isawaitable(res):
|
||||
if inspect.isawaitable(res) and not is_async():
|
||||
return asyncio.run(res)
|
||||
else:
|
||||
return res
|
||||
|
||||
#def apply(self, *args, **kwargs):
|
||||
# return self.__call__(*args, **kwargs)
|
||||
|
||||
@classmethod
|
||||
def _loads(
|
||||
cls,
|
||||
|
Loading…
Reference in New Issue
Block a user