美文网首页
django+celery+rabbitmq+centos7

django+celery+rabbitmq+centos7

作者: Ming_a221 | 来源:发表于2018-06-28 11:09 被阅读0次

    一 .rabbitmq搭建

        https://www.jianshu.com/p/ce725e41edab
        https://www.cnblogs.com/zhangningyang/p/8507745.html(推荐)
    

    二.rabbitmq远程登录

        https://www.jianshu.com/p/e3af4cf97820
        1)遇到的问题,报错:pika.exceptions.ProbableAuthenticationError,可以查看日志            ( **重要**):
    
        tail -f /var/log/rabbitmq/rabbit\@Minion.log
    
        =ERROR REPORT==== 27-Jun-2016::02:06:19 ===
        Error on AMQP connection <0.542.0> (192.168.243.131:43681 ->                 192.168.243.131:5672, state: starting):
        PLAIN login refused: user 'guest' can only connect via localhost
    
        解决办法:修改配置文件:%% {loopback_users, [<<"guest">>]},   为 {loopback_users,         []}     ,service rabbitmq restart,参考[http://www.rabbitmq.com/configure.html]                          (http://www.rabbitmq.com/configure.html)
    

    三.Django配置

    工程目录

    celery.py内容:

      from __future__ import absolute_import
    
      import os
      import django
    
      from celery import Celery
      from django.conf import settings
    
      os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'TestingPlatform.settings')
      django.setup()
    
      app = Celery('TestingPlatform')
    
      app.config_from_object('django.conf:settings')
      app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
    

    setting.py配置:

      #数据库调度
      # CELERYBEAT_SCHEDULER = 'djcelery.schedulers.DatabaseScheduler'
      BROKER_URL = 'amqp://admin:admin@x.x.x.x:5672//'
      # CELERY_RESULT_BACKEND = 'amqp://admin:admin@x.x.x.x:5672//'
      # schedules
      from datetime import timedelta
      CELERYBEAT_SCHEDULE = {
          'add-every-30-seconds': {
           'task': 'device.tasks.device_check',
           'schedule': timedelta(seconds=30),       # 每 30 秒执行一次
           'args': ()                           # 任务函数参数
          },
            # 'multiply-at-some-time': {
            #     'task': 'celery_app.task2.multiply',
            #     'schedule': crontab(hour=9, minute=50),   # 每天早上 9 点 50 分执行一次
            #     'args': (3, 7)                            # 任务函数参数
            # }
      }
    
    task 位置

    tasks.py内容:

      from __future__ import absolute_import
      from TestingPlatform.celery import app
      from utils import adb
      from .models import Device
    
      import logging
    
    
      @app.task
      def device_check():
          logging.info("device_check")
    

    启动:

      celery -A TestingPlatform worker -B -l debug 启动定时任务
    

    四.rabbitmq实现

    1.java

      http://www.cnblogs.com/leocook/p/mq_rabbitmq_5.html
    

    相关文章

      网友评论

          本文标题:django+celery+rabbitmq+centos7

          本文链接:https://www.haomeiwen.com/subject/igcgyftx.html