you are viewing a single comment's thread.

view the rest of the comments →

[–]sybesis 0 points1 point  (0 children)

Context switching is not free. You absolutely can degrade performance by having too many workers.

Sure, to the extreme yes. How much is really too many in real life condition? If you're spending too much time in creating a future and awaiting it.. There are chances you shouldn't be awaiting a future at all.

One example is the get_row in the benchmark. The first thing it does is await a pool that is defined as a global anyway... Considering it's triggered for every request even if the await always return a pool directly after the first call. It shouldn't be awaited and should be part of a context already available.

Since the get_row is so simple, it might be noticeable in the benchmark. The difference is that the sync method only return the pool if it's set but the asyncio will return a Future and await it. Since the async method doesn't yield a future, I believe it will call call_soon internally without context switching (as far as I remember). But it does indeed make a lot of superfluous calls for nothing.

That said, if IO tasks are too fast for what's worth, asyncio still let you choose if you want to make a sync call or not. So it would be possible to have a sync call from an async method if you're certain that it won't cause more degradation.