Skip to main content

cache the results of slow-running functions to disk, with multi-process safety

Project description

pyfilecache

cache the results of slow-running functions to disk, with multi-process safety

Install

pip install pyfilecache

Examples

Basic Usage

# examples/add.py
from pyfilecache import file_cache

@file_cache
def add(a, b):
    print(f'adding {repr(a)} and {repr(b)}')
    return a + b

print(repr(add(1, 2)))
print(repr(add(1, 2)))
print(repr(add('1', '2')))
print(repr(add('1', '2')))
$ python3 examples/add.py
adding 1 and 2
3
3
adding '1' and '2'
'12'
'12'
$ python3 examples/add.py
3
3
'12'
'12'

Customized Reader and Writer

# examples/df.py
import pandas as pd
from functools import partial
from pyfilecache import file_cache

@file_cache(
    reader=partial(pd.read_csv, index_col=0),
    writer=pd.DataFrame.to_csv
)
def func(a, b):
    print("func called with args", a, b)
    return pd.DataFrame(
        data = {
            'col1': [1, 2],
            'col2': [a, b]
        }
    )

print(func(3, 4))
print(func(3, 4))
$ python3 examples/df.py 
func called with args 3 4
   col1  col2
0     1     3
1     2     4
   col1  col2
0     1     3
1     2     4
$ python3 examples/df.py 
   col1  col2
0     1     3
1     2     4
   col1  col2
0     1     3
1     2     4

Multi-Process Safety

# examples/process.py
from pyfilecache import file_cache
import multiprocessing

@file_cache
def func(a, b):
    print(a, b)
    return a + b

def loop():
    while True:
        print(func(1, 2))
        print(func('1', '2'))

        print(func.fp(1, 2))
        func.clear()

        print(func(1, 2))
        print(func('1', '2'))
        func.clear()

        print(func(1, 2))
        print(func(3, 4))
        print(func(5, 6))

if __name__ == '__main__':
    ps = [multiprocessing.Process(target=loop) for _ in range(10)]
    list(map(multiprocessing.Process.start, ps))
    list(map(multiprocessing.Process.join, ps))

Cache Non-Pure Function with Different Versions

[!NOTE] nonlocal variables used in function (e.g. outer) and version string (e.g. "v0", "v1") must correspond

# examples/versions.py
from pyfilecache import file_cache

@file_cache
def func(x: int):
    print(f'multiply {outer} and {x}')
    return outer * x

outer = 100
print(func(2))
print(func(10))

outer = 5
print(func['v0'](2))
print(func['v0'](10))

outer = 1
print(func['v1'](2))
print(func['v1'](10))
$ python3 examples/versions.py 
multiply 100 and 2
200
multiply 100 and 10
1000
multiply 5 and 2
10
multiply 5 and 10
50
multiply 1 and 2
2
multiply 1 and 10
10
$ python3 examples/versions.py 
200
1000
10
50
2
10

Functions with Different Signatures

# examples/signatures.py
from pyfilecache import file_cache

@file_cache
def func(x = 1000):
    print('OK')
    return x

print(func())

@file_cache
def func(x = 100):
    print('OK')
    return x

print(func())

@file_cache
def func(x: int = 100) -> int:
    print('OK')
    return x

print(func())
$ python3 examples/signatures.py 
OK
1000
OK  
100 
OK 
100
$ python3 examples/signatures.py 
1000
100
100

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyfilecache-0.1.6.tar.gz (3.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyfilecache-0.1.6-py3-none-any.whl (3.7 kB view details)

Uploaded Python 3

File details

Details for the file pyfilecache-0.1.6.tar.gz.

File metadata

  • Download URL: pyfilecache-0.1.6.tar.gz
  • Upload date:
  • Size: 3.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.11.7

File hashes

Hashes for pyfilecache-0.1.6.tar.gz
Algorithm Hash digest
SHA256 db6299706cb8441e3e5cdbaa13e794dc6ee1fbb63502903d87cc5f5ecc46a647
MD5 c05a880e8083c4155f02174244ed0a80
BLAKE2b-256 a6b3ebf4d69114dd7f95c0c40c1df9881b96ed2f82d7b0525f1479f240e028b3

See more details on using hashes here.

File details

Details for the file pyfilecache-0.1.6-py3-none-any.whl.

File metadata

  • Download URL: pyfilecache-0.1.6-py3-none-any.whl
  • Upload date:
  • Size: 3.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.11.7

File hashes

Hashes for pyfilecache-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 e13f50ef24f3fc96ed314516121791369258094c9e794a9f0ad4d2f9c77e7721
MD5 ba6f0a557e3983ed14f4cf31a97c4980
BLAKE2b-256 38844bb667e6739b1ad845581f291fe1efa5a4ea492dda55c76926edf41113f3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page