`
alfred_long
  • 浏览: 561104 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

后台处理beanstalkd 的python脚本

 
阅读更多
#!/usr/bin/python
#encoding=utf-8
import beanstalkc
import yaml
import json
import MySQLdb
#load config
handle = open('config.yaml')
conf = yaml.load(handle)
#get data from beanstalkd
def get_beanstalk_data(conf):
    beanstalk = beanstalkc.Connection(host=conf['beans']['host'],port=conf['beans']['port'])
    beanstalk.use('test')
    beanstalk.watch('test')
    job = beanstalk.reserve()
    data = json.loads(job.body)
    job.delete()
    return data
#insert data to mysql 
def insert_to_mysql(conf,data):
    try:
        conn = MySQLdb.connect(host=conf['mysql']['host'], user=conf['mysql']['user'],passwd=conf['mysql']['pass'], port=conf['mysql']['port'], db=conf['mysql']['db'])
        cursor = conn.cursor()
        sql = "insert into stat_plugin_log (id, md, cd, st) values(%s, %s, %s, %s)"
        param = (data['id'], data['md'], data['cd'], data['st'])
        cursor.execute(sql, param)
        cursor.close()
        conn.close()
    except MySQLdb.Error,e:
        print "Mysql Error %d: %s" % (e.args[0], e.args[1])
#go 
while True:
    data = get_beanstalk_data(conf)
    insert_to_mysql(conf,data)

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics