2016-03-02 147 views
0

我写了python文件在数据库中写入somethin。 此任务将添加到RabbitMQ队列中。 DB如何自动从队列中使用任务?RabbitMQ自动消费


import MySQLdb 
from celery import Celery 
import pika 


app2 = Celery('task2', broker='amqp://[email protected]//') 



@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() 

回答