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

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.4.tar.gz (4.2 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.4-py3-none-any.whl (3.7 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for pyfilecache-0.1.4.tar.gz
Algorithm Hash digest
SHA256 01723e5ceae5413220c64d8a7f4eefe7c40168e7e5c0bfb395847d601fa29101
MD5 89565a19a13a1b95929f5124f85107f6
BLAKE2b-256 4c4e11550e9fada8aee60d7e7c577d1834acc232949f8e495a67803cc4cba5e5

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pyfilecache-0.1.4-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.8.10

File hashes

Hashes for pyfilecache-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 588e18f456cd17c2df8f6fdbde9a531a0c23f48e41d6ffaa2be9c5008bfac5b6
MD5 9d3b4ad3796da4ef3421cf31df6acad1
BLAKE2b-256 c480f6648893eaf0ad317d72898b1220de71a8e2680ba2a204f0b8b7b1efcde2

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