Open mei-li opened 6 years ago
Do you connect to the database in your app's init()
/main()
phase (i.e. before the application forks or spawns threads)? This error looks like you do. SqlAlchemy connections are not safe to cross threads/forks/processes, and you need to call engine.dispose()
before the "fork". Calling dispose
will get rid of the engine's connection, so each process/thread will have it's own isolated connection after the "fork".
Thank you for you response. We actually avoided using database calls inside the different threads as we call them only to parallelise some web requests. Though we used scoped session that is a thread local and each thread uses another session, which seems to be an appropriate use. so I wonder whether the connection should not be assumed to be used by one thread.
Hi again, It is becoming hard to avoid having to do DB queries in the various threads. My understanding from reading about it is that we can initiate new sessions, but we would stick to the same connection and that would still be an issue with debug toolbar. What you mention about engine.dispose() I understand from sqlalchemy docs makes sense in a multiprocessing context and seems to be expensive too. In our case we spawn threads to parallelize processing for short time. Do you think there is a way to have the debugtoolbar depending on the session instead of the connection or sth else that would work?
Can you show some sort of snippet of what’s going on? Are you passing a session between threads? That’s not supported by SQLAlchemy at all. They are not threadsafe.
Sqa.py code is adding to the DB connection the attribute
pdtb_start_timer
hereThen it removes this attribute. When accessing the DB with multiple threads this code breaks if both threads are executing and then the first that reaches the after will remove it and the second will fail.
Using latest version, 4.5
Unfortunately this code will still be executed, even is this panel is not configured. I tried to fix this issue without any luck so far. Maybe one way to start is to isolate this code so it can actually be switched off.