From 39343f414617736831237cd1d417d1ba83c8268a Mon Sep 17 00:00:00 2001 From: "Kali Kaneko (leap communications)" Date: Thu, 31 Mar 2016 20:51:47 -0400 Subject: working ampoule task --- server3.py | 38 +++++++++++++------------------------- tasks.py | 21 +++++++++++++++++++++ 2 files changed, 34 insertions(+), 25 deletions(-) create mode 100644 tasks.py diff --git a/server3.py b/server3.py index 8841c14..93b6d61 100644 --- a/server3.py +++ b/server3.py @@ -1,46 +1,34 @@ from klein import run, route -from twisted.internet.threads import deferToThread -from twisted.protocols import amp +from twisted.internet import defer from twisted.internet import reactor -from ampoule import child, pool +from ampoule import pool + +import tasks import sys from twisted.python import log -log.startLogging(sys.stdout) - -def fib(n): - if n <= 2: - return 1 - else: - return fib(n-1) + fib(n-2) +log.startLogging(sys.stdout) -class Fib(amp.Command): - response = [('total', amp.Integer())] - -class DelayedFib(amp.AMP): - def slowFib(self): - result = fib(25) - return result - Fib.responder(slowFib) @route('/') def home(request): - d = pp.doWork(Fib) - return d + d = pp.doWork(tasks.Fib) + d.addCallback(lambda res: str(res['fib'])) + return d pp = None + +@defer.inlineCallbacks def start_pool(): global pp - pp = pool.ProcessPool(child.AMPChild, recycleAfter=5000) - pp.min = 1 - pp.max = 5 - pp.start() - + pp = pool.ProcessPool(tasks.FibCalculator, min=1, max=1) + print 'starting pool' + yield pp.start() if __name__ == "__main__": reactor.callWhenRunning(start_pool) diff --git a/tasks.py b/tasks.py new file mode 100644 index 0000000..5079e4b --- /dev/null +++ b/tasks.py @@ -0,0 +1,21 @@ +from twisted.protocols import amp +from ampoule import child + + +def fib(n): + if n <= 2: + return 1 + else: + return fib(n-1) + fib(n-2) + + +class Fib(amp.Command): + response = [("fib", amp.Integer())] + + +class FibCalculator(child.AMPChild): + @Fib.responder + def fib(self): + print 'called responder, fib...' + n = 10 + return {"fib": fib(n)} -- cgit v1.2.3