Meaning
Schedules a callback to be called as soon as possible in the event loop, after the current control yields back to the loop.
Primary Function
Schedule a callback for execution in the next iteration of the event loop.
Communicative Purpose
Expresses the intention to defer execution of a callback until the event loop is ready, yielding control back to the loop.
Pattern
loop.call_soon(callback, *args)
Core Structure
loop.call_soon(..., ...)
Função primária
Schedule a callback for execution in the next iteration of the event loop.
Propósito comunicativo
Expresses the intention to defer execution of a callback until the event loop is ready, yielding control back to the loop.
Situações de gatilho
When you need to schedule a callback to run soon without blocking the current coroutine or callback, e.g., after scheduling I/O, to wake up the loop, or to defer cleanup.
Contextos
Inside asyncio event loop callbacks, protocol/transport callbacks, or any code running within the event loop where you want to schedule another callback to run soon.
Padrão
loop.call_soon(callback, *args)
Estrutura central
loop.call_soon(..., ...)
Slots de substituição
loop: asyncio.AbstractEventLoop instance, callback: callable, args: zero or more positional arguments passed to callback
Colocados típicos
- asyncio.get_event_loop()
- loop
- callback
- functools.partial
- lambda
- *args
- call_soon_threadsafe
- call_later.
Substituições comuns
- loop.call_later(delay
- callback
- *args) for delayed scheduling
- loop.call_soon_threadsafe for thread‑safe scheduling
- loop.create_task for fire‑and‑forget coroutines.
Erros comuns
Assuming the callback runs immediately; passing a non‑callable as callback; forgetting *args and missing arguments; calling from a non‑event‑loop thread without using the thread‑safe variant.
Similar / contraste
loop.call_later schedules after a delay; loop.call_soon_threadsafe schedules from another thread; asyncio.create_task schedules a coroutine as a Task.
Interferências
Coming from Python: assuming call_soon executes immediately → it actually runs after the current yield to the event loop; confusing call_soon with call_later → use call_later for delayed scheduling; using call_soon from a non‑event‑loop thread → use call_soon_threadsafe for thread‑safe scheduling.
Família do chunk
- asyncio event loop scheduling methods
Nuance
Do not use call_soon when you need a guaranteed delay or precise timing; it introduces negligible overhead, merely queuing a callback; the exact order among already‑queued callbacks depends on their queue position and is not guaranteed if other callbacks are added concurrently.
Efeito pragmático
Signals that the callback should be deferred, allowing the current task to yield and letting the event loop process other events first.
Dica de memória
"Call soon" – think of asking the loop to call you back shortly after it finishes the current task.
Nota
The callback receives the arguments passed via *args in the order they appear; keyword arguments are not supported by call_soon.
Upgrade path
Consider loop.call_later for delayed execution, asyncio.create_task for fire‑and‑forget coroutines, or loop.call_soon_threadsafe for cross‑thread safety.
Log in to save chunks.