python 解决本地 无法安装环境依赖问题
tools --- mapping(本地 /远程)
47.98.53.254:8787/code/?email=
http://47.98.53.254:8787/code/?email=7@qq.com
redis --- 数据库请求无效
celery----生产消费者模式(rabbitmq,kafka)
生产者 -- 缓冲区 (redis)-- 消费者
celery--分布式任务队列 --消息中间件 任务执行单元(worker) 任务存储结果(redis)
简单 高效 灵活
celery worker --help
celery安装及软连接
[root@iz2ze7pyqb098vwyxxnq3wz ~]# cd /usr/local/python3
[root@iz2ze7pyqb098vwyxxnq3wz python3]# ls
[root@iz2ze7pyqb098vwyxxnq3wz python3]# cd bin
[root@iz2ze7pyqb098vwyxxnq3wz bin]# ls
[root@iz2ze7pyqb098vwyxxnq3wz bin]# python -m pip install celery
[root@iz2ze7pyqb098vwyxxnq3wz bin]# ln -s /usr/local/python3/bin/celery /usr/bin/celery
ln: failed to create symbolic link ‘/usr/bin/celery’: File exists
[root@iz2ze7pyqb098vwyxxnq3wz bin]# rm -rf /usr/bin/celery
[root@iz2ze7pyqb098vwyxxnq3wz bin]# ln -s /usr/local/python3/bin/celery /usr/bin/celery
[root@iz2ze7pyqb098vwyxxnq3wz bin]# celery worker --help
CELERY_BROKER_URL = 'redis://localhost:6379/2'
#: Only add pickle to this list if your broker is secured
#: from unwanted access (see userguide/security.html)
CELERY_ACCEPT_CONTENT = ['json']
CELERY_RESULT_BACKEND = 'redis://localhost:6379/3'
CELERY_TASK_SERIALIZER = 'json'
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
# 这是wsgi.py中的数据copy下,配置
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'Celery_Django.settings')
app = Celery('Celery_Django')
app.config_from_object('django.conf:settings', namespace='CELERY')
# Load task modules from all registered Django app configs.
app.autodiscover_tasks()
创建Celey_Django\tasks.py
from __future__ import absolute_import, unicode_literals
from celery import shared_task
from Celery_Django import settings
from django.core.mail import send_mail
@shared_task
def send_register_email(email):
message = ''
title = '甲骨文的信息'
body = '<h1>for,欢迎成为甲骨文IT的精英</h1>'
try:
send_mail(title, message, settings.EMAIL_FROM, [email], html_message=body)
except Exception as e:
print(e)
from django.http import HttpResponse
from .tasks import send_register_email
def first_celery(req):
#任务函数的异步调用
send_register_email.delay("3@qq.com")
return HttpResponse("OK")
from __future__ import absolute_import, unicode_literals
from .celery import app as celery_app
__all__ = ('celery_app',)
celery -A django_v1 worker -l info
http://:8787/send_email/?email=
http://:8787/email/?email=
1.在项目settings目录下建立一个celery.py
2.settings.py设置celery环境变量
3.settings.py同级目录创建task.py
4.在settings.py创建views.py
5.填写url
6.celery -A django_v1 worker -l info 进入manage.py同级目录执行语句
7.启动djando项目
celery
最新推荐文章于 2023-10-26 01:17:13 发布