@@ -49,6 +49,17 @@ def func_copy_input_to_output(input_filename, output_filename):
4949 f .write (contents )
5050 return 0
5151
52+ def func_load_partial_data_from_file (filename ):
53+ with open (filename , 'r' ) as f :
54+ contents = f .read ()
55+ contents = contents .strip ()
56+ partial_value = sum ([int (x ) for x in contents .split (' ' )])
57+ return {'partial_value' : partial_value }
58+
59+ def compute_final_value (value ):
60+ partial_value = load_variable_from_library ('partial_value' )
61+ return partial_value + value
62+
5263def main ():
5364 parser = argparse .ArgumentParser ("Test for taskvine python bindings." )
5465 parser .add_argument ("port_file" , help = "File to write the port the queue is using." )
@@ -88,6 +99,14 @@ def main():
8899 # this is to test the input/output staging of function calls
89100 libtask_with_io_fn = q .create_library_from_functions ('test-library-with-io-fn' , func_copy_input_to_output , add_env = False , exec_mode = 'fork' )
90101
102+ # define a function that loads partial data from a file in the library.
103+ # this is to test the context loading of the library.
104+ test_filename_for_libtask_with_context_from_file = os .path .basename (__file__ ) + '.context.libtask.input'
105+ with open (test_filename_for_libtask_with_context_from_file , 'w' ) as f :
106+ print ('1 2' , file = f )
107+ libtask_with_context_from_file = q .create_library_from_functions ('test-library-with-context-from-file' , compute_final_value , add_env = False , exec_mode = 'fork' , library_context_info = [func_load_partial_data_from_file , [test_filename_for_libtask_with_context_from_file ], {}])
108+ taskvine_file_for_libtask_with_context_from_file = q .declare_file (test_filename_for_libtask_with_context_from_file )
109+ libtask_with_context_from_file .add_input (taskvine_file_for_libtask_with_context_from_file , test_filename_for_libtask_with_context_from_file )
91110 # Just take default resources for the library, this will cause it to fill the whole worker.
92111 # And the number of functions slots will match the number of cores available.
93112
@@ -97,12 +116,14 @@ def main():
97116 q .install_library (libtask_with_context_fork )
98117 q .install_library (libtask_with_special_fns )
99118 q .install_library (libtask_with_io_fn )
119+ q .install_library (libtask_with_context_from_file )
100120 lib_task_names = ['test-library-no-context-direct' ,
101121 'test-library-no-context-fork' ,
102122 'test-library-with-context-direct' ,
103123 'test-library-with-context-fork' ,
104124 'test-library-with-special-fns' ,
105- 'test-library-with-io-fn' ]
125+ 'test-library-with-io-fn' ,
126+ 'test-library-with-context-from-file' ]
106127 print ("Submitting function call tasks..." )
107128
108129 tasks = 100
@@ -130,6 +151,12 @@ def main():
130151 s_task .add_output (output_file , output_filename )
131152 q .submit (s_task )
132153
154+ # do this test once only
155+ break
156+ elif lib_name == 'test-library-with-context-from-file' :
157+ s_task = vine .FunctionCall (lib_name , 'compute_final_value' , 3 )
158+ q .submit (s_task )
159+
133160 # do this test once only
134161 break
135162 else :
@@ -161,7 +188,8 @@ def main():
161188 with_context_direct_expected = (tasks * (divide (2 , 2 ** 2 ) + double (3 ) + cube (4 ) + base_val * 3 ))
162189 with_context_fork_expected = (tasks * (divide (2 , 2 ** 2 ) + double (3 ) + cube (4 ) + base_val * 3 ))
163190 special_fns_expected = (tasks * (lambda_fn (1 ) + dyn_fn (1 )))
164- expected = no_context_direct_expected + no_context_fork_expected + with_context_direct_expected + with_context_fork_expected + special_fns_expected
191+ with_context_from_file_expected = (1 + 2 + 3 ) # 1 and 2 from the context loaded from the library input file, and 3 from the function call
192+ expected = no_context_direct_expected + no_context_fork_expected + with_context_direct_expected + with_context_fork_expected + special_fns_expected + with_context_from_file_expected
165193
166194 print (f"Total: { total_sum } " )
167195 print (f"Expected: { expected } " )
0 commit comments