zoukankan      html  css  js  c++  java
  • django中使用celery

    django-celery(python2)

    参考:http://docs.celeryproject.org/en/latest/getting-started/index.html

     

    配置(项目同名文件夹下):

     

    settings.py文件:

    import os
    import djcelery

    djcelery.setup_loader()
    BROKER_URL='redis://ip:port'
    # BROKER_POOL_LIMIT=0mm
    CELERY_RESULT_BACKEND='redis://ip:port'
    CELERY_ACCEPT_CONTENT=['application/json']
    CELERY_TASK_SERIALIZER='json'
    CELERY_RESULT_SERIALIZER='json'
    CELERY_TIMOZONE='Africa/Nairobi'

     

    celery.py文件:

    # -*- coding: utf-8 -*-

    from __future__ import absolute_import
    import os
    from celery import Celery
    from django.conf import settings

    # set the default Django settings module for the 'myimport' program.
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myimport.settings')
    app = Celery('myimport')

    # 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))

     

    __init.py__文件:

    from .celery import app as celery_app

     

    定义task.py文件:

    在各个app目录下定义app文件

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-

    from celery import task

    @task(name="share_start")
    def shareTask(username, website, keyword):

     

    运行:

    上图中配置好各文件后,直接在项目目录下执行命令:celery worker -A 项目同名目录名 -l info

    下图中myimport为项目同名文件夹的名字(即项目名)

     

     

     

     

  • 相关阅读:
    洛谷 题解 P5595 【【XR-4】歌唱比赛】
    洛谷 题解 CF1151D 【Stas and the Queue at the Buffet】
    洛谷 题解 CF299A 【Ksusha and Array】
    仙人掌找环
    2-SAT
    带花树
    帮我背单词
    csp2019退役祭
    P5284 [十二省联考2019]字符串问题 题解
    【网络流24题】魔术球问题
  • 原文地址:https://www.cnblogs.com/zealousness/p/8757755.html
Copyright © 2011-2022 走看看