Sometimes the best way to perform complex behavior in a callback is to put that behavior into an external script which can take the job ID as an argument.
The following is an example of a callback that runs for every frame when the job completes. Admittedly, it may be more efficient to run it once and have the external script iterate over every frame, but this is for demonstration purposes.
Job submission code
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 | #!/bin/env python3 import sys,os sys.path.append( '%s/api/python' % os.environ[ 'QBDIR' ]) import qb scriptPathOnSupervisor = '/Users/jburk/test/testCbScript.py' ######################################## # build the job ######################################## job = { 'prototype' : 'cmdrange' , 'name' : 'supervisor-side script execution callback test' , 'package' : { 'cmdline' : 'hostname' }, 'agenda' : [], 'callbacks' : [ ] } ######################################## # iterate over the frame range to build the job's agenda and callbacks ######################################## for i in range ( 5 ): # an agenda item can simply be a frame number work = { 'name' : i} job[ 'agenda' ].append(work) # build a callback for each item in the agenda (each frame) cb = { 'language' : 'python' , 'triggers' : 'done-job-self' , 'code' : ''' import os,subprocess jobId = qb.jobid() workName = %s script = '%s' if os.path.exists(script): pid = subprocess.Popen(["python", script, str(jobId), str(workName)]).pid ''' % (i, scriptPathOnSupervisor) } # append the frame's callback to the job's callback list job[ 'callbacks' ].append(cb) ######################################## # submit the job ######################################## submitted = qb.submit(job) for job in submitted: print ( 'submitted %(id)s: %(name)s' % job) |
testCbScript.py - external script run by the callback
#!/usr/bin/env python3 import sys import os # # sys.path.append('yourPathHere') # import myModule jobid = sys.argv[ 1 ] workName = sys.argv[ 2 ] workId = '%s_%s' % (jobid, workName) fh = open ( '/tmp/cb_output.%s' % workId, 'w' ) fh.write( 'This is jobid:%s, frame number:%s\n' % (jobid, workName)) fh.close() sys.exit() |