Skip to content

Commit

Permalink
Merge pull request #8
Browse files Browse the repository at this point in the history
* feat: integrate celery
  • Loading branch information
lihuacai168 authored Apr 20, 2024
1 parent e1374b6 commit 7691a22
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 0 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,28 @@ python manage.py runserver localhost:8000
## Authorize and request API
![img.png](assets/authorize.png)
![img_1.png](assets/request_api.png)

# Celery
## Config celery broker
```python
# setting.py
broker_url = "redis://127.0.0.1:6379/0"
```
## Run celery worker
```shell
# start celery worker, using command line
python -m celery -A apidemo.celery_config worker -l INFO
```

## PyCharm run_celery_worker_configuration
![pycharm_run_celery_worker_configuration](assets/celery_worker.png)


## Run celery beat
```shell
# start celery beat, using command line
python -m celery -A apidemo.celery_config beat -l DEBUG
```

## PyCharm run_celery_beat_configuration
![pycharm_run_celery_beat_configuration](assets/celery_beat.png)
93 changes: 93 additions & 0 deletions apidemo/celery_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# !/usr/bin/python3
# -*- coding: utf-8 -*-

import logging
import os
from datetime import timedelta
from logging.handlers import RotatingFileHandler

from celery import Celery, platforms, shared_task
from celery.schedules import crontab
from celery.signals import after_setup_logger

logger = logging.getLogger(__name__)

platforms.C_FORCE_ROOT = True
# https://www.celerycn.io/fu-lu/django
# set the default Django settings module for the 'celery' program.
# os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'rhea.settings')
app = Celery("proj")

# Using a string here means the worker doesn't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
# should have a `CELERY_` prefix.
settings_module_obj = os.getenv("DJANGO_SETTINGS_MODULE")
app.config_from_object(settings_module_obj, namespace="CELERY")

app.autodiscover_tasks()

app.conf.update(
CELERY_BEAT_SCHEDULER="django_celery_beat.schedulers:DatabaseScheduler",
task_reject_on_worker_lost=True,
task_acks_late=True,
# celery worker的并发数 根据并发量是适当配置,不易太大
CELERYD_CONCURRENCY=20,
# 每个worker执行了多少次任务后就会死掉,建议数量大一些
CELERYD_MAX_TASKS_PER_CHILD=300,
# 每个worker一次性拿的任务数
CELERYD_PREFETCH_MULTIPLIER=1,
)


@after_setup_logger.connect
def setup_loggers(logger, *args, **kwargs):
# fh = logging.FileHandler("logs/celery.log", "a", encoding="utf-8") # 这个是python3的
# 使用RotatingFileHandler替代FileHandler
# maxBytes设置为50MB,backupCount设置为5,意思是日志文件会轮转5个,每个日志文件最大50MB
fh = RotatingFileHandler(
"logs/celery.log",
"a",
maxBytes=50 * 1024 * 1024,
backupCount=5,
encoding="utf-8",
)

fh.setLevel(logging.INFO)

# 再创建一个handler,用于输出到控制台
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)

# 定义handler的输出格式
formatter = logging.Formatter(
"%(asctime)s %(levelname)s [pid:%(process)d] [%(name)s %(filename)s->%(funcName)s:%(lineno)s] %(message)s"
)
fh.setFormatter(formatter)
ch.setFormatter(formatter)

# 给logger添加handler
logger.addHandler(fh)
logger.addHandler(ch)


@shared_task(name="apidemo.celery_config.test.ping124")
def ping123():
print("pong.......")


@app.task(bind=True)
def test(self):
print("test task......")


app.conf.beat_schedule = {
"ping123": {
"task": "apidemo.celery_config.test.ping124",
"schedule": timedelta(seconds=3),
},
"test": {
"task": "apidemo.celery_config.test",
"schedule": timedelta(seconds=3),
},
}
5 changes: 5 additions & 0 deletions apidemo/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
'django.contrib.staticfiles',
'employee',
'ninja_jwt',
'django_celery_beat',
]

MIDDLEWARE = [
Expand Down Expand Up @@ -258,3 +259,7 @@
AUTHENTICATION_BACKENDS = [
'core.auth.CustomAuthBackend',
]


# celery broker
broker_url = "redis://127.0.0.1:6379/0"
Binary file added assets/celery_beat.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/celery_worker.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified db.sqlite3
Binary file not shown.
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ django-redis
gevent==22.10.2
gunicorn==20.1.0
django-ninja-jwt==5.2.7

django-celery-beat==2.6.0

0 comments on commit 7691a22

Please sign in to comment.