我试图异步地调用要完成的任务,因为它需要很长时间才能完成,并且如果我等待任务完成,则请求将超时(我正在对图像进行一些处理并获得结果) 。当我将打印件放入任务中时,我看到它正在执行,所以我知道这不是问题。当我保存模型时,它的行为就像保存的一样,但实际上并没有永久保存。
项目>项目> settings.py
local function myfunction() do_something() end
local API = {my = myfuncion}
项目>项目> 初始 .py
import environ
env = environ.Env()
CELERY_BROKER_URL = env.str('BROKER_URL', 'amqp://qisdvxct:b0iKxulu3z2evLScg1hnCNfWflDEBBm2@lion.rmq.cloudamqp.com/qisdvxct')
CELERY_RESULT_BACKEND = 'django-db'
INSTALLED_APPS = [
...
'django_celery_results',
]
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
MEDIA_URL = '%s.s3.amazonaws.com/media/' % AWS_STORAGE_BUCKET_NAME
DEFAULT_FILE_STORAGE = 'accounting.s3utils.MediaRootS3BotoStorage'
MEDIA_ROOT = MEDIA_URL
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
项目>项目> celery.py
from __future__ import absolute_import, unicode_literals
# This will make sure the app is always imported when
# Django starts so that shared_task will use this app.
from .celery import app as celery_app
__all__ = ('celery_app',)
项目>项目>任务.py
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'accounting.settings')
app = Celery('accounting')
# Using a string here means the worker doesn't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
# should have a `CELERY_` prefix.
app.config_from_object('django.conf:settings', namespace='CELERY')
# Load task modules from all registered Django app configs.
app.autodiscover_tasks()
@app.task(bind=True)
def debug_task(self):
print('Request: {0!r}'.format(self.request))
查看日志,似乎在任务中调用save()之后,该字段确实发生了更改(即使我再次输入Receipt.objects.get并检查该字段),但是如果我进入Django管理面板,更改未在此处反映出来。
为什么保存实际上没有保存到数据库?