本文整理匯總了Python中torndb.Connection.executemany方法的典型用法代碼示例。如果您正苦於以下問題:Python Connection.executemany方法的具體用法?Python Connection.executemany怎麽用?Python Connection.executemany使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類torndb.Connection
的用法示例。
在下文中一共展示了Connection.executemany方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: run
# 需要導入模塊: from torndb import Connection [as 別名]
# 或者: from torndb.Connection import executemany [as 別名]
def run(self):
# init our varying wrappers
self.config = self.config_parser
rabbit_queue = self.conf('rabbitqueue')
rabbit_requeue = self.conf('rabbitrequeue')
rabbitmq_url = self.conf('rabbitmq_url')
client = puka.Client(rabbitmq_url)
promise = client.connect()
client.wait(promise)
promise = client.queue_declare(queue=rabbit_queue, durable=True)
client.wait(promise)
if rabbit_requeue:
promise = client.queue_declare(queue=rabbit_requeue, durable=True)
client.wait(promise)
consume_promise = client.basic_consume(queue=rabbit_queue, prefetch_count=1)
host = self.conf('mysql_host')
user = self.conf('mysql_user')
password = self.conf('mysql_password')
database = self.conf('mysql_database')
table = self.conf('mysql_table')
mysql_query_fields = self.conf('mysql_query_fields').split(",")
mysql_key_field = self.conf('mysql_key_field')
mysql_field_length = self.conf('mysql_field_length')
mysql_insert_query = self.conf('mysql_insert_query')
mysqldb = Connection(host, database, user=user, password=password)
while True:
try:
result = client.wait(consume_promise)
payload = json.loads(result['body'])
key = payload[mysql_key_field]
# Do some processing to get all they key/val pairs
for k, v in payload['body'].iteritems():
values = []
for field in mysql_query_fields:
if isinstance(payload[field], basestring):
values.append(payload[field].strip())
else:
values.append(payload[field])
if k == 'body':
values.append(key)
else:
values.append(k)
values.append(v)
logger.debug(k)
if len(v) > 0 and len(v) < int(mysql_field_length):
try:
mysqldb.executemany(mysql_insert_query, [values])
except Exception as e:
logger.error(e)
client.basic_ack(result)
if rabbit_requeue:
promise = client.basic_publish(exchange='', routing_key=rabbit_requeue, body=result['body'])
client.wait(promise)
except KeyboardInterrupt as e:
logger.error(e)
promise = client.close()
client.wait(promise)
mysqldb.close()
raise