Skip to content

Instantly share code, notes, and snippets.

@willkelly
Created November 14, 2014 04:50
Show Gist options
  • Save willkelly/17f959b8781c09fb8950 to your computer and use it in GitHub Desktop.
Save willkelly/17f959b8781c09fb8950 to your computer and use it in GitHub Desktop.
from sqlalchemy import create_engine, Column, Integer
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
Base = declarative_base()
engine = create_engine('postgresql+psycopg2://testman:testmanpass@localhost:5432/testman')
class TestMan(Base):
__tablename__ = "testman"
id = Column(Integer, primary_key=True)
value = Column(Integer)
Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
session1 = Session()
session2 = Session()
for i in xrange(10000):
session1.add(TestMan(value=i))
session1.commit()
results = session1.query(TestMan).execution_options(stream_results=True).filter(TestMan.value >= 0).yield_per(20)
for count, r in enumerate(results):
if count % 100 == 0:
print "%s" % count
session2.query(TestMan).filter(TestMan.value <= count).delete()
session2.commit()
print "Got here!"
session2.close()
print "SECOND GOT HERE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment