-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmath.py
76 lines (54 loc) · 1.8 KB
/
math.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
75
76
import asyncio
from dataclasses import dataclass
from typing import Iterator, Tuple
import jockey
# each math operation accepts 2 integer numbers
Payload = Tuple[int, int]
# each math operation is identified by a symbol (like "+" or "/")
Key = str
# each math operation returns a float
Result = float
@dataclass
class Message(jockey.Adapter[Payload, Key, Result]):
left: int
op: Key
right: int
def get_keys(self) -> Iterator[Key]:
yield self.op
async def get_payload(self) -> Payload:
return (self.left, self.right)
async def on_success(self, result: Result) -> None:
print(f'SUCCESS: {self.left} {self.op} {self.right} = {result}')
async def on_failure(self, exc: Exception) -> None:
print(f'FAILURE: {self.left} {self.op} {self.right} caused {exc!r}')
async def on_cancel(self, exc: asyncio.CancelledError) -> None:
print(f'CANCELED: {self.left} {self.op} {self.right}')
class Registry(jockey.Registry[Payload, Key, Result]):
pass
registry = Registry()
@registry.add('+')
def _add(payload: Payload) -> Result:
left, right = payload
return left + right
@registry.add('/', execute_in=jockey.ExecuteIn.PROCESS)
def _div(payload: Payload) -> Result:
left, right = payload
return left / right
@registry.add('-')
async def _sub(payload: Payload) -> Result:
left, right = payload
await asyncio.sleep(1)
return left / right
async def main() -> None:
async with jockey.Executor(registry).run() as executor:
messages = [
Message(3, '-', 2),
Message(4, '+', 5),
Message(3, '/', 2),
Message(3, '/', 0),
Message(3, '+', 0),
]
for msg in messages:
await executor.execute(msg)
if __name__ == '__main__':
asyncio.run(main())