I have a simple numba
example where I
- Define a numba function
- Enable caching via
enable_caching()
(instead ofcache=True
) - Call the function
- Print the contents of the cache directory
rom numba import njit
from os import listdir
import pathlib
PYCACHEDIR = '__pycache__'
@njit
def add(a, b):
return a + b
if __name__ == "__main__":
add.enable_caching()
add(4, 5)
print(listdir(PYCACHEDIR))
As expected, this prints out:
['test.add-9.py312.nbi', 'test.add-9.py312.1.nbc']
However, if I:
- Manually clear/delete the cache
- Call the
add
function again - Print the contents of the cache directory
[f.unlink() for f in pathlib.Path(PYCACHEDIR).glob("*nb*") if f.is_file()]
print(listdir(PYCACHEDIR))
add(4, 5)
print(listdir(PYCACHEDIR))
Then nothing gets printed as the cache directory is empty! Since I had originally called add.enable_cache()
, I would’ve expected the function to be cached again (after having manually cleared/deleted the cache) once I call the function again. Instead, it almost appears as if the writing to the PYCACHEDIR
is disabled and, perhaps, the add
function is either:
- Not cached at all (which would be undesirable)
- or cached elsewhere (but where?!)
Can anybody tell me what is happening here?
The full script is as follows:
from numba import njit
from os import listdir
import pathlib
PYCACHEDIR = '__pycache__'
@njit
def add(a, b):
return a + b
if __name__ == "__main__":
add.enable_caching()
add(4, 5)
print(listdir(PYCACHEDIR))
[f.unlink() for f in pathlib.Path(PYCACHEDIR).glob("*nb*") if f.is_file()]
print(listdir(PYCACHEDIR))
add(4, 5)
print(listdir(PYCACHEDIR))