1

I have a python script that submits multiple jobs using bjobs. Below is the code snippet

 for jobs in job_list:
    i=0
    os.system("bsub -J JOB_{} jobs".format(str(i))
    i+=1

I want to print "Finished runnning" only when all the jobs have completed. How can I do the same ?

Astro
  • 11
  • 5

2 Answers2

1

you will need to get the result of each job, hence use subprocess module instead.

if you are calling each job on a separate thread, you can use a shared list to for saving the result of each job, so whenever all get done, this list will be the same size as the jobs_list, then you can print the proper message.

but if the code is just the above snippet, you can do this:

for index, job in enumerate(job_list):
    result = subprocess.call(f"bsub -J JOB_{index} jobs")
print("Finished runnning")
Deadpool
  • 66
  • 3
  • What If I want to wait for all the jobs to be completed and then continue with the remaining script? – Astro Mar 28 '21 at 08:44
  • I edited my answer, call method is blocking, so it will not continue till the current subprocess gets done. `result` will be the subprocess return code, you can use it as further checks. You'll find more detail here: [subprocess.call](https://docs.python.org/3/library/subprocess.html#subprocess.call) – Deadpool Mar 28 '21 at 17:09
  • wow thank you, that is what i wanted so far – roozbeh sharifnasab Mar 28 '21 at 21:18
0

not sure... but please have a try this !!

counter = 0
for jobs in job_list:
    counter +=1
    i=0
    os.system("bsub -J JOB_{} jobs".format(str(i))
    i+=1
    if len(job_list) <= counter:
        print("Completed")
'''
KLAS R
  • 52
  • 7