Skip to content

Instantly share code, notes, and snippets.

@amjith
Created November 23, 2013 00:14

Revisions

  1. amjith created this gist Nov 23, 2013.
    34 changes: 34 additions & 0 deletions bg_task.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    import newrelic.agent

    newrelic.agent.initialize('staging.ini')
    newrelic.agent.register_application(timeout=10.0)

    def add(a, b):
    return a + b

    def sub(a, b):
    return a - b

    def mul(a, b):
    return a * b

    def div(a, b):
    return a / b


    class TaskRunner(object):
    def __init__(self):
    self.application = newrelic.agent.application()
    self.task_list = {'add': add, 'sub': sub, 'mul': mul, 'div': div}

    def run_task(self, name, args):
    task = self.task_list[name]
    name = newrelic.agent.callable_name(task)
    with newrelic.agent.BackgroundTask(self.application, name=name, group='Task'):
    return task(*args)

    t = TaskRunner()
    print t.run_task('add', (1, 2))
    print t.run_task('sub', (1, 2))
    print t.run_task('mul', (1, 2))
    print t.run_task('div', (1, 2))