Created
August 20, 2018 23:02
-
-
Save warrenrentlytics/2f34372992e39ceb7fdf4c51628483ec to your computer and use it in GitHub Desktop.
gRPC file evaluation example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Copyright 2015 gRPC authors. | |
# | |
# Licensed under the Apache License, Version 2.0 (the "License"); | |
# you may not use this file except in compliance with the License. | |
# You may obtain a copy of the License at | |
# | |
# http://www.apache.org/licenses/LICENSE-2.0 | |
# | |
# Unless required by applicable law or agreed to in writing, software | |
# distributed under the License is distributed on an "AS IS" BASIS, | |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
# See the License for the specific language governing permissions and | |
# limitations under the License. | |
"""The Python implementation of the GRPC helloworld.Greeter client.""" | |
from __future__ import print_function | |
import time | |
import datetime | |
import grpc | |
import helloworld_pb2 | |
import helloworld_pb2_grpc | |
def run(): | |
# NOTE(gRPC Python Team): .close() is possible on a channel and should be | |
# used in circumstances in which the with statement does not fit the needs | |
# of the code. | |
with grpc.insecure_channel('localhost:50051') as channel: | |
stub = helloworld_pb2_grpc.GreeterStub(channel) | |
i = 1 | |
while True: | |
start = datetime.datetime.now() | |
response = stub.SayHello(helloworld_pb2.HelloRequest(name='/Users/warren/code/exec_sample.py')) | |
end = datetime.datetime.now() | |
elapsed = (end - start) | |
print(response.message, "in", elapsed.microseconds, "microseconds") | |
break | |
if __name__ == '__main__': | |
run() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Copyright 2015 gRPC authors. | |
# | |
# Licensed under the Apache License, Version 2.0 (the "License"); | |
# you may not use this file except in compliance with the License. | |
# You may obtain a copy of the License at | |
# | |
# http://www.apache.org/licenses/LICENSE-2.0 | |
# | |
# Unless required by applicable law or agreed to in writing, software | |
# distributed under the License is distributed on an "AS IS" BASIS, | |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
# See the License for the specific language governing permissions and | |
# limitations under the License. | |
"""The Python implementation of the GRPC helloworld.Greeter server.""" | |
from concurrent import futures | |
import time | |
import grpc | |
import helloworld_pb2 | |
import helloworld_pb2_grpc | |
_ONE_DAY_IN_SECONDS = 60 * 60 * 24 | |
import sys | |
import io | |
# setup the environment | |
backup = sys.stdout | |
# based on https://stackoverflow.com/questions/5136611/capture-stdout-from-a-script-in-python | |
class Greeter(helloworld_pb2_grpc.GreeterServicer): | |
def SayHello(self, request, context): | |
backup = sys.stdout | |
sys.stdout = io.StringIO() # capture output | |
exec(open(request.name).read()) | |
out = sys.stdout.getvalue() # release output | |
# #### | |
sys.stdout.close() # close the stream | |
sys.stdout = backup # restore original stdout | |
return helloworld_pb2.HelloReply(message=out) | |
def serve(): | |
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10)) | |
helloworld_pb2_grpc.add_GreeterServicer_to_server(Greeter(), server) | |
server.add_insecure_port('[::]:50051') | |
server.start() | |
try: | |
while True: | |
time.sleep(_ONE_DAY_IN_SECONDS) | |
except KeyboardInterrupt: | |
server.stop(0) | |
if __name__ == '__main__': | |
serve() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment