Django导入芹菜任务和我的模​​型之间的循环

时间:2021-11-27 22:13:49

I am using celery with my django project.

我在我的django项目中使用芹菜。

In the celery tasks file, I need to import my models, in order to trigger model methods. However, I would also like my model to be able to trigger certain celery tasks.

在celery任务文件中,我需要导入我的模型,以便触发模型方法。但是,我也希望我的模型能够触发某些芹菜任务。

Right now I am importing my models to celery, however trying to import celery tasks into my models file results in an import loop and import error.

现在我将我的模型导入芹菜,但是尝试将芹菜任务导入我的模型文件会导致导入循环和导入错误。

What is the correct way to go about this?

这是怎么回事?

4 个解决方案

#1


8  

What I ended up doing, is using imports within methods, instead of a general import at the top of the models file. Obviously, i didn't really need circular imports. My problem was that I was importing the model at the top of the celery tasks file and importing the celery tasks at the top of the models file. That wasn't really necessary. by compartmentalizing the imports I was able to avoid the circular import problem

我最终做的是在方法中使用导入,而不是在模型文件顶部使用常规导入。显然,我并不真的需要循环进口。我的问题是我在celery任务文件的顶部导入模型,并在模型文件的顶部导入芹菜任务。那不是必要的。通过划分导入我能够避免循环导入问题

#2


2  

Celery provides the send_task() method, which allows to send a task by name, thus eliminating the need to import it - for example:

Celery提供了send_task()方法,该方法允许按名称发送任务,因此无需导入任务 - 例如:

# models.py
from celery import current_app
# no need to import do_stuff from tasks because it will be sent by name

current_app.send_task('myapp.tasks.do_stuff', args=(1, 'two'), kwargs={'foo': 'bar'})

More in the documentation.

更多文档。

#3


1  

The general approach to solve these seeming circular dependency problems, is to factor out code that can be imported by both the models and the tasks. For example, you could factor out the model methods that you mention. Your models would import this factored out code, and so would the tasks.

解决这些看似循环依赖问题的一般方法是分解可由模型和任务导入的代码。例如,您可以分解出您提到的模型方法。您的模型将导入此分解代码,任务也将导入。

#4


1  

How about not using a tasks.py file and just your applying the task decorators to methods in models.py?

如何不使用tasks.py文件,只是将任务装饰器应用于models.py中的方法?

#1


8  

What I ended up doing, is using imports within methods, instead of a general import at the top of the models file. Obviously, i didn't really need circular imports. My problem was that I was importing the model at the top of the celery tasks file and importing the celery tasks at the top of the models file. That wasn't really necessary. by compartmentalizing the imports I was able to avoid the circular import problem

我最终做的是在方法中使用导入,而不是在模型文件顶部使用常规导入。显然,我并不真的需要循环进口。我的问题是我在celery任务文件的顶部导入模型,并在模型文件的顶部导入芹菜任务。那不是必要的。通过划分导入我能够避免循环导入问题

#2


2  

Celery provides the send_task() method, which allows to send a task by name, thus eliminating the need to import it - for example:

Celery提供了send_task()方法,该方法允许按名称发送任务,因此无需导入任务 - 例如:

# models.py
from celery import current_app
# no need to import do_stuff from tasks because it will be sent by name

current_app.send_task('myapp.tasks.do_stuff', args=(1, 'two'), kwargs={'foo': 'bar'})

More in the documentation.

更多文档。

#3


1  

The general approach to solve these seeming circular dependency problems, is to factor out code that can be imported by both the models and the tasks. For example, you could factor out the model methods that you mention. Your models would import this factored out code, and so would the tasks.

解决这些看似循环依赖问题的一般方法是分解可由模型和任务导入的代码。例如,您可以分解出您提到的模型方法。您的模型将导入此分解代码,任务也将导入。

#4


1  

How about not using a tasks.py file and just your applying the task decorators to methods in models.py?

如何不使用tasks.py文件,只是将任务装饰器应用于models.py中的方法?