v 0.1.1 若干优化,增加celery异步任务 demo

v 0.1.1 若干优化,增加celery异步任务 demo
This commit is contained in:
何全
2019-03-05 15:07:03 +08:00
parent a130ca70bd
commit 26a5fbe509
7 changed files with 175 additions and 5 deletions

View File

@@ -1,4 +1,5 @@
# 海豹
> django-base-templastes
> 因本项目开始时间为3月1日,是 国际海豹日,故项目起名为 海豹 seal
@@ -10,7 +11,7 @@
* v0.1
## 介绍
* 基于bootstrap4+django2.1+python3.7
* 基于bootstrap4+django2.1+python3.7(兼容3.6)+celery异步任务
* 会尽量多加一些注释
* 采用cbv开发方式提高开发效率
@@ -39,9 +40,33 @@ python manage.py createsuperuser
python manage.py runserver 0.0.0.0:80
```
* 扩展功能
```bash
#需要安装redis
#启动celery异步任务
cd seal
celery -B -A seal worker -l info
```
## 注意
* 如果想直接拿来做生产项目,请重新生成一个 settings 文件里面的 SECRET_KEY
* 时区问题
```python
##因为开启了时区,所以django在数据库里面保存的为 utc 时间, 调用的时候会帮你 转为 东八区, celery会自动识别时间
from django.utils import timezone
for i in Users.objects.all():
print(i.last_login) ## 直接读取时间,会是 utc时间,未转换, 如果需要处理 请注意
print(timezone.localtime(i.last_login).strftime("%Y-%m-%d %H:%M:%S")) ## 时间格式化为 正常时间
## 2019-03-05 06:41:18.040809+00:00
## 2019-03-05 14:41:18
```
### 售后服务
* 有问题 可以加QQ群 620176501 <a target="_blank" href="//shang.qq.com/wpa/qunwpa?idkey=bbe5716e8bd2075cb27029bd5dd97e22fc4d83c0f61291f47ed3ed6a4195b024"><img border="0" src="https://github.com/hequan2017/cmdb/blob/master/static/img/group.png" alt="django开发讨论群" title="django开发讨论群"></a>

View File

@@ -1,3 +1,36 @@
django==2.1.7
django-bootstrap4
django-pure-pagination
amqp==2.4.2
backcall==0.1.0
billiard==3.5.0.5
celery==4.2.1
certifi==2018.11.29
chardet==3.0.4
decorator==4.3.0
Django==2.1.7
django-bootstrap4==0.0.7
django-celery-beat==1.4.0
django-celery-results==1.0.4
django-pure-pagination==0.3.0
django-timezone-field==3.0
idna==2.8
ipython==6.4.0
ipython-genutils==0.2.0
jedi==0.12.0
kombu==4.4.0
parso==0.2.1
pexpect==4.6.0
pickleshare==0.7.4
prompt-toolkit==1.0.15
ptyprocess==0.5.2
Pygments==2.2.0
PyMySQL==0.9.3
python-crontab==2.3.6
python-dateutil==2.8.0
pytz==2018.4
redis==3.2.0
requests==2.21.0
simplegeneric==0.8.1
six==1.11.0
traitlets==4.3.2
urllib3==1.24.1
vine==1.2.0
wcwidth==0.1.7

View File

@@ -0,0 +1,6 @@
from .celery import app as celery_app
__all__ = ['celery_app']
import pymysql
pymysql.install_as_MySQLdb()

34
seal/celery.py Normal file
View File

@@ -0,0 +1,34 @@
from __future__ import absolute_import, unicode_literals
import os
from celery.schedules import crontab
from datetime import timedelta
from celery import Celery, platforms
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'seal.settings')
app = Celery('seal')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()
platforms.C_FORCE_ROOT = True
@app.task(bind=True)
def debug_task(self):
print('Request: {0!r}'.format(self.request))
##定时任务
app.conf.update(
CELERYBEAT_SCHEDULE={
'demo1': {
'task': 'system.tasks.system_demo',
'schedule': timedelta(seconds=10),
'args': [111]
},
'demo2': {
'task': 'system.tasks.system_demo',
'schedule': crontab(minute=00, hour=00,day_of_month=1),
'args': [222]
},
}
)

View File

@@ -39,6 +39,8 @@ INSTALLED_APPS = [
'system.apps.SystemConfig',
'assets.apps.AssetsConfig',
'bootstrap4',
'django_celery_results',
'django_celery_beat',
]
@@ -180,3 +182,29 @@ PAGINATION_SETTINGS = {
# 表格table 一页 展示数据
DISPLAY_PER_PAGE = 15
## celery 4
CELERY_RESULT_BACKEND = 'redis://localhost:6379/1'
# CELERY_RESULT_BACKEND = 'django-db'
CELERY_BROKER_URL = 'redis://localhost:6379/2'
CELERY_BEAT_SCHEDULER = 'django_celery_beat.schedulers:DatabaseScheduler'
CELERYD_CONCURRENCY = 4
CELERY_TIMEZONE = 'Asia/Shanghai'
CELERYD_MAX_TASKS_PER_CHILD = 10
CELERYD_FORCE_EXECV = True
# 设置默认不存结果
# CELERY_IGNORE_RESULT = True
CELERY_CREATE_MISSING_QUEUES = True
CELERY_DISABLE_RATE_LIMITS = True
CELERYD_TASK_SOFT_TIME_LIMIT = 600
CELERY_TASK_RESULT_EXPIRES = 600
CELERY_ENABLE_UTC = False
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
## 钉钉 报警机器人 地址 调用地方为 system.tasks.ding_ding_to_info
web_hook_url=""

45
system/tasks.py Normal file
View File

@@ -0,0 +1,45 @@
import logging
import requests
import json
from celery import shared_task
from system.models import Users
from seal import settings
logger = logging.getLogger('system_celery')
@shared_task
def system_demo(one):
##因为开启了时区,所以django在数据库里面保存的为 utc 时间, 调用的时候会帮你 转为 东八区, celery会自动识别时间
from django.utils import timezone
for i in Users.objects.all():
print(i.last_login) ## 直接读取时间,会是 utc时间,未转换,如果需要处理 请注意
print(timezone.localtime(i.last_login).strftime("%Y-%m-%d %H:%M:%S")) ## 时间格式化为 正常时间
print("celery定时任务demo 每分钟执行一遍",one)
return
@shared_task
def ding_ding_to_info(content,type=None):
"""
钉钉接口 异步调用 ding_ding_to_info.delay("报警1")
:param content: 文本内容
:param type:
:return:
"""
web_hook_url = getattr(settings, 'web_hook_url'),
headers = {'content-type': 'application/json'}
data = {
"msgtype": "text",
"text": {
"content": content
},
"at": {
"atMobiles": [
],
}
}
try:
r = requests.post(web_hook_url[0], data=json.dumps(data), headers=headers)
print(r.text)
except Exception as e:
logger.error(e)

View File

@@ -1,5 +1,4 @@
{% extends "base/base.html" %}
{% load i18n %}
{% load asset_filter %}
{% load bootstrap4 %}
{% load static %}