tezvyn:

How do you safely execute blocking code from an async function

Source: docs.python.orgintermediate

Tests whether you know how to prevent event loop blocking by offloading sync work. Name asyncio.to_thread or run_in_executor, explain ThreadPoolExecutor scheduling, and note when processes beat threads.

Tests your understanding of event loop blocking and the specific asyncio API used to offload synchronous work. A great answer names asyncio.to_thread or loop.run_in_executor, explains that the function is scheduled in a ThreadPoolExecutor so the event loop keeps running, and distinguishes CPU-bound work that may need a ProcessPoolExecutor. Red flag: suggesting you can safely call the blocking function directly, wrap it in create_task, or await a non-coroutine without using a thread pool.

Read the original → docs.python.org

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.

How do you safely execute blocking code from an async function · Tezvyn