<gh_stars>10-100
import asyncio
import time
import threading
from proto.ai.ai_grpc import AiServiceBase
from proto.ai.ai_pb2 import HelloRequest, HelloReply
from grpclib.server import Server
from grpclib.utils import graceful_exit
from configs.config import Config
# from broker.consumer import coroutine
# from broker.producer import sendMsg

# generated by protoc


class AiService(AiServiceBase):

    async def SayHello(self, stream):
        request: HelloRequest = await stream.recv_message()
        message = f'Hello, {request.name}!'
        await stream.send_message(HelloReply(message=message))

    async def SayHi(self, stream):
        return super().SayHi(stream)


async def main(*, host='0.0.0.0', port=Config['port']):
    # 起协程监听kafka消费
    # loop = asyncio.get_event_loop()
    # tasks = asyncio.gather(coroutine)
    # loop.run_until_complete(tasks)
    #
    server = Server([AiService()])
    with graceful_exit([server]):
        await server.start(host, port)
        print(f'grpcServer serving on {host}:{port}')
        await server.wait_closed()


if __name__ == '__main__':
    # sendMsg(value=b'88888888888888')
    asyncio.run(main())

import asyncio
import time
import threading
from proto.ai.ai_grpc import AiServiceBase
from proto.ai.ai_pb2 import HelloRequest, HelloReply
from grpclib.server import Server
from grpclib.utils import graceful_exit
from configs.config import Config
# from broker.consumer import coroutine
# from broker.producer import sendMsg

# generated by protoc


class AiService(AiServiceBase):

    async def SayHello(self, stream):
        request: HelloRequest = await stream.recv_message()
        message = f'Hello, {request.name}!'
        await stream.send_message(HelloReply(message=message))

    async def SayHi(self, stream):
        return super().SayHi(stream)


async def main(*, host='0.0.0.0', port=Config['port']):
    # 起协程监听kafka消费
    # loop = asyncio.get_event_loop()
    # tasks = asyncio.gather(coroutine)
    # loop.run_until_complete(tasks)
    #
    server = Server([AiService()])
    with graceful_exit([server]):
        await server.start(host, port)
        print(f'grpcServer serving on {host}:{port}')
        await server.wait_closed()


if __name__ == '__main__':
    # sendMsg(value=b'88888888888888')
    asyncio.run(main())
# from broker.consumer import coroutine # from broker.producer import sendMsg # generated by protoc # 起协程监听kafka消费 # loop = asyncio.get_event_loop() # tasks = asyncio.gather(coroutine) # loop.run_until_complete(tasks) # # sendMsg(value=b'88888888888888')