Posts

Sqlite3 in Python

Never share the connections b/w threads instead create new connections, always use driver arg {'check_same_thread':False,'timeout': 2000} to avoid errors related to connections

Python self.cache using sqlite3 db

Make caching part of your class, easiest way is to use a sqlite3 db to store cache. Make member variables (which you don't want to cache like the db connection, etc) private (names starts with double underscores like self.__iam_private_variable) And while caching the object (self) make sure you ignore all private variables Below is a simple class with self caching import os,sys from timeit import default_timer as timer import concurrent.futures import threading from threading import Lock from pydal import DAL, Field import pickle class myclass: def __setupCache(self): self.__db = DAL('sqlite://object_cache_for_myclass.sqlite',driver_args={'check_same_thread':False,'timeout': 2000}) self.__db.define_table('obj_cache',Field('cache_id',type='double',required=True,unique=True), Field('obj',type='blob')) self.__insert = False def __setupAttribsFromCache(self,obj