make pool an argument instead of progress handling; timeout=30

This commit is contained in:
Damien Elmes 2009-09-25 18:08:42 +09:00
parent 1028b04d9c
commit 0863acc160

View file

@ -2664,7 +2664,7 @@ backupDir = os.path.expanduser("~/.anki/backups")
class DeckStorage(object): class DeckStorage(object):
def Deck(path=None, backup=True, lock=True, progress=False): def Deck(path=None, backup=True, lock=True, pool=True):
"Create a new deck or attach to an existing one." "Create a new deck or attach to an existing one."
create = True create = True
if path is None: if path is None:
@ -2677,7 +2677,7 @@ class DeckStorage(object):
# sqlite needs utf8 # sqlite needs utf8
sqlpath = path.encode("utf-8") sqlpath = path.encode("utf-8")
try: try:
(engine, session) = DeckStorage._attach(sqlpath, create, progress) (engine, session) = DeckStorage._attach(sqlpath, create, pool)
s = session() s = session()
metadata.create_all(engine) metadata.create_all(engine)
if create: if create:
@ -2715,7 +2715,7 @@ class DeckStorage(object):
deck.needLock = lock deck.needLock = lock
deck.progressHandlerCalled = 0 deck.progressHandlerCalled = 0
deck.progressHandlerEnabled = False deck.progressHandlerEnabled = False
if progress: if pool:
try: try:
deck.engine.raw_connection().set_progress_handler( deck.engine.raw_connection().set_progress_handler(
deck.progressHandler, 100) deck.progressHandler, 100)
@ -2792,19 +2792,20 @@ class DeckStorage(object):
return deck return deck
Deck = staticmethod(Deck) Deck = staticmethod(Deck)
def _attach(path, create, progress=False): def _attach(path, create, pool=True):
"Attach to a file, initializing DB" "Attach to a file, initializing DB"
if path is None: if path is None:
path = "sqlite://" path = "sqlite://"
else: else:
path = "sqlite:///" + path path = "sqlite:///" + path
if progress: if pool:
engine = create_engine(path, # open and lock connection for single use
connect_args={'timeout': 0}) engine = create_engine(path, connect_args={'timeout': 0})
else: else:
# no pool & concurrent access w/ timeout
engine = create_engine(path, engine = create_engine(path,
poolclass=NullPool, poolclass=NullPool,
connect_args={'timeout': 90}) connect_args={'timeout': 30})
session = sessionmaker(bind=engine, session = sessionmaker(bind=engine,
autoflush=False, autoflush=False,
autocommit=True) autocommit=True)