Examples

Basic Usage

examples/simple.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
from jitcache import Cache
import time


cache = Cache()


@cache.memoize
def slow_fn(input_1, input_2, input_3=10):
    print("Slow Function Called")
    time.sleep(1)
    return input_1 * input_2 * input_3


print(slow_fn(10, 2))

Output:

Slow Function Called
40

Usage with Threads

examples/threads.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from jitcache import Cache
import time
from concurrent.futures import ThreadPoolExecutor

cache = Cache()


@cache.memoize
def slow_fn(input_1, input_2):
    print("Slow Function Called")
    time.sleep(1)
    return input_1 * input_2


n_threads = 10

executor = ThreadPoolExecutor(max_workers=n_threads)

future_list = []
result_list = []

n_requests = 5

# Set up n requests for an object
for i in range(n_requests):
    f = executor.submit(slow_fn, 10, 4)
    future_list.append(f)

# Collect the results
for f in future_list:
    result_list.append(f.result())

print(result_list)

Output:

Slow Function Called
Slow Function Called
[40, 40, 40, 40, 40, 120, 120, 120, 120, 120]

Usage with Processes

examples/process.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from jitcache import Cache
import time
import multiprocessing as mp

cache = Cache()


@cache.memoize
def slow_fn(input_1, input_2):
    print("Slow Function Called")
    time.sleep(1)
    return input_1 * input_2


n_processes = 10

process_list = []

# Create a set of processes who will request the same value
for i in range(n_processes):
    p = mp.Process(target=slow_fn, args=(10, 4))
    process_list.append(p)

# Start each process
for p in process_list:
    p.start()

# Wait for completion
for p in process_list:
    p.join()

# Print the value that they tried to compute
print(slow_fn(10, 4))

Output:

Slow Function Called
40