i have a real headache from trying to understand the cause of the following problem. We are using a combination of the following libraries:
- pyTelegramBotAPI to process requests in a multi-threaded way
- SQLAlchemy
- sqlite
The SQLAlchemy
was first using NullPool
and now is configured to utilize QueuePool
. I am also using the following idiom to have a new DB session firing up for each thread (as per my understanding)
Session = sessionmaker(bind=create_engine(classes.db_url, poolclass=QueuePool))
@contextmanager
def session_scope():
session = Session()
try:
yield session
session.commit()
except:
session.rollback()
raise
finally:
session.close()
@bot.message_handler(content_types=['document'])
def method_handler:
with session_scope() as session:
do_database_stuff_here(session)
Nevertheless I am still getting this annoying exception: (sqlite3.ProgrammingError) SQLite objects created in a thread can only be used in that same thread
Any ideas? ;) In particular, i don't get how it is possible for another tread to get somewhere in between the db operations...this is probably the reason of the pesky exception
update 1: if i change the poolclass
to SingletonThreadPool
, then there seems to be no more errors coming up. However, the documentation of SQLAlchemy
tells that it's not production rife.