def chunked(seq, chunksize):
    """Yields items from an iterator in chunks."""
it = iter(seq)
while True:
    yield chain([next(it)], islice(it, chunksize - 1))
conn = engine.commit()
for chunk in chunked(records, 1000):
    with conn.begin():
        for rec in chunk:
            pass
How do I efficiently do a bulk insert-or-update with SQLAlchemy?