1.安装Celery
pip install celery
pip install django-celery
2.安装RabbitMQ
Centos7版安装
(CentOS7)Error: unable to connect to node rabbit@compute1: nodedown
Windows10版安装,参考用,最后Celery会启动失败
3.登陆RabbitMQ后台
- Centos7
# 注意防火墙是否开放端口
# 防火墙相关操作 https://www.jb51.net/article/147012.htm
http://ip:15672
- Windows10
http://localhost:15672
4.启动Celery Beat(相当于发送消息给RabbitMQ)
# 使用默认的celery.py启动
python3 manage.py celery beat -l info > ./logs/beat.log 2>&1
# 指定的celery.py启动,其中的 -A是指定文件
celery -A FasterRunner.mycelery beat -l info > ~/logs/beat.log 2>&1
image.png
4.启动Celery work(处理RabbitMQ中的消息)
# 使用默认的celery.py启动
celery multi start w1 -A FasterRunner -l info --logfile=./logs/worker.log
# 指定的celery.py启动,其中的 -A是指定文件
celery multi start w1 -A FasterRunner.mycelery -l info --logfile=./logs/worker.log
# mycelery .py文件
import os
from celery import Celery
from FasterRunner.settings import pro as settings
# 指定Django的配置
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'FasterRunner.settings.pro')
app = Celery('FasterRunner')
app.config_from_object(settings)
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
5.增加定时任务
image.png# 发送消息日志
(fastrunner) [root@izwz9awyk38uq20rb3czmnz FasterRunner]# tailf logs/beat.log
[2019-06-24 18:09:00,000: INFO/MainProcess] Scheduler: Sending due task 每分钟执行一次 (fastrunner.tasks.schedule_debug_suite)
[2019-06-24 18:10:00,000: INFO/MainProcess] Scheduler: Sending due task 每分钟执行一次 (fastrunner.tasks.schedule_debug_suite)
# 处理消息日志
(fastrunner) [root@izwz9awyk38uq20rb3czmnz FasterRunner]# tailf ./logs/worker.log
[2019-06-24 18:09:00,172: WARNING/Worker-5] Ran 1 test in 0.138s
[2019-06-24 18:09:00,173: WARNING/Worker-5] OK
[2019-06-24 18:09:00,173: DEBUG/Worker-5]
================== Variables & Output ==================
Type | Variable : Value
------ | ---------------- : ---------------------------
--------------------------------------------------------
[2019-06-24 18:09:00,199: INFO/MainProcess] Task fastrunner.tasks.schedule_debug_suite[adda0f06-ed65-470c-a7db-7042707f3fd6] succeeded in 0.19341683201491833s: None
[2019-06-24 18:10:00,004: INFO/MainProcess] Received task: fastrunner.tasks.schedule_debug_suite[5eecf0ce-afd5-4b2a-8d60-55a184f093bf]
[2019-06-24 18:10:00,024: WARNING/Worker-1] 111
[2019-06-24 18:10:00,024: WARNING/Worker-1] 111
[2019-06-24 18:10:00,027: INFO/Worker-1] Start to run testcase: 测试用例_登陆
[2019-06-24 18:10:00,028: WARNING/Worker-1] 登陆
[2019-06-24 18:10:00,028: DEBUG/Worker-1] call hook: ${setup_hook_prepare_kwargs($request)}
[2019-06-24 18:10:00,028: INFO/Worker-1] POST /api/user/login/
[2019-06-24 18:10:00,029: DEBUG/Worker-1] request kwargs(raw): {'headers': {'Content-Type': 'application/json'}, 'verify': False, 'json': {'username': 'fastrunner', 'password': 'fastrunner'}}
[2019-06-24 18:10:00,029: DEBUG/Worker-1] processed request:
> POST http://119.23.50.82:8000/api/user/login/
> kwargs: {'headers': {'Content-Type': 'application/json'}, 'verify': False, 'json': {'username': 'fastrunner', 'password': 'fastrunner'}, 'timeout': 120}
[2019-06-24 18:10:00,160: DEBUG/Worker-1]
================== request details ==================
url : 'http://ip:8000/api/user/login/'
method : 'POST'
headers : {'User-Agent': 'python-requests/2.21.0', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive', 'Content-Type': 'application/json', 'Content-Length': '52'}
start_timestamp : 1561371000.029365
verify : False
json : {'username': 'fastrunner', 'password': 'fastrunner'}
body : b'{"username": "fastrunner", "password": "fastrunner"}'
[2019-06-24 18:10:00,161: DEBUG/Worker-1]
================== response details ==================
status_code : 200
headers : {'Server': 'nginx', 'Date': 'Mon, 24 Jun 2019 10:10:00 GMT', 'Content-Type': 'application/json', 'Content-Length': '115', 'Connection': 'keep-alive', 'Vary': 'Accept, Origin', 'Allow': 'POST, OPTIONS', 'X-Frame-Options': 'SAMEORIGIN'}
content_size : 115
response_time_ms : 131.36
elapsed_ms : 128.769
encoding : None
content : b'{"code":"0001","success":true,"msg":"login success","token":"22b3bf6afd31435e2bdce609713ed7b9","user":"fastrunner"}'
content_type : 'application/json'
ok : True
url : 'http://ip:8000/api/user/login/'
reason : 'OK'
cookies : {}
text : '{"code":"0001","success":true,"msg":"login success","token":"22b3bf6afd31435e2bdce609713ed7b9","user":"fastrunner"}'
json : {'code': '0001', 'success': True, 'msg': 'login success', 'token': '22b3bf6afd31435e2bdce609713ed7b9', 'user': 'fastrunner'}
执行用例报告
网友评论