vmagamedov / grpclib

Pure-Python gRPC implementation for asyncio
http://grpclib.readthedocs.io
BSD 3-Clause "New" or "Revised" License
936 stars 92 forks source link

Compatibility with hypercorn? #183

Closed ggilder closed 8 months ago

ggilder commented 8 months ago

Hi, I'm trying to run a grpclib server under hypercorn, so far unsuccessfully.

Going off the example in the readme, my server code is basically:

import asyncio

from grpclib.utils import graceful_exit
from grpclib.server import Server

# generated by protoc
from .helloworld_pb2 import HelloReply
from .helloworld_grpc import GreeterBase

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

def create_server():
    return Server([Greeter()])

async def main(*, host='127.0.0.1', port=50051):
    server = create_server()
    # Note: graceful_exit isn't supported in Windows
    with graceful_exit([server]):
        await server.start(host, port)
        print(f'Serving on {host}:{port}')
        await server.wait_closed()

I've tried running this using hypercorn server:create_server() as well as hypercorn server:main(), but these both result in errors when I make an actual gRPC call against the server using grpcurl.

For create_server, I get: TypeError: 'Server' object is not callable For main, I get: TypeError: 'coroutine' object is not callable

Looking at the ASGI spec, it seems like hypercorn expects an ASGI application, e.g., a callable that takes three positional arguments scope, receive, send.

Is there a way to make a grpclib server work with the ASGI spec?

vmagamedov commented 8 months ago

Hey, grpclib doesn't work as an ASGI application, it has its own server, it doesn't need hypercorn to work.

ggilder commented 8 months ago

Ok, thanks for confirming.