本文介绍了Celery动态任务/隐藏Celery实现后面的界面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚如何使用Celery实现我的异步作业,而不是将它们绑定到Celery实现。

I am trying to figure out how to implement my asynchronous jobs with Celery, without tying them to the Celery implementation.

如果我有一个接受对象的接口调度,例如callables(或包装可调用的对象):

If I have an interface that accepts objects to schedule, such as callables (Or an object that wraps a callable):

ITaskManager(Interface):
    def schedule(task):
        #eventually run task

我可以用treading模块实现它:

And I might implement it with the treading module:

ThreadingTaskManager(object)
    def schedule(task):
        Thread(task).start() # or similar

但似乎芹菜无法做到这一点,对吗?

But it seems this couldn't be done with celery, am I right?

推荐答案

也许一个,虽然相当丑陋,解决方案可能是定义一个芹菜任务,它动态加载作为传递的任务对象一个参数:

Perhaps one, albeit quite ugly, solution might be to define one celery task which dynamically loads the task object that is passed as an argument:

@celery.task
def taskrunner(taskname):
    taskModule = __import__(taskname)
    taskModule.run()

CeleryTaskManager(object)
    def schedule(task):
        taskrunner.delay(task.__file__)


from mytask import run

CeleryTaskManager().schedule(run)

这篇关于Celery动态任务/隐藏Celery实现后面的界面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 11:04