2 ms·
I ported a large Python codebase from sync to async. There's a temptation to partially move and keep a mixed async/sync structure. That temptation is wrong th
by jnwatson 6y ago
I ported a large Python codebase from sync to async. There's a temptation to partially move and keep a mixed async/sync structure. That temptation is wrong though.
There's a worse secondary effect of mixing async and sync code by using executors (async->sync) or posting to the async loop (sync->async). The first effect: it means that your sync functions that were previously non-blocking are now blocking, so all async callers are now forced to make calls into them with executors.
Even worse, if you have chains of async -> executor -> sync -> post -> async -> executor -> sync (as you might do by mechanically converting sync code), each executor call takes up an entire worker in the pool, and the Python thread pool has a maximum number of workers. You need as many workers as the sum of the maximum number of outstanding async->sync transition stacks in all of your async tasks. The penalty for underestimating this is deadlock.