forked from faasm/faasm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdict_state.py
52 lines (39 loc) · 1.2 KB
/
dict_state.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import pickle
from pyfaasm.core import get_state, get_state_size, set_state, chain_this_with_input, await_call
KEY_A = "dict_a"
KEY_B = "dict_b"
def get_dict_from_state(key):
dict_size = get_state_size(key)
pickled_dict = get_state(key, dict_size)
return pickle.loads(pickled_dict)
def write_dict_to_state(key, dict_in):
pickled_dict = pickle.dumps(dict_in)
set_state(key, pickled_dict)
# This function is chained
def check_pickle(input_bytes):
d = get_dict_from_state(KEY_A)
d["epsilon"] = "zeta"
write_dict_to_state(KEY_B, d)
# This is the main entrypoint
def faasm_main():
# Write initial dictionary to state
dict_a = {
"alpha": "beta",
"gamma": "delta"
}
write_dict_to_state(KEY_A, dict_a)
# Make the chained call
call_id = chain_this_with_input(check_pickle, b'')
await_call(call_id)
# Load from state again
dict_b = get_dict_from_state(KEY_B)
# Check expectation
expected = {
"alpha": "beta",
"gamma": "delta",
"epsilon": "zeta"
}
if dict_b != expected:
print("Expected {} but got {}".format(expected, dict_b))
exit(1)
print("Dictionary as expected: {}".format(dict_b))