RabbitMQ自动消费

时间:2016-03-02 07:03:55

标签: rabbitmq

我已经编写了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()

1 个答案:

答案 0 :(得分:0)

你必须运行一个芹菜工作者,它将监听队列并执行你的代码

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