-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcore.py
More file actions
182 lines (122 loc) · 4.2 KB
/
Copy pathcore.py
File metadata and controls
182 lines (122 loc) · 4.2 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import os
import ctypes
# For a tutorial on ctypes, see here:
# https://docs.python.org/3/library/ctypes.html
#
# Faasm host interface:
# https://github.com/faasm/cpp/blob/main/libfaasm/faasm/host_interface.h
env_cache = dict()
input_data = None
output_data = None
_host_interface = None
def get_env_bool(var_name):
if var_name not in env_cache:
value = os.environ.get(var_name)
env_cache[var_name] = bool(value)
return env_cache[var_name]
def set_env_bool(var_name, value):
env_cache[var_name] = value
def is_local_chaining():
return get_env_bool("PYTHON_LOCAL_CHAINING")
def is_local_output():
return get_env_bool("PYTHON_LOCAL_OUTPUT")
def set_local_chaining(value):
set_env_bool("PYTHON_LOCAL_CHAINING", value)
def set_local_input_output(value):
set_env_bool("PYTHON_LOCAL_OUTPUT", value)
def _init_host_interface():
global _host_interface
_host_interface = ctypes.CDLL(None)
def get_input_len():
_init_host_interface()
return _host_interface.__faasm_read_input(None, 0)
def read_input(input_len):
_init_host_interface()
input_len = int(input_len)
buff = ctypes.create_string_buffer(input_len)
_host_interface.__faasm_read_input(buff, input_len)
return buff.value
def write_output(output):
if is_local_output():
global output_data
output_data = output
else:
_init_host_interface()
_host_interface.__faasm_write_output(output, len(output))
def get_output():
if is_local_output():
return output_data
else:
raise RuntimeError(
"Should not be getting output in non-local input/ output"
)
def read_state_size(key):
_init_host_interface()
return _host_interface.__faasm_read_state(bytes(key, "utf-8"), None, 0)
def read_state(key, state_len):
_init_host_interface()
state_len = int(state_len)
buff = ctypes.create_string_buffer(state_len)
_host_interface.__faasm_read_state(bytes(key, "utf-8"), buff, state_len)
return bytes(buff)
def read_state_offset(key, total_len, offset, offset_len):
_init_host_interface()
total_len = int(total_len)
offset_len = int(offset_len)
buff = ctypes.create_string_buffer(offset_len)
_host_interface.__faasm_read_state_offset(
bytes(key, "utf-8"), total_len, offset, buff, offset_len
)
return bytes(buff)
def write_state(key, value):
_init_host_interface()
_host_interface.__faasm_write_state(bytes(key, "utf-8"), value, len(value))
def write_state_offset(key, total_len, offset, value):
_init_host_interface()
offset = int(offset)
total_len = int(total_len)
_host_interface.__faasm_write_state_offset(
bytes(key, "utf-8"), total_len, offset, value, len(value)
)
def push_state(key):
_init_host_interface()
_host_interface.__faasm_push_state(bytes(key, "utf-8"))
def push_state_partial(key):
_init_host_interface()
_host_interface.__faasm_push_state_partial(bytes(key, "utf-8"))
def pull_state(key, state_len):
_init_host_interface()
state_len = int(state_len)
_host_interface.__faasm_pull_state(bytes(key, "utf-8"), state_len)
def chain(func, input_data):
if is_local_chaining():
# Run function directly
func(input_data)
return 0
else:
# Call into host interface
_init_host_interface()
func_name = func.__name__
return _host_interface.__faasm_chain_py(
bytes(func_name, "utf-8"), input_data, len(input_data)
)
def await_call(call_id):
if is_local_chaining():
# Calls are run immediately in local chaining
return 0
else:
_init_host_interface()
return _host_interface.__faasm_await_call(call_id)
def set_emulator_message(message_json):
_init_host_interface()
message_bytes = bytes(message_json, "utf-8")
if is_local_output():
global output_data
output_data = None
return _host_interface.setEmulatedMessageFromJson(message_bytes)
def set_emulator_status(success):
_init_host_interface()
_host_interface.__set_emulator_status(success)
def get_emulator_async_response():
_init_host_interface()
return _host_interface.__get_emulator_async_response()