Скорее всего эту будем использовать:
Другие библиотеки:
- https://pypi.python.org/pypi/retry/
- https://pypi.python.org/pypi/retrying
- https://github.com/jd/tenacity
Еще:
- на SO есть декоратор конкретно для sqlalchemy, но, кажется, лучше все же вручную делать обработку
Примерно как здесь:
# session.begin() # больше не нужно вызывать .begin()
# there is no need to explcitly begin transactions when using modern Session programming patterns
try:
item1 = session.query(Item).get(1)
item2 = session.query(Item).get(2)
item1.foo = 'bar'
item2.bar = 'foo'
session.commit()
except:
session.rollback()
raiseЕще советики отсюда
- Use read-only database sessions if you know you do not need to modify the database and you need weaker transaction guarantees e.g. for displaying the total balance.
- Never do external actions, like sending emails, inside
managed_transaction. If the database transaction is replayed, the code is run twice and you end up sending the same email twice. - Managed transaction section should be as small and fast as possible.
- Avoid long-running transactions by splitting up big transaction to smaller worker batches.
Ищем все db.commit и переносим их в отдельную функцию с повторяющим декоратором. А внутри функции делать try: код, db.commit(), except: db.rollback()
Мб убрать expire_on_commit=False?
В итоге пришел к решению contextmanager отсюда http://docs.sqlalchemy.org/en/latest/orm/session_basics.html и еще
А db создается каждый раз в том contextmanager