-
-
Save andrey-yantsen/6eec43e66a22ee540ca326dc14620794 to your computer and use it in GitHub Desktop.
Async bulk
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
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