Skip to content

Commit

Permalink
Add Test Thrift Service
Browse files Browse the repository at this point in the history
Summary: Temp service for training exercise

Differential Revision: D63664138

fbshipit-source-id: 1cb88f24f466db91282aeaf775a48391864cdbc8
  • Loading branch information
Aditya Venkataraman authored and facebook-github-bot committed Oct 1, 2024
1 parent e322850 commit eb4f93e
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
49 changes: 49 additions & 0 deletions thrift/tco-sample-thrift/python/client/echo_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# 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.

import asyncio

import click
from example.chatroom.thrift_clients import Echo
from thrift.python.client import ClientType, get_client


async def async_main(host, port, http):
async with get_client(
Echo,
host=host,
port=port,
path=("/" if http else None),
client_type=(
ClientType.THRIFT_HTTP2_CLIENT_TYPE
if http
else ClientType.THRIFT_ROCKET_CLIENT_TYPE
),
) as client:
while True:
text = input("> ")
resp = await client.echo(text)
print(f"< {resp}")


@click.command()
@click.option("--host", default="localhost", help="Hostname of remote thrift server")
@click.option("--port", default=7778, help="Listening port of remote thrift server")
@click.option("--http/--no-http", default=False, help="Use HTTP2 client")
def main(*args, **kwargs):
asyncio.run(async_main(*args, **kwargs))


if __name__ == "__main__":
main()
53 changes: 53 additions & 0 deletions thrift/tco-sample-thrift/python/server/echo_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# 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.

import asyncio
from signal import SIGINT, SIGTERM

import click
from example.chatroom.thrift_services import EchoInterface
from thrift.python.server import ThriftServer


class EchoHandler(EchoInterface):
async def echo(self, message: str):
# modified_message = message.upper()
# return modified_message
name, age = message.split(",")
modified_message = "My name is {} and I am {}, but my mental age is {}".format(
name, age, int(age) / 5
)
return modified_message


async def async_main(port):
loop = asyncio.get_event_loop()
server = ThriftServer(EchoHandler(), port=port)
serve_task = loop.create_task(server.serve())
for signal in [SIGINT, SIGTERM]:
loop.add_signal_handler(signal, server.stop)
addr = await server.get_address()
print(f"Listening on {addr}")
await serve_task


@click.command()
@click.option("--port", default=7778, help="Listening port of remote thrift server")
def main(port):
# TODO: due to some bug, using asyncio.run() will cause process to stuck on exit, to be fixed
asyncio.get_event_loop().run_until_complete(async_main(port))


if __name__ == "__main__":
main()

0 comments on commit eb4f93e

Please sign in to comment.