Versions Compared

    Key

    • This line was added.
    • This line was removed.
    • Formatting was changed.

    ...

    Warning
    titleNever use sys.exit() in a callback

    Never call sys.exit() at the end of the callback code, this kills the calling supervisor process.

    Code Block
    titleJob submission code
    linenumberstrue
    languagepythonpy
    #!/bin/env python
    
    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
    
    
    Tip
    titlePython path

    In line 41 of the above script, using "python" assumes that python is in the supervisor user's PATH environment. If not, use the full path to python. If the python script can be invoked on its own (without calling it as an argument to the python executable itself), then you can leave out "python" all together.

    Code Block
    titletestCbScript.py - external script run by the callback
    languagepy
    #!/usr/bin/python
    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()
    Note
    titleExecution

    Remember, callbacks are executed on the supervisor by the user who is running the supervisor (typically the local system account or "root"). Keep this in mind when reading from or writing to the shared file system.