zoukankan      html  css  js  c++  java
  • celery Django 简单示例

    一、目录结构

    二、创建worker文件夹

    __init__.py

    # -*- coding:utf-8 -*-
    import os
    from celery import Celery, platforms
    # elery不能root用户启动解决(C_FORCE_ROOT environment)
    platforms.C_FORCE_ROOT = True

    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "upload_pro.settings")

    # 创建Celery Application

    celery_app = Celery('upload_pro')
    celery_app.config_from_object('worker.config')
    celery_app.autodiscover_tasks()


    def call_by_worker(func):
    """ 将任务在 Celery 中异步执行 """
    task = celery_app.task(func)
    return task.delay

    config.py

    # -*- coding:utf-8 -*-
    # 将此文件celery启动 windows celery worker -A worker --loglevel=info -P gevent
    # 将此文件celery启动 linux celery worker -A worker --loglevel=info
    # 本地测试
    # broker_url = 'redis://192.168.10.10:6379/8'
    broker_url = 'redis://:root1234@A@192.168.8.191:7007/11'
    broker_pool_limit = 1000 # Broker 连接池 默认为10

    timezone = 'Asia/Shanghai'
    accept_content = ['pickle', 'json']

    task_serializer = 'pickle'
    result_expires = 3600 # 任务过期时间

    # 本地测试
    # result_backend = 'redis://192.168.10.10:6379/8'
    result_backend = 'redis://:root1234@A@192.168.8.191:7007/11'
    result_serializer = 'pickle'
    result_cache_max = 1000 # 任务结果最大缓存数量

    worker_redirect_stdouts_level = 'INFO'

    三、启动 

    celery worker -A worker --loglevel=info -P gevent
    from worker import call_by_worker
    # celery worker -A worker --loglevel=info -P gevent
    
    # @call_by_worker
    def add():
        time.sleep(10)
        return 10
    
    
    
    @csrf_exempt
    def get_celery(request):
        if request.method == "GET":
            # 'application/x-zip-compressed'表示返回的文件格式是zip,根据需要替换
            res = add()
            response = HttpResponse(js_data, 'text/javascript')
            return response
        return JsonResponse({"code": 1001})
  • 相关阅读:
    python删除列表重复元素
    maven常用打包命令
    python开发之函数
    手把手教你用Strace诊断问题
    python引用列表--10
    Python中open函数怎么操作文件--9
    python数据操作--8
    图解源码之FutureTask篇(AQS应用)
    图解源码之java锁的获取和释放(AQS)篇
    图解线程池工作机制,手写线程池?
  • 原文地址:https://www.cnblogs.com/xiao-xue-di/p/13810635.html
Copyright © 2011-2022 走看看