2020import osparc_client
2121from httpx import BasicAuth , Client , HTTPStatusError
2222from tenacity import retry , retry_if_exception_type , stop_after_delay , wait_exponential
23+ from tqdm import tqdm
2324
2425_SCRIPT_DIR = Path (__file__ ).parent
2526_MAIN_FILE = _SCRIPT_DIR / "main.py"
3031assert _VALUES_FILE .is_file (), f"Values file not found: { _VALUES_FILE } "
3132
3233_SOLVER_KEY = "simcore/services/comp/s4l-python-runner"
33- _SOLVER_VERSION = "1.2.200 "
34+ _SOLVER_VERSION = "1.2.130 "
3435
35- # _SOLVER_KEY = "simcore/services/comp/osparc-python-runner"
36- # _SOLVER_VERSION = "1.4.1"
3736
38-
39- def main ( log_job : bool = False ):
37+ def main ( njobs : int , log_job : bool = False ):
38+ assert njobs > 0
4039
4140 url = os .environ .get ("OSPARC_API_URL" )
4241 assert url
@@ -81,7 +80,7 @@ def main(log_job: bool = False):
8180 solver_function = osparc_client .Function (
8281 osparc_client .SolverFunction (
8382 uid = None ,
84- title = "s4l-python-runner" ,
83+ title = "test- s4l-python-runner" ,
8584 description = "Run Python code using sim4life" ,
8685 input_schema = osparc_client .JSONFunctionInputSchema (),
8786 output_schema = osparc_client .JSONFunctionOutputSchema (),
@@ -102,21 +101,26 @@ def main(log_job: bool = False):
102101 function_id = registered_function .to_dict ().get ("uid" )
103102 assert function_id
104103
105- function_job = api_instance .run_function (
106- function_id , {"input_3" : values_file }
107- )
104+ # Prepare inputs for map_function
105+ inputs = njobs * [{"input_3" : values_file }]
108106
109- print (f"function_job: { function_job .to_dict ()} " )
107+ function_jobs = api_instance .map_function (
108+ function_id = function_id ,
109+ request_body = inputs ,
110+ )
110111
111- function_job_uid = function_job .to_dict ().get ("uid" )
112- assert function_job_uid
113- solver_job_id = function_job .to_dict ().get ("solver_job_id" )
114- assert solver_job_id
112+ print (f"function_jobs: { function_jobs .to_dict ()} " )
113+ function_job_ids = function_jobs .job_ids
114+ assert function_job_ids
115115
116116 if log_job :
117+ job = job_api_instance .get_function_job (function_job_ids [0 ])
118+ solver_job_id = job .actual_instance .solver_job_id
117119 print_job_logs (configuration , solver_job_id )
118120
119- for job_uid in [function_job_uid ]:
121+ for job_uid in tqdm (
122+ function_job_ids , desc = "Waiting for jobs to complete" , unit = "job"
123+ ):
120124 status = wait_until_done (job_api_instance , job_uid )
121125 job_statuses [status ] = job_statuses .get (status , 0 ) + 1
122126
@@ -132,7 +136,6 @@ def main(log_job: bool = False):
132136 plt .show (block = True )
133137
134138 finally :
135-
136139 for file in uploaded_files :
137140 try :
138141 file_client_instance .delete_file (file .id )
@@ -141,11 +144,12 @@ def main(log_job: bool = False):
141144 print (f"Failed to delete file { file .id } : { e } " )
142145
143146 for function in registered_functions :
147+ uid = function .actual_instance .uid
144148 try :
145- api_instance .delete_function (function . uid )
146- print (f"Deleted function { function . uid } " )
149+ api_instance .delete_function (uid )
150+ print (f"Deleted function { uid } " )
147151 except Exception as e :
148- print (f"Failed to delete function { function . uid } : { e } " )
152+ print (f"Failed to delete function { uid } : { e } " )
149153
150154
151155@retry (
@@ -177,7 +181,7 @@ def print_job_logs(configuration: osparc_client.Configuration, solver_job_uid: s
177181 with client .stream (
178182 "GET" ,
179183 f"/v0/solvers/{ _SOLVER_KEY } /releases/{ _SOLVER_VERSION } /jobs/{ solver_job_uid } /logstream" ,
180- timeout = 5 * 60 ,
184+ timeout = 60 ,
181185 ) as response :
182186 response .raise_for_status ()
183187 for line in response .iter_lines ():
@@ -190,5 +194,11 @@ def print_job_logs(configuration: osparc_client.Configuration, solver_job_uid: s
190194 parser .add_argument (
191195 "--log-job" , action = "store_true" , help = "Log details of a single job"
192196 )
197+ parser .add_argument (
198+ "--njobs" ,
199+ type = int ,
200+ default = 1 ,
201+ help = "Number of jobs to run (default: 1)" ,
202+ )
193203 args = parser .parse_args ()
194- main (log_job = args .log_job )
204+ main (njobs = args . njobs , log_job = args .log_job )
0 commit comments