我面临的一个问题是,我将一个任务放入队列中,并且它正在多次运行。从芹菜日志中我可以看到同一个工人在执行任务.
[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行日志。
我在RabbitMQ中使用Django 1.6。进入队列的方法是通过在函数上放置延迟。
此函数(添加了任务装饰器(然后调用正在运行的类)。
有谁知道解决这个问题的最佳方法吗?
编辑:根据请求,这里是代码,
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):
.... Codesettings.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谢谢,
发布于 2014-06-14 09:42:37
我没有确切的答案给你,但有几件事你应该研究:
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希望能帮上忙。
https://stackoverflow.com/questions/24085621
复制相似问题