-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathexample_service.py
More file actions
194 lines (161 loc) · 6.13 KB
/
example_service.py
File metadata and controls
194 lines (161 loc) · 6.13 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
from concurrent import futures
import contextlib
import datetime
import logging
import multiprocessing
import time
import socket
import sys
import argparse
import os
import grpc
from service import registry
# Importing the generated codes from buildproto.sh
import service.service_spec.example_service_pb2_grpc as grpc_bt_grpc
from service.service_spec.example_service_pb2 import Result
logging.basicConfig(level=10, format="%(asctime)s - [%(levelname)8s]"
" - %(name)s - %(message)s")
_LOGGER = logging.getLogger("example_service")
_ONE_DAY = datetime.timedelta(days=1)
_PROCESS_COUNT = multiprocessing.cpu_count()
_THREAD_CONCURRENCY = _PROCESS_COUNT
"""
Simple arithmetic service to test the Snet Daemon (gRPC), dApp and/or Snet-CLI.
The user must provide the method (arithmetic operation) and
two numeric inputs: "a" and "b".
e.g:
With dApp: 'method': mul
'params': {"a": 12.0, "b": 77.0}
Resulting: response:
value: 924.0
Full snet-cli cmd:
$ snet client call mul '{"a":12.0, "b":77.0}'
Result:
(Transaction info)
Signing job...
Read call params from cmdline...
Calling service...
response:
value: 924.0
"""
# Create a class to be added to the gRPC server
# derived from the protobuf codes.
class CalculatorServicer(grpc_bt_grpc.CalculatorServicer):
def __init__(self):
self.pid = os.getpid()
self.a = 0
self.b = 0
self.result = 0
# Just for debugging purpose.
_LOGGER.debug("[{}] CalculatorServicer created".format(self.pid))
# The method that will be exposed to the snet-cli call command.
# request: incoming data
# context: object that provides RPC-specific information (timeout, etc).
def add(self, request, context):
# In our case, request is a Numbers() object (from .proto file)
self.a = request.a
self.b = request.b
# To respond we need to create a Result() object (from .proto file)
self.result = Result()
self.result.value = self.a + self.b
_LOGGER.debug("[{}] add({},{})={}".format(self.pid,
self.a,
self.b,
self.result.value))
return self.result
def sub(self, request, context):
self.a = request.a
self.b = request.b
self.result = Result()
self.result.value = self.a - self.b
_LOGGER.debug("[{}] sub({},{})={}".format(self.pid,
self.a,
self.b,
self.result.value))
return self.result
def mul(self, request, context):
self.a = request.a
self.b = request.b
self.result = Result()
self.result.value = self.a * self.b
_LOGGER.debug("[{}] mul({},{})={}".format(self.pid,
self.a,
self.b,
self.result.value))
return self.result
def div(self, request, context):
self.a = request.a
self.b = request.b
self.result = Result()
self.result.value = self.a / self.b
_LOGGER.debug("[{}] div({},{})={}".format(self.pid,
self.a,
self.b,
self.result.value))
return self.result
def wait_forever(server):
try:
while True:
time.sleep(_ONE_DAY.total_seconds())
except KeyboardInterrupt:
server.stop(None)
# The gRPC serve function.
#
# Params:
# max_workers: pool of threads to execute calls asynchronously
# port: gRPC server port
#
# Add all your classes to the server here.
# (from generated .py files by protobuf compiler)
def run_server(grpc_port=7777):
options = (('grpc.so_reuseport', 1),)
server = grpc.server(
futures.ThreadPoolExecutor(max_workers=_THREAD_CONCURRENCY,),
options=options)
grpc_bt_grpc.add_CalculatorServicer_to_server(CalculatorServicer(), server)
server.add_insecure_port("[::]:{}".format(grpc_port))
server.start()
wait_forever(server)
@contextlib.contextmanager
def reserve_port(grpc_port=7777):
"""Find and reserve a port for all subprocesses to use."""
sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
if sock.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT) == 0:
raise RuntimeError("Failed to set SO_REUSEPORT.")
sock.bind(('', grpc_port))
try:
yield sock.getsockname()[1]
finally:
sock.close()
def main():
""" Runs the gRPC server to communicate with the SNET Daemon. """
parser = argparse.ArgumentParser(prog=__file__)
service_name = os.path.splitext(os.path.basename(__file__))[0]
parser.add_argument("--grpc-port",
help="port to bind gRPC service to",
default=registry[service_name]['grpc'],
type=int,
required=False)
parser.add_argument("--mp",
help="number of concurrent processes",
metavar="NUMBER_OF_PROCESSES",
default=1,
type=int,
required=False)
args = parser.parse_args()
num_processes = _PROCESS_COUNT if args.mp > _PROCESS_COUNT else args.mp
with reserve_port(args.grpc_port) as port:
sys.stdout.flush()
workers = []
for _ in range(num_processes):
# NOTE: It is imperative that the worker subprocesses be forked before
# any gRPC servers start up. See
# https://github.com/grpc/grpc/issues/16001 for more details.
worker = multiprocessing.Process(target=run_server, args=(port,))
worker.start()
workers.append(worker)
for worker in workers:
worker.join()
if __name__ == "__main__":
main()