Skip to content

Instantly share code, notes, and snippets.

@amjith
Created November 23, 2013 00:14
Show Gist options
  • Save amjith/7609058 to your computer and use it in GitHub Desktop.
Save amjith/7609058 to your computer and use it in GitHub Desktop.
Example newrelic background job context manager.
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))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment