Skip to content

Instantly share code, notes, and snippets.

@alicegoldfuss
Forked from amjith/bg_task.py
Created March 20, 2014 22:30
Show Gist options
  • Save alicegoldfuss/9675397 to your computer and use it in GitHub Desktop.
Save alicegoldfuss/9675397 to your computer and use it in GitHub Desktop.
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