summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKali Kaneko (leap communications) <kali@leap.se>2016-03-31 20:51:47 -0400
committerKali Kaneko (leap communications) <kali@leap.se>2016-03-31 20:51:47 -0400
commit39343f414617736831237cd1d417d1ba83c8268a (patch)
tree6204f5d99cd83e168267820299bfd6680d3bcfa6
parent8e7a4c0b8bdbefdeb6db9660da97de5320899910 (diff)
working ampoule task
-rw-r--r--server3.py38
-rw-r--r--tasks.py21
2 files changed, 34 insertions, 25 deletions
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)}