Skip to content

Instantly share code, notes, and snippets.

@andrey-yantsen
Last active January 9, 2017 08:10
Show Gist options
  • Save andrey-yantsen/6eec43e66a22ee540ca326dc14620794 to your computer and use it in GitHub Desktop.
Save andrey-yantsen/6eec43e66a22ee540ca326dc14620794 to your computer and use it in GitHub Desktop.
Async bulk
class bulk:
def __init__(self, handler, size):
self.handler = handler
self.size = size
self.data = []
async def __aenter__(self):
return self
async def flush(self):
await self.handler(self.data)
self.data = []
async def append(self, item):
self.data.append(item)
if len(self.data) >= self.size:
await self.flush()
async def __aexit__(self, exc_type, exc_val, exc_tb):
if len(self.data):
await self.flush()
async def store(data: list):
for row in data:
print(row)
async with bulk(store, 100) as data:
for i in range(10000):
await data.append(i)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment