sharkbound / PythonTwitchBotFramework

asynchronous twitchbot framework made in pure python
MIT License
100 stars 26 forks source link

Make sql objects friendlier to multithreaded environments #18

Closed roguedarkjedi closed 4 years ago

roguedarkjedi commented 4 years ago

Really quick hack to make the SQL database access and objects more multithread friendly. For most uses and implementations, this will do nothing. However, should someone need the ability, this exposes and provides methods of manipulating database objects for the framework.

The plus side of going with this solution is that it shouldn't negatively affect current initialization sequences requiring them to be rewritten or encapsulated, and mods/commands should require minimal to no changes (depending on what's in them).

A better, safer method would be an entire encapsulation for data safety but could negatively affect overall performance. Thus, with this solution these functions are provided as a sort of "if you call these directly you know what you're doing" ideology.

Appears to work properly in tests. Alternatives could be to use scoped_sessions, so long as the engine is properly set up.

roguedarkjedi commented 4 years ago

I know there was a discussion to move the sql initialization to the bot's run loop as a first case step, this would be fine so long as modules and commands were alerted as to when the database was ready to be used. However, this could also then be problematic because if the modules/commands are mounted in the main thread, the cross thread object usage problem would appear again.