本文整理汇总了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