Skip to content

Instantly share code, notes, and snippets.

@mjallday
Forked from amjith/bg_task.py
Created December 4, 2013 19:00
Show Gist options
  • Save mjallday/7793408 to your computer and use it in GitHub Desktop.
Save mjallday/7793408 to your computer and use it in GitHub Desktop.
import newrelic.agent
import time
import logging
logging.basicConfig()
logger = logging.getLogger('newrelic')
logger.setLevel(logging.DEBUG)
newrelic.agent.initialize('newrelic.ini', environment='staging')
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)
def main():
t = TaskRunner()
while True:
time.sleep(10)
print t.run_task('add', (1, 2))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment