2016-03-02 83 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() 

回答