Skip to content

Commit 751f1e8

Browse files
committed
指定worker数为1,完成200个任务后重启worker
1 parent 4ee744e commit 751f1e8

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

configs/celery.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,30 @@
1+
# coding: utf-8
12
from __future__ import absolute_import
23

34
import os
4-
import logging
55

66
from celery import Celery
77
from django.conf import settings
88

9-
# set the default Django settings module for the 'celery' program.
10-
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'configs.settings')
11-
app = Celery('configs')
9+
# set the default Django settings module for the 'celery' program.
10+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'configs.settings')
11+
app = Celery('configs')
1212

13-
# Using a string here means the worker will not have to
14-
# pickle the object when using Windows.
15-
app.config_from_object('django.conf:settings', namespace='CELERY')
16-
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
13+
# Using a string here means the worker will not have to
14+
# pickle the object when using Windows.
15+
app.config_from_object('django.conf:settings', namespace='CELERY')
16+
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
1717

1818
app.conf.update(
1919
CELERY_TASK_SERIALIZER='json',
2020
CELERY_ACCEPT_CONTENT=['json'],
2121
CELERY_RESULT_SERIALIZER='json',
22+
CELERYD_CONCURRENCY=1,
23+
CELERY_WORKER_MAX_TASKS_PER_CHILD=200, # 完成一定数量后重启该worker
24+
CELERY_TIMEZONE='Asia/Shanghai'
2225
)
2326

24-
@app.task(bind=True)
25-
def debug_task(self):
26-
print('Request: {0!r}'.format(self.request))
27+
28+
@app.task(bind=True)
29+
def debug_task(self):
30+
print('Request: {0!r}'.format(self.request))

0 commit comments

Comments
 (0)