(python) How to spawn blocking CPU bound tasks

13 views
Skip to first unread message

Joshua Benz

unread,
Sep 9, 2023, 1:54:15 AMSep 9
to grpc.io
I have python gRPC server that has a handle to a gRPC client for a different python gRPC server. I have an RPC that returns a stream, i use `async for` to iterate over that stream. I was trying to follow this https://docs.python.org/3/library/asyncio-eventloop.html?highlight=run_in_executor#asyncio.loop.run_in_executor but I can't get it to work. When I do get_running_loop() and then call loop.run_in_executor with a ProcessPoolExecutor, it always causes "too_many_pings" error. Messing with settings on the client and servers doesn't seem to make a difference.

How do people not block the runtime when doing CPU bound work?

Joshua Benz

unread,
Sep 9, 2023, 2:01:15 AMSep 9
to grpc.io
async def my_rpc(self, request, context)
...
...
...
stream: AsyncIterable[Response2] = self.runner.get_stream()
        res = list()

        try:
            loop = asyncio.get_running_loop()
            with ProcessPoolExecutor() as executor:
                async for msg in stream:
                    print(msg)
                    if msg.success:
                        stuff = await loop.run_in_executor(executor, my_cpu_bound_func, msg.data)
         ....
         ...
        ...
        except Exception as e:
            print(e)


This fails with too_many_pings error and the server side closes the connection. But if i take out asyncio.get_running_loop() and consequently run_in_executor() and just do the work there like a normal function call, it works.
Reply all
Reply to author
Forward
0 new messages