gpt4 book ai didi

python - Django Celery 任务运行两次

转载 作者:太空宇宙 更新时间:2023-11-03 15:48:06 27 4
gpt4 key购买 nike

我的项目目录结构如下:

rss_reader  
- reader
- __init__.py
- models.py
- views.py
- ...
- rss_reader
- __init__.py
- settings.py
- urls.py
- wsgi.py
- rss_contents
- __init__.py
- celery.py
- tasks.py
- config.py
- ...

任务.py

from __future__ import absolute_import
from rss_contents.celery import app

@app.task
def processArticle():
print "100"

celery .py

from __future__ import absolute_import
from celery import Celery

app = Celery('rss_contents', include=['rss_contents.tasks'])
app.config_from_object('rss_contents.config')

配置.py

from __future__ import absolute_import

CELERY_RESULT_BACKEND = 'redis://127.0.0.1:6379/5'
BROKER_URL = 'redis://127.0.0.1:6379/6'

阅读器> __init__.py

from rss_contents import tasks
tasks.processArticle.delay()
<小时/>

我的步骤如下:

  1. celery -A rss_contents worker -l信息

控制台显示:

[2017-01-11 10:34:33,829: INFO/MainProcess] Connected to redis://127.0.0.1:6379/6  
[2017-01-11 10:34:33,855: INFO/MainProcess] mingle: searching for neighbors
[2017-01-11 10:34:34,861: INFO/MainProcess] mingle: all alone
[2017-01-11 10:34:34,892: WARNING/MainProcess] celery@DESKTOP-6KAT7MF ready.
  • 运行服务器,在 http://127.0.0.1:8000/ 处启动开发服务器
  • 但是celery控制台显示任务运行了两次:

    [2017-01-11 10:41:20,910: INFO/MainProcess] Received task: rss_contents.tasks.processArticle[aa355c77-4ee8-4208-9e8c-915b110c7bbd]  
    [2017-01-11 10:41:20,911: WARNING/Worker-1] 100
    [2017-01-11 10:41:20,917: INFO/MainProcess] Task rss_contents.tasks.processArticle[aa355c77-4ee8-4208-9e8c-915b110c7bbd] succeeded in 0.00600004196167s: None

    [2017-01-11 10:41:22,430: INFO/MainProcess] Received task: rss_contents.tasks.processArticle[d92a151c-f0f9-4e8f-921a-fff2c1eb64c6]
    [2017-01-11 10:41:22,431: WARNING/Worker-1] 100
    [2017-01-11 10:41:22,447: INFO/MainProcess] Task rss_contents.tasks.processArticle[d92a151c-f0f9-4e8f-921a-fff2c1eb64c6] succeeded in 0.0160000324249s: None

    为什么会运行两次?我怎样才能做到一次?

    提前致谢。

    最佳答案

    这是因为您已在 __init__.py 文件中定义了任务调用。

    我已经开始使用您的代码进行测试,并在 __init__.py 中插入一个 pdb 中断,如下所示

    from celery_proj import tasks
    import pdb; pdb.set_trace() # <-- make a break point here
    print __file__
    tasks.processArticle.delay()

    并获得以下兴趣结果:

    当您运行python manage.py runserver时,断点会触发一次,并显示以下堆栈跟踪:

    (Pdb) where
    /Users/enix/Source/python/stackoverflow/sotest/manage.py(10)<module>()
    -> execute_from_command_line(sys.argv)
    /Users/enix/Source/python/bae/lib/python2.7/site-packages/django/core/management/__init__.py(353)execute_from_command_line()
    -> utility.execute()
    /Users/enix/Source/python/bae/lib/python2.7/site-packages/django/core/management/__init__.py(316)execute()
    -> autoreload.check_errors(django.setup)()
    /Users/enix/Source/python/bae/lib/python2.7/site-packages/django/utils/autoreload.py(226)wrapper()
    -> fn(*args, **kwargs)
    /Users/enix/Source/python/bae/lib/python2.7/site-packages/django/__init__.py(18)setup()
    -> apps.populate(settings.INSTALLED_APPS)
    /Users/enix/Source/python/bae/lib/python2.7/site-packages/django/apps/registry.py(85)populate()
    -> app_config = AppConfig.create(entry)
    /Users/enix/Source/python/bae/lib/python2.7/site-packages/django/apps/config.py(90)create()
    -> module = import_module(entry)
    /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py(37)import_module()
    -> __import__(name)
    > /Users/enix/Source/python/stackoverflow/sotest/app/__init__.py(4)<module>()
    -> print __file__

    当我输入 c 继续时,断点会使用相同的堆栈跟踪再次触发。

    所以你的任务被调用了两次。但我不太清楚为什么 django 会导入应用程序包两次。

    (bae)bogon:sotest enix$ python manage.py runserver
    /Users/enix/Source/python/stackoverflow/sotest/app/__init__.py # <-- print __file__
    /Users/enix/Source/python/stackoverflow/sotest/app/__init__.pyc # <-- print __file__
    Performing system checks...
    System check identified no issues (0 silenced).
    January 10, 2017 - 21:44:12
    Django version 1.9.12, using settings 'sotest.settings'
    Starting development server at http://127.0.0.1:8000/
    Quit the server with CONTROL-C.

    也许您应该将任务调用移至其他位置,然后重试。

    关于python - Django Celery 任务运行两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41582064/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com