77# Faasm host interface:
88# https://github.com/faasm/faasm/blob/master/include/faasm/host_interface.h
99
10- HOST_INTERFACE_LIB = "libemulator.so"
11-
12- SUPPORTING_LIBS = [
10+ NATIVE_SUPPORTING_LIBS = [
1311 "libpistache.so" ,
1412 "libfaabricmpi.so" ,
1513]
1614
17- HOST_INTERFACE_LIB = "libemulator.so"
18- PYTHON_LOCAL_CHAINING = bool ( os . environ . get ( "PYTHON_LOCAL_CHAINING" ))
19- PYTHON_LOCAL_OUTPUT = bool ( os . environ . get ( "PYTHON_LOCAL_OUTPUT" ) )
15+ NATIVE_INTERFACE_LIB = "libemulator.so"
16+
17+ env_cache = dict ( )
2018
2119input_data = None
2220output_data = None
2321
2422_host_interface = None
2523
2624
27- def _init_host_interface ( ):
28- global _host_interface
25+ def get_env_bool ( var_name ):
26+ global env_cache
2927
30- if _host_interface is None :
31- # Load all supporting libs as globally linkable
32- for lib in SUPPORTING_LIBS :
33- ctypes .CDLL (lib , mode = ctypes .RTLD_GLOBAL )
28+ if var_name not in env_cache :
29+ value = os .environ .get (var_name )
30+ env_cache [var_name ] = bool (value )
31+
32+ return env_cache [var_name ]
33+
34+
35+ def set_env_bool (var_name , value ):
36+ global env_cache
37+ env_cache [var_name ] = value
38+
39+
40+ def is_wasm ():
41+ return get_env_bool ("PYTHONWASM" )
42+
43+
44+ def is_local_chaining ():
45+ return get_env_bool ("PYTHON_LOCAL_CHAINING" )
3446
35- # Load main Faasm host interface lib
36- _host_interface = ctypes . CDLL ( "libemulator.so" )
37- print ( "Loaded Faasm host interface: {}" . format ( _host_interface ) )
47+
48+ def is_local_output ():
49+ return get_env_bool ( "PYTHON_LOCAL_OUTPUT" )
3850
3951
4052def set_local_chaining (value ):
41- global PYTHON_LOCAL_CHAINING
42- PYTHON_LOCAL_CHAINING = value
53+ set_env_bool ("PYTHON_LOCAL_CHAINING" , value )
4354
4455
4556def set_local_input_output (value ):
46- global PYTHON_LOCAL_OUTPUT
47- PYTHON_LOCAL_OUTPUT = value
57+ set_env_bool ("PYTHON_LOCAL_OUTPUT" , value )
58+
59+
60+ def _init_host_interface ():
61+ global _host_interface
62+
63+ if _host_interface is None :
64+ # Wasm and native environments are different
65+ if is_wasm ():
66+ # Wasm expects the main application to handle the relevant calls
67+ _host_interface = ctypes .CDLL (None )
68+ else :
69+ # Load all supporting libs as globally linkable
70+ for lib in NATIVE_SUPPORTING_LIBS :
71+ ctypes .CDLL (lib , mode = ctypes .RTLD_GLOBAL )
72+
73+ # Load main Faasm host interface lib
74+ _host_interface = ctypes .CDLL (NATIVE_INTERFACE_LIB )
75+
76+
77+ def get_input_len ():
78+ _init_host_interface ()
79+ return _host_interface .__faasm_read_input (None , 0 )
4880
4981
50- def read_input ():
82+ def read_input (input_len ):
5183 _init_host_interface ()
52- return _host_interface .__faasm_read_input ()
84+ input_len = int (input_len )
85+ buff = ctypes .create_string_buffer (input_len )
86+ return _host_interface .__faasm_read_input (buff , input_len )
5387
5488
5589def write_output (output ):
56- if PYTHON_LOCAL_OUTPUT :
90+ if is_local_output () :
5791 global output_data
5892 output_data = output
5993 else :
6094 _init_host_interface ()
61- _host_interface .__faasm_write_output (output )
95+ _host_interface .__faasm_write_output (output , len ( output ) )
6296
6397
6498def get_output ():
65- if PYTHON_LOCAL_OUTPUT :
99+ if is_local_output () :
66100 global output_data
67101 return output_data
68102 else :
@@ -129,7 +163,7 @@ def pull_state(key, state_len):
129163
130164
131165def chain (func , input_data ):
132- if PYTHON_LOCAL_CHAINING :
166+ if is_local_chaining () :
133167 # Run function directly
134168 func (input_data )
135169 return 0
@@ -144,7 +178,7 @@ def chain(func, input_data):
144178
145179
146180def await_call (call_id ):
147- if PYTHON_LOCAL_CHAINING :
181+ if is_local_chaining () :
148182 # Calls are run immediately in local chaining
149183 return 0
150184 else :
@@ -156,7 +190,7 @@ def set_emulator_message(message_json):
156190 _init_host_interface ()
157191
158192 message_bytes = bytes (message_json , "utf-8" )
159- if PYTHON_LOCAL_OUTPUT :
193+ if is_local_output () :
160194 global output_data
161195 output_data = None
162196
0 commit comments