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