本文整理汇总了Python中cqlengine.query.BatchQuery.add_query方法的典型用法代码示例。如果您正苦于以下问题:Python BatchQuery.add_query方法的具体用法?Python BatchQuery.add_query怎么用?Python BatchQuery.add_query使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cqlengine.query.BatchQuery
的用法示例。
在下文中一共展示了BatchQuery.add_query方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Batch
# 需要导入模块: from cqlengine.query import BatchQuery [as 别名]
# 或者: from cqlengine.query.BatchQuery import add_query [as 别名]
class Batch(BatchQuery):
'''
Performs a batch of insert queries using async connections
'''
def __init__(self, **kwargs):
self.instances = []
self._batch = BatchQuery()
def batch_insert(self, model_instance):
self.instances.append(model_instance)
def __enter__(self):
return self
def add_query(self, query):
self._batch.add_query(query)
def add_callback(self, fn, *args, **kwargs):
raise TypeError('not supported')
def execute(self):
promises = []
session = get_session()
for instance in self.instances:
query = instance.__dmlquery__(instance.__class__, instance)
query.batch(self._batch)
query.save()
for query in self._batch.queries:
statement = SimpleStatement(str(query))
params = query.get_context()
promises.append(session.execute_async(statement, params))
return [r.result() for r in promises]
def __exit__(self, exc_type, exc_val, exc_tb):
self.execute()