Created
August 15, 2018 08:44
-
-
Save kthwaite/9f131d4eb48ca394b28f71a75bc2308f to your computer and use it in GitHub Desktop.
aiopg execute_batch implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
'''Async implementation of psycopg2.extras.execute_batch, for use with aiopg. | |
async with aiopg.connect(...) as conn: | |
async with conn.cursor() as cur: | |
await execute_batch(cur, 'SELECT ...', args) | |
''' | |
def _paginate(seq, page_size): | |
'''Consume an iterable and return it in chunks. | |
Every chunk is at most `page_size`. Never return an empty chunk. | |
''' | |
page = [] | |
it = iter(seq) | |
while True: | |
try: | |
for _ in range(page_size): | |
page.append(it.next()) | |
yield page | |
page = [] | |
except StopIteration: | |
if page: | |
yield page | |
return | |
async def execute_batch(cur, sql, argslist, page_size=100): | |
'''Execute groups of statements in fewer server roundtrips. | |
''' | |
for page in _paginate(argslist, page_size=page_size): | |
sqls = [await cur.mogrify(sql, args) for args in page] | |
await cur.execute(b';'.join(sqls)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment