What happens if you transfer the database connection to a function that will work in another thread, and close the connection in the main thread? Will the connection be closed if you use with-statement?
A thread function that uses a database connection to read and write.
def post(db, states, user_list) Work with database:
class SQLighter: def __init__(self, database=config.database_name): self.connection = sqlite3.connect(database) self.cursor = self.connection.cursor() def close(self): """ Закрываем текущее соединение с БД """ self.connection.close() def __enter__(self): return self def __exit__(self, Type, Value, Trace): self.close() I open the connection in the main process:
with SQLighter() as db: pub_thread = Thread(target=post, args=(db, states_for_thread, user_ids)) pub_thread.start() # do other work How, then, will the post function work? I use SQLite and Python3 database