我想在django中定期运行代码。为此,我下载了芹菜并尝试配置它
添加到settings.py
INSTALLED_APPS = [
'django_celery_results',
'django_celery_beat',
}
CELERY_TIMEZONE = "Australia/Tasmania"
CELERY_TASK_TRACK_STARTED = True
CELERY_TASK_TIME_LIMIT = 30 * 60
CELERY_RESULT_BACKEND = 'django-db'
CELERY_RESULT_BACKEND = 'django-cache'
CELERY_CACHE_BACKEND = 'default'
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
'LOCATION': 'my_cache_table',
}
}
创建一个celery.py文件
import os
from celery import Celery
name='shop'
os.environ.setdefault('DJANGO_SETTINGS_MODULE', name+'.settings')
app = Celery(name)
app.config_from_object('django.conf:settings', namespace='CELERY')
app.conf.beat_schedule = {
'every-15-seconds':{
'task':'offers.tasks.send_email',
'schedule':15,
'args':('dsssew.com',)
}
}
app.conf.timezone = 'UTC'
app.autodiscover_tasks()
@app.task(bind=True)
def debug_task(self):
print(f'Request: {self.request!r}')
创建tasks.py文件
from __future__ import absolute_import, unicode_literals
from celery import shared_task
@shared_task
def send_email(email):
print(email)
@shared_task
def add(x, y):
return x + y
@shared_task
def mul(x, y):
return x * y
@shared_task
def xsum(numbers):
return sum(numbers)
依次启动所有内容
python manage.py runserver
celery -A shop worker -l INFO
celery -A shop beat -l INFO
beat bring me
芹菜击败v5.0.5(奇点)开始。__ -……__ - _本地时间->2021-02-18 20:31:57配置->. 代理→amqp://客人:* * @localhost: 5672//. 装载机→celery.loaders.app.AppLoader. 调度器→celery.beat.PersistentScheduler. 德国联邦铁路(db)→celerybeat-schedule. 日志文件→(stderr) @ %信息. maxinterval→5.00分钟(300s) [2021-02-18 20:31:57,062: INFO/MainProcess] beat: Starting…[2021-02-18 20:31:57,216:INFO/MainProcess] Scheduler:每15秒发送一次到期的任务(offers.tasks.send_email)
但是工人每隔15秒就会扔掉下一个
[2010-02-19 16:18:02275: WARNING/SpawnPoolWorker-1]c: 用户 admin appdata 本地python 程序 python37 lib 网站芹菜 app trace.py: 617:TypeError('ord() ')期望字符串长度为1,但找到int '):回溯(最近call last):文件"c: 用户 admin appdata 本地python 程序 python37 lib 网站芹菜 app trace.py",第400行,在trace_task中请求=task_request,文件"c:usersadminappdatalocalprogramspythonpython37libsite-packages芹菜backendsbase.py"第435行,在store_result中请求=请求,**kwargs)文件"c:usersadminappdatalocalprogramspythonpython37libsite-packages芹菜backendsbase.py",第851行,在_store_result中current_meta = self._get_task_meta_for(task_id)文件"c:usersadminappdatalocalprogramspythonpython37libsite-packages芹菜backendsbase.py",第869行,在_get_task_meta_for中meta = self.get(self.get_key_for_task(task_id))文件"c:usersadminappdatalocalprogramspythonpython37libsite-packagesdjango_celery_resultsbackendscache.py",第19行,get返回self.cache_backend.get(key)文件"c:usersadminappdatalocalprogramspythonpython37libsite-packagesdjangocorecachebackendsdb. pyquot;第51行,get回归自我。get_many((例子),版本)。get(key, default)文件"c:usersadminappdatalocalprogramspythonpython37libsite-packagesdjangocorecachebackendsdb.py"第59行,在get_manyself.validate_key(key)文件"c:usersadminappdatalocalprogramspythonpython37libsite-packagesdjangocorecachebackendsbase.py"第249行,validate_keymemcache_key_warnings(key): File "c:usersadminappdatalocalprogramspythonpython37libsite-packagesdjangocorecachebackendsbase.py"第287行,在memcache_key_warningsIf ord(char) <33或ord(char) == 127: TypeError: ord()期望字符串长度为1,但发现int
我不明白什么是错误和如何修复它?
@sortfact-尝试以下操作。它有很好的界面,你可以在管理中访问,并且有一些非常棒的配置,你可以用它来配置调度程序。
https://django-celery-beat.readthedocs.io/en/latest/