问题描述
我正在开发一个使用Celery的小型独立python应用程序。我已经使用django框架构建了这个,但我的应用程序只是后端。这意味着用户不需要访问我的站点,我的应用程序只是为了从芹菜接收任务队列和对数据库执行操作的目的而构建的。为了在数据库上执行操作,我需要使用Django模块。我想要做的是消除我的django应用程序的其余部分,并且只使用芹菜和django模块模块(包括运行这些依赖项)。
简而言之,我的简单芹菜应用程序将运行从我的redis代理接收指令,并在数据库中执行操作django模型。
是否可以这样做?如果是这样,那么如何?
这是我的项目结构:
myproject /
--manage.py
--myproject /
----celery.py
----models.py
----设置.py
----tasks.py
----urls.py
----wsgi.py
这是我的settings.py:
你有一个python脚本需要一些芹菜任务,而且您也需要Django ORM来进行数据库交互。
-
您可以设置django项目
-
创建一个应用程序为您的目的,包括在settings.py和您的应用程序中的models.py创建所需的模型。
ref: -
设置执行芹菜的环境。即,redis服务器。将djcelery与django项目集成。为芹菜任务目的。
您可以使用芹菜节拍进行定期任务。或延迟。
ref: -
您可以导入和使用django模型,如正常里面芹菜的任务。
-
而您可以使用
运行芹菜任务。芹菜-A任务工作者--loglevel =信息
ii。芹菜-A任务beat -l信息。如果您想要为定期执行的任务编写使用节拍。
-
如果任务需要立即或者在一段时间间隔后异步执行,可以使用task_name.delay()
调用内部的任务使用delay()
i的python脚本认为在脚本中使用djcelery,您可能需要在脚本中设置django env。
只是做django.setup()。
我认为这将帮助你解决你的问题。
I am developing a small independent python application which uses Celery. I have built this using django framework but my application is back end only. This means that the users do not need to visit my site and my application is built only for the purpose of receiving tasks queue from celery and performing operations on the database. In order to perform operations on the database, I need to use Django modules.
What I am trying to do is eliminate the rest of my django application and use ONLY celery and django models modules (including the dependencies required to run these).
In short, my simple celery application will be running receiving instructions from my redis broker and perform operations in database using django models.
Is is possible to do this? If so, how?
Here is my project structure:
myproject/
--manage.py
--myproject/
----celery.py
----models.py
----settings.py
----tasks.py
----urls.py
----wsgi.py
Here is my settings.py:
You have have a python script that requires some celery tasks and you need Django ORM too for the database interactions.
You can setup the django project
create an app for your purpose, include in settings.py and inside your app in models.py create the required models.ref : What minimal files i need to use django ORM
Set up the environment for executing celery. Ie, redis server. integrate "djcelery" with django project. for the celery task purpose.you can use celery beats for the periodic tasks. or delay.ref: http://docs.celeryproject.org/en/latest/django/first-steps-with-django.html
You can import and use the django models like normal inside the celery tasks.
And The celery tasks you can run using
i. celery -A tasks worker --loglevel=info
ii. celery -A tasks beat -l info. use beats if you want the tasks which are written for periodic execution.
If the tasks need to be executed asynchronously just immediately or after a time interval , you can use task_name.delay()call the tasks inside the python script using delay()i think to use djcelery in your script you may need to set up the django env inside the script.just Do django.setup().
i think this will help you to solve your problem.
这篇关于是否可以在我的项目中使用Django模型模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!