RabbitMQ自动消费

RabbitMQ Automatic Consuming

我写了 python 文件来在数据库中写入一些东西。 此任务正在添加到 RabbitMQ 队列中。 DB如何自动消费队列中的任务??


import MySQLdb
from celery import Celery
import pika


app2 = Celery('task2', broker='amqp://guest@localhost//')



@app2.task(queue='Test')
def update_db(): 
    # Open database connection
    db = MySQLdb.connect("localhost","root","root","test" )

    # prepare a cursor object using cursor() method
    cursor = db.cursor()

    # Prepare SQL query to DELETE required records
    sql = "insert into tt values(1,'james')"
    #print sql
    # Execute the SQL command
    cursor.execute(sql)
    # Commit your changes in the database
    db.commit()
        # disconnect from server
    db.close()

你必须 运行 一个 celery worker 来监听队列并执行你的代码

http://docs.celeryproject.org/en/latest/getting-started/first-steps-with-celery.html#running-the-celery-worker-server