Ability to use Granian with asyncio.create_task #209
Replies: 2 comments 4 replies
-
@XavierGeerinck I'm not sure what you're referring to by Probably you want to run |
Beta Was this translation helpful? Give feedback.
-
Apologies, will provide as much context as I can. I implemented an abstraction to let me pick between protocols (http or gRPC). for The first I am trying to have Granian implemented as a HTTP Server. For that, I have a class that implements the class ServerHTTP:
async def start(self):
self._server = Granian(
# target="asgi:app",
# target=app,
target="dyn",
interface="rsgi",
address=self.host,
port=int(self.port),
# workers=self.config.get("workers", 5),
threading_mode=ThreadModes.workers,
# opt=True, # Enable loop optimizations
backlog=2048,
# log_level="error",
)
self._server.started = True
self._server.serve(target_loader=granian_app_loader(self)) Now to test this, I have a test suite that uses an util and starts up the HTTP server in the background: async def create_server(protocol="grpc", host="127.0.0.1", port=1337):
server = server_make.make(
server_impl=MockServerImpl,
host=host,
port=port,
protocol=protocol
)
# Start it in the background and return the task
server_task = asyncio.create_task(server.start())
# Ensure the server started
timeout = 5
while not system_util.is_port_in_use(port):
await asyncio.sleep(0.05)
timeout -= 0.1
if timeout <= 0:
raise Exception("Server did not start within the given timeout")
return server, server_task Where For my gRPC implementation this runs fine, for FastAPI this runs fine, but for Granian this appears to fail. |
Beta Was this translation helpful? Give feedback.
-
Hi!
For my application I am trying out the Granian server. However, for testing it, I would like to spin it up in the background through
asyncio.create_task(server.start())
I currently see the server starting, but it blocking the entire event loop. Is there any known way of making it not block the event loop?
Beta Was this translation helpful? Give feedback.
All reactions