Python: grpc aio server parallelism multicore

466 views
Skip to first unread message

weide zhang

unread,
Jul 4, 2023, 10:06:17 AM7/4/23
to grpc.io
Hi, 

It seems from the documentation that the grpc aio server only uses one thread to do async io.  Does that mean for a multicore system, in order to leverage the performance of all the cores and using Async IO to achieve maximum performance , I have to spawn multiple thread or process each with its own aio server? 

My question is really how to leverage all the cores on one server in the meanwhile still using AIO to achieve maximum performance. 

Thank you,

Weide 

Richard Belleville

unread,
Jul 12, 2023, 1:46:56 PM7/12/23
to grpc.io

In general, the way to achieve performance in Python is to use a single thread, not multiple threads. This is because of the global interpreter lock. When a thread is accessing Python objects, no other thread in the process may access any objects. A god lock for all of Python. As a result, threads in Python will buy concurrency but not parallelism. What's more, the cost of inter-thread synchronization results in thread-based concurrency being less performant than single-thread concurrency. As a result, the assumption for asyncio is that you use a single thread except for compatibility/interop reasons.

This may change in the future depending on the fate of PEP 703, but the core CPython development team is currently hyper-focused on single threaded performance, not multi-threaded performance. If and when the core Python development team changes their stance on this, we'll reassess, but for the moment, if you're looking to performantly run gRPC Python, you should do it with asyncio on a single thread.
Reply all
Reply to author
Forward
0 new messages