Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
tanglang1990 committed Feb 16, 2019
0 parents commit b9ac617
Show file tree
Hide file tree
Showing 21 changed files with 508 additions and 0 deletions.
116 changes: 116 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version

# celery beat schedule file
celerybeat-schedule

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# ide
*.idea/


15 changes: 15 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true

[dev-packages]

[packages]
celery = {extras = ["redis"],version = "*"}
django = "*"
django-celery = "*"
redis = "==2.10.6"

[requires]
python_version = "3.6"
96 changes: 96 additions & 0 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
### The Project show how to use celery in Django
4 changes: 4 additions & 0 deletions imooc/celerybeat-schedule.bak
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
'entries', (0, 226)
'__version__', (512, 22)
'tz', (1024, 13)
'utc_enabled', (1536, 4)
Binary file added imooc/celerybeat-schedule.dat
Binary file not shown.
4 changes: 4 additions & 0 deletions imooc/celerybeat-schedule.dir
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
'entries', (0, 226)
'__version__', (512, 22)
'tz', (1024, 13)
'utc_enabled', (1536, 4)
Empty file added imooc/course/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions imooc/course/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
5 changes: 5 additions & 0 deletions imooc/course/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class CourseConfig(AppConfig):
name = 'course'
Empty file.
3 changes: 3 additions & 0 deletions imooc/course/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.db import models

# Create your models here.
12 changes: 12 additions & 0 deletions imooc/course/tasks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import time
from celery.task import Task


class CourseTask(Task):
name = 'course-task'

def run(self, *args, **kwargs):
print('start course task')
print('args={}, kwargs={}'.format(args, kwargs))
time.sleep(4)
print('end course task')
3 changes: 3 additions & 0 deletions imooc/course/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
11 changes: 11 additions & 0 deletions imooc/course/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from django.http import JsonResponse
from django.shortcuts import render

from .tasks import CourseTask


def do(request):
print('start request')
CourseTask.delay()
print('end request, start response')
return JsonResponse({'result': 'ok'})
Empty file added imooc/imooc/__init__.py
Empty file.
58 changes: 58 additions & 0 deletions imooc/imooc/celery_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
from datetime import timedelta

import djcelery

djcelery.setup_loader()

CELERY_QUEUES = {
'beat_tasks': {
'exchange': 'beat_tasks',
'exchange_tye': 'direct',
'binding_key': 'beat_tasks'
},
'work_queue': {
'exchange': 'work_queue',
'exchange_tye': 'direct',
'binding_key': 'work_queue'
}

}

CELERY_DEFAULT_QUEUE = 'work_queue'

CELERY_IMPORTS = (
'course.tasks',
)

# 有些情况下可以防止死锁
CELERY_FORCE_EXECV = True

# 设置并发的worker数量
CELERY_CONCURRENCY = 4

# 允许重试
CELERY_ACKS_LATE = True

# 每个worker最多执行100个任务被消费,可以防止内存泄漏
CELERYD_MAX_TASKS_PER_CHILD = 100

# 单个任务的最大执行时间
CELERYD_TASK_TIME_LIMIT = 12 * 30

# 启动消息队列命令
# python manage.py celery worker -l INFO


# 定时任务
CELERYBEAT_SCHEDULE = {
'task1': {
'task': 'course-task',
'schedule': timedelta(seconds=5),
'options': {
'queue': 'beat_tasks'
}
}
}

# 启动定时任务命令
# python manage.py celery beat -l INFO
Loading

0 comments on commit b9ac617

Please sign in to comment.