我希望能够决定是否应该在运行时同步或异步执行函数,所以我编写了以下代码:
class DefaultUseCeleryPolicy(UseCeleryPolicy):
def use(self):
return anyCeleryWorkerRunning() or FORCE_CELERY
#--------------------------------------------------------------------------------------
class TaskDispatcher(object):
def __init__(self, usePolicy):
self.usePolicy = usePolicy
def dispatch(self, function, *args, **kwargs):
pass
#--------------------------------------------------------------------------------------
class CeleryTaskDispatcher(TaskDispatcher):
def __init__(self, usePolicy = DefaultUseCeleryPolicy()):
super(CeleryTaskDispatcher, self).__init__(usePolicy)
def dispatch(self, function, *args, **kwargs):
if self.usePolicy.use():
t = task(function)
tasks.register(t)
t.delay(args, kwargs)
else:
function(args, kwargs)
不幸的是,这不起作用(我从芹菜工人那里收到“收到未注册类型的任务”错误)。有可能实现我想要的吗?