18.5. asyncio
– Asynchronous I/O, event loop, coroutines and tasks¶
Note
The asyncio package has been included in the standard library on a provisional basis. Backwards incompatible changes (up to and including removal of the module) may occur if deemed necessary by the core developers.
Nouveau dans la version 3.4.
Code source : Lib/asyncio/
This module provides infrastructure for writing single-threaded concurrent code using coroutines, multiplexing I/O access over sockets and other resources, running network clients and servers, and other related primitives. Here is a more detailed list of the package contents:
- a pluggable event loop with various system-specific implementations;
- transport and protocol abstractions (similar to those in Twisted);
- concrete support for TCP, UDP, SSL, subprocess pipes, delayed calls, and others (some may be system-dependent);
- a
Future
class that mimics the one in theconcurrent.futures
module, but adapted for use with the event loop; - coroutines and tasks based on
yield from
(PEP 380), to help write concurrent code in a sequential fashion; - cancellation support for
Future
s and coroutines; - synchronization primitives for use between coroutines in
a single thread, mimicking those in the
threading
module; - an interface for passing work off to a threadpool, for times when you absolutely, positively have to use a library that makes blocking I/O calls.
Asynchronous programming is more complex than classical « sequential » programming: see the Develop with asyncio page which lists common traps and explains how to avoid them. Enable the debug mode during development to detect common issues.
Table des matières :
- 18.5.1. Base Event Loop
- 18.5.1.1. Exécuter une boucle d’évènements
- 18.5.1.2. Appels
- 18.5.1.3. Appels différés
- 18.5.1.4. Coroutines
- 18.5.1.5. Créer des connections
- 18.5.1.6. Attendre des connections
- 18.5.1.7. Surveiller des descripteurs de fichiers
- 18.5.1.8. Opérations bas niveau sur les socket
- 18.5.1.9. Résout le nom d’hôte
- 18.5.1.10. Connect pipes
- 18.5.1.11. Signaux UNIX
- 18.5.1.12. Exécuteur
- 18.5.1.13. API de gestion d’erreur
- 18.5.1.14. Mode débug
- 18.5.1.15. Serveur
- 18.5.1.16. Handle
- 18.5.1.17. Exemples de boucles d’évènements
- 18.5.2. Boucles d’évènements
- 18.5.3. Tâches et coroutines
- 18.5.4. Transports and protocols (low-level API)
- 18.5.5. Streams (high-level API)
- 18.5.6. Subprocess
- 18.5.7. Processus fils et threads
- 18.5.8. Subprocess examples
- 18.5.9. Synchronization primitives
- 18.5.10. Queues
- 18.5.11. Programmer avec asyncio
- 18.5.11.1. Mode de débug d’asyncio
- 18.5.11.2. Annulation
- 18.5.11.3. Concourrance et multithreading
- 18.5.11.4. Gérer les fonctions bloquantes correctement
- 18.5.11.5. Journalisation
- 18.5.11.6. Détecte les coroutines qui ne sont jamais exécutées
- 18.5.11.7. Detect exceptions never consumed
- 18.5.11.8. Chaîner les coroutines correctement
- 18.5.11.9. Pending task destroyed
- 18.5.11.10. Close transports and event loops