-
Notifications
You must be signed in to change notification settings - Fork 3
/
service.py
74 lines (55 loc) · 1.84 KB
/
service.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import uuid
import eventlet
from eventlet.event import Event
from nameko.rpc import rpc
from nameko.extensions import DependencyProvider
# a simple task
def fibonacci(n):
a, b = 1, 1
for i in xrange(n-1):
a, b = b, a+b
if n % 50 == 0:
eventlet.sleep() # won't yield voluntarily since there's no i/o
return a
class TaskProcessor(DependencyProvider):
def __init__(self):
self.tasks = {
'fibonacci': fibonacci
# add other tasks here
}
self.results = {}
def start_task(self, name, args, kwargs):
# generate unique id
task_id = uuid.uuid4().hex
# get the named task
task = self.tasks.get(name)
# execute it in a container thread and send the result to an Event
event = Event()
gt = self.container.spawn_managed_thread(lambda: task(*args, **kwargs))
gt.link(lambda res: event.send(res.wait()))
# store the Event and return the task's unique id to the caller
self.results[task_id] = event
return task_id
def get_result(self, task_id):
# get the result Event for `task_id`
result = self.results.get(task_id)
if result is None:
return "missing"
# if the Event is ready, return its value
if result.ready():
return result.wait()
return "pending"
def get_dependency(self, worker_ctx):
class TaskApi(object):
start_task = self.start_task
get_result = self.get_result
return TaskApi()
class TaskService(object):
name = "tasks"
processor = TaskProcessor()
@rpc
def start_task(self, name, *args, **kwargs):
return self.processor.start_task(name, args, kwargs)
@rpc
def get_result(self, task_id):
return self.processor.get_result(task_id)