Author Topic: Trouble using callbacks with python  (Read 2938 times)

dandymite

  • Jr. Member
  • **
  • Posts: 5
Trouble using callbacks with python
« on: May 17, 2013, 02:17:10 PM »
Using Qube 5.5.1 and trying to use callbacks with python but can't get it to work.

Here my simple python submit script:

Code: [Select]
import qb

myjob = qb.Job()
myjob['name'] = 'Testjob_Callback'
myjob['prototype'] = 'cmdline'
myjob['priority'] = '1'

myjob['retry'] = 3
myjob['cpus'] = '1'

myjob['hosts'] = 'mws021'
myjob['package'] = {'cmdline' : 'dir'}

myjob['callbacks'] = [{ 'triggers' : 'complete-job-self',
                        'language' : 'python',
                        'code' : str("print 'CALLBACK EXECUTED'"),
                        'maximum' : '1'    
                    }]

qb.submit(myjob)

This is what I see in the Callback tab of the submitted job:
Code: [Select]
count       : 1
code        : print 'CALLBACK EXECUTED'
language    : python
triggers    : complete-job-self
max         : 1
user        : myusername
ready       : complete-job-self 1
id          : 1

In the manual it is mentioned that:
"Print statements in callbacks are output to the *.cb file in the job

jburk

  • Administrator
  • *****
  • Posts: 493
Re: Trouble using callbacks with python
« Reply #1 on: May 17, 2013, 06:14:20 PM »
Which OS are you using?  I seem to recall that the "print" statements don't end up in the .cb files on a particular OS (perhaps Windows).

What I've done in the past is to print to another filehandle instead of stdout (and note that the file will be created on the supervisor):

Code: [Select]
job = {
    'prototype': 'cmdline',
    'name': 'python callback test',
    'package': {
        'cmdline': 'hostname',
    },
    'callbacks': [
        {
            'language': 'python',
            'triggers': 'done-job-self',
            'code': '''
try:
    import sys
    
    fh = open('c:/temp/err.txt', 'w')
    fh.close()

    fh = open('c:/temp/foo.txt', 'w')
    fh.write('Hello from job id %s\\n' % qb.jobid())
    fh.write('sys.version : %s\\n' % sys.version)
    fh.write('sys.version info : %s\\n' % '.'.join([str(x) for x in sys.version_info]))
    fh.write('sys.executable : %s\\n' % sys.executable)
    fh.close()

except Exception, e:
    fh = open('c:/temp/err.txt', 'w')
    fh.write('Error from job id %s\\n' % qb.jobid())
    fh.write('%s\\n' % e)
    fh.close()
'''
        }
    ]
}

print '%(id)s: %(name)s' % qb.submit(job)[0]

dandymite

  • Jr. Member
  • **
  • Posts: 5
Re: Trouble using callbacks with python
« Reply #2 on: May 22, 2013, 02:40:45 PM »
Thank you very much. That will help me with my problem.