我的应用程序不调用芹菜任务
我的设置.py:
CLICKHOUSE_HOST = '127.0.0.1'
CLICKHOUSE_PORT = '6379'
CELERY_BROKER_URL = 'redis://' + CLICKHOUSE_HOST + ':' + CLICKHOUSE_PORT + '/0'
CELERY_BROKER_TRANSPORT_OPTIONS = {'visibility_timeout': 60}
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TIMEZONE = "Australia/Tasmania"
CELERY_TASK_TRACK_STARTED = True
CELERY_TASK_TIME_LIMIT = 30 * 60
CELERY_RESULT_BACKEND = 'django-db'
CELERY_CACHE_BACKEND = 'django-cache'
这是referral_app目录中的init.py:
from __future__ import absolute_import
from .celery import app as celery_app
__all__ = ('celery_app',)
以下是我的观点,必须称之为芹菜任务:
class DefineView(View):
def get(self, request):
create_points.delay(1, 2)
return render(request, 'main/homepage.html', {})
这是我的任务(只是测试工作(:
from celery import shared_task
from referal_app.celery import app
@shared_task
def create_points(a, b):
with open('e:\txt.txt', 'w') as file:
for _ in range(1):
file.write(f'{a + b}')
return 1
以下是referr_app目录中的芹菜设置:
from __future__ import absolute_import
import os
from celery import Celery
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'referal_app.settings')
app = Celery('referal_app')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()
@app.task(bind=True)
def debug_task(self):
print(f'Request: {self.request!r}')
当我推出芹菜时,我看不到功能在减弱这里是celery -A referal_app worker -l INFO
命令输出:
[tasks]
. main.tasks.create_points
. referal_app.celery.debug_task
[2021-05-28 19:42:52,266: INFO/MainProcess] celery@DESKTOP-LM7RKH1 ready.
[2021-05-28 19:42:52,745: INFO/MainProcess] Received task: referal_app.celery.debug_task[e42e42e2-a3d2-4721-ae37-a35abfe31ca6]
[2021-05-28 19:42:52,747: INFO/MainProcess] Received task: main.tasks.create_points[22d35a11-de3e-4b68-8777-7e4b8756c3bd]
[2021-05-28 19:42:53,870: INFO/MainProcess] Received task: main.tasks.create_points[2211846f-7be7-49c1-9628-d4806fbd4bc0]
[2021-05-28 19:42:53,872: INFO/MainProcess] Received task: referal_app.celery.debug_task[181a599b-5b3b-4e7d-96f7-03f5a7a7d2f0]
[2021-05-28 19:42:54,329: INFO/SpawnPoolWorker-11] child process 11056 calling self.run()
[2021-05-28 19:42:54,337: INFO/SpawnPoolWorker-12] child process 11676 calling self.run()
[2021-05-28 19:42:54,338: INFO/SpawnPoolWorker-9] child process 6560 calling self.run()
[2021-05-28 19:42:54,342: INFO/SpawnPoolWorker-10] child process 16992 calling self.run()
您需要启动芹菜过程
运行此命令来测试芹菜(您需要将此命令运行到Django项目目录中(
celery -A <django_project> worker -l info
如果一切顺利,那么你就可以在生产中供应芹菜了
我建议使用supervisor,让我们安装安装监督员
sudo apt-get install supervisor
现在创建一个配置文件
sudo nano /etc/supervisor/conf.d/<app>.conf
这是的例子
[program:celery]
command=/home/<user>/miniconda3/envs/venv/bin/celery -A <django_project> worker -l info
directory=<django_project>
user=<user>
group=www-data
autostart=true
autorestart=true
stdout_logfile=/<log_dir>/log/access.log
redirect_stderr=true
numprocs=3
priority=998
最后,使用此命令更新supervisor
sudo supervisorctl reread
sudo supervisorctl update
运行以下命令来停止、启动和/或检查芹菜程序的状态(此处为应用程序名称芹菜(
sudo supervisorctl stop celery
sudo supervisorctl start celery
sudo supervisorctl status celery