So I have a program where I preload some data into an lru cache and the run multiple processed in parallel. There are two main preload cached calls: openExcelWithCache
and returnReferenceDataWithCache
. The two functions are implemented like this:
from functools import lru_cache
@lru_cache(maxsize=32)
def openExcelWithCache(pathFile, blDataOnly):
return(openpyxl.load_workbook(file,data_only = blDataOnly)
@lru_cache(maxsize=32)
def returnReferenceDataWithCache(tableName, id):
logAtLevel("INFO", "Retrieving uncached reference for Id: " + str(id) + "(" + str(type(id)) + ") table: " + tableName + ".")
return self.interface.referencequery(tbl, id)
My parallel class is set up like this and makes calls to the above functions for pre loading:
from multiprocessing.dummy import Pool
#Pre-loading
openExcelWithCache('File1',True)
openExcelWithCache('File1',False)
openExcelWithCache('File2',True)
returnReferenceDataWithCache('Tbl1', 1)
lsFolders = ['Folder1', 'Folder2']
pool = Pool( processes = 6 )
# instantiated by call to
pool.map(worker, lsFolders)
def worker(clientFolder):
iterateThroughFiles = IterateThroughFiles()
iterateThroughFiles.runProcess(clientFolder)
The IterateThroughFiles
class is:
class IterateThroughFiles( object ):
def runProcess( self, folder ):
openExcelWithCache('File1',True)
print(openExcelWithCache.cache_info()) #outputs: CacheInfo(hits=1,misses=3,maxsize=32,currsize=3)
openExcelWithCache('File1',False)
print(openExcelWithCache.cache_info()) #outputs: CacheInfo(hits=2,misses=3,maxsize=32,currsize=3)
openExcelWithCache('File2',True)
print(openExcelWithCache.cache_info()) #outputs: CacheInfo(hits=3,misses=3,maxsize=32,currsize=3)
returnReferenceDataWithCache('Tbl1', 1)
print(retrieveReferenceDataWithCache.cache_info()) #outputs: CacheInfo(hits=0,misses=2,maxsize=32,currsize=2)
So for some reason, the openExcelWithCache
cache is working properly, but not the one for the returnReferenceDataWithCache
function. The same call is still causing a "miss" and a new key pair to be generated. I checked that the type of '1' was the same for both, so I am not sure what is going on here.
I apologize for writing this a little pseudocodey, let me know if there is anything else that needs clarification.