[英]Celery / Django Single Tasks being run multiple times
我正面臨一個問題,我將一個任務放入隊列,並且它正在運行多次。 從芹菜日志中我可以看到同一個工人正在執行任務......
[2014-06-06 15:12:20,731: INFO/MainProcess] Received task: input.tasks.add_queue
[2014-06-06 15:12:20,750: INFO/Worker-2] starting runner..
[2014-06-06 15:12:20,759: INFO/Worker-2] collection started
[2014-06-06 15:13:32,828: INFO/Worker-2] collection complete
[2014-06-06 15:13:32,836: INFO/Worker-2] generation of steps complete
[2014-06-06 15:13:32,836: INFO/Worker-2] update created
[2014-06-06 15:13:33,655: INFO/Worker-2] email sent
[2014-06-06 15:13:33,656: INFO/Worker-2] update created
[2014-06-06 15:13:34,420: INFO/Worker-2] email sent
[2014-06-06 15:13:34,421: INFO/Worker-2] FINISH - Success
但是當我查看應用程序的實際日志時,每個步驟顯示5-6個日志行(??)。
我使用Django 1.6與RabbitMQ。 放入隊列的方法是通過在函數上放置延遲。
這個函數(添加了任務裝飾器(然后調用一個運行的類)。
有沒有人知道解決這個問題的最佳方法?
編輯 :根據要求繼承代碼,
views.py
在我看來,我通過...將數據發送到隊列
from input.tasks import add_queue_project
add_queue_project.delay(data)
tasks.py
from celery.decorators import task
@task()
def add_queue_project(data):
""" run project """
logger = logging_setup(app="project")
logger.info("starting project runner..")
f = project_runner(data)
f.main()
class project_runner():
""" main project runner """
def __init__(self,data):
self.data = data
self.logger = logging_setup(app="project")
def self.main(self):
.... Code
settings.py
THIRD_PARTY_APPS = (
'south', # Database migration helpers:
'crispy_forms', # Form layouts
'rest_framework',
'djcelery',
)
import djcelery
djcelery.setup_loader()
BROKER_HOST = "127.0.0.1"
BROKER_PORT = 5672 # default RabbitMQ listening port
BROKER_USER = "test"
BROKER_PASSWORD = "test"
BROKER_VHOST = "test"
CELERY_BACKEND = "amqp" # telling Celery to report the results back to RabbitMQ
CELERY_RESULT_DBURI = ""
CELERY_IMPORTS = ("input.tasks", )
celeryd
我正在運行的線是開始芹菜,
python2.7 manage.py celeryd -l info
謝謝,
我沒有給你一個確切的答案,但你應該研究一些事情:
djcelery
已被棄用,因此如果您使用新版本的celery
,可能會出現某種沖突。
如果您的input
應用程序在INSTALLED_APPS
列出,芹菜會發現它,所以您不需要將它添加到CELERY_IMPORTS = ("input.tasks", )
,這可能是您的問題的原因,因為任務可以多次加載
嘗試給你的任務命名@task(name='input.tasks.add')
,無論你如何導入它,都會知道它是同一個任務。
看看你的設置,看起來你正在使用舊版本的芹菜,或者你正在使用新配置的新版芹菜。 在任何情況下,請確保您擁有最新版本並嘗試此配置而不是您擁有的:
BROKER_URL = 'amqp://<user>:<password>@localhost:5672/<vhost>'
CELERY_RESULT_BACKEND = 'amqp'
CELERY_ACCEPT_CONTENT = ['json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
現在,您還必須以不同方式配置芹菜:
完全擺脫djcelery
東西。
在你的django項目中創建proj/celery.py
:
from __future__ import absolute_import
import os
from celery import Celery
from django.conf import settings
# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'settings')
app = Celery('proj')
# Using a string here means the worker will not have to
# pickle the object when using Windows.
app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
@app.task(bind=True)
def debug_task(self):
print('Request: {0!r}'.format(self.request))
在你的proj/__init__.py
:
from __future__ import absolute_import
from proj.celery import app as celery_app
然后,如果您的input
應用程序是可重用的應用程序並且不屬於您的項目,請使用@shared_task
而不是@task
裝飾器。
然后運行芹菜:
celery -A proj worker -l info
希望能幫助到你。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.