blob: f093e9ed25b226d52ec8bd5b66ba14c875606041 [file] [log] [blame]
Guido van Rossum7a465642013-11-22 11:47:22 -08001:mod:`asyncio` -- Asynchronous I/O, event loop, coroutines and tasks
2====================================================================
3
4.. module:: asyncio
5 :synopsis: Asynchronous I/O, event loop, coroutines and tasks.
6
7.. versionadded:: 3.4
8
9
Antoine Pitroubba86822013-11-23 00:34:26 +010010This module provides infrastructure for writing single-threaded concurrent
11code using coroutines, multiplexing I/O access over sockets and other
12resources, running network clients and servers, and other related primitives.
Guido van Rossum7a465642013-11-22 11:47:22 -080013
Antoine Pitroubba86822013-11-23 00:34:26 +010014Here is a more detailed list of the package contents:
15
16* a pluggable :ref:`event loop <event-loop>` with various system-specific
17 implementations;
18
19* :ref:`transport <transport>` and :ref:`protocol <protocol>` abstractions
20 (similar to those in `Twisted <http://twistedmatrix.com/>`_);
21
22* concrete support for TCP, UDP, SSL, subprocess pipes, delayed calls, and
23 others (some may be system-dependent);
24
25* a Future class that mimicks the one in the :mod:`concurrent.futures` module,
26 but adapted for use with the event loop;
27
28* coroutines and tasks based on ``yield from`` (:PEP:`380`), to help write
29 concurrent code in a sequential fashion;
30
31* cancellation support for Futures and coroutines;
32
33* :ref:`synchronization primitives <sync>` for use between coroutines in
34 a single thread, mimicking those in the :mod:`threading` module;
35
Guido van Rossumf0f5d382013-11-22 15:45:02 -080036* an interface for passing work off to a threadpool, for times when
37 you absolutely, positively have to use a library that makes blocking
38 I/O calls.
39
Antoine Pitroubba86822013-11-23 00:34:26 +010040
41Disclaimer
42----------
Guido van Rossum7a465642013-11-22 11:47:22 -080043
44Full documentation is not yet ready; we hope to have it written
45before Python 3.4 leaves beta. Until then, the best reference is
46:PEP:`3156`. For a motivational primer on transports and protocols,
47see :PEP:`3153`.
Antoine Pitroubba86822013-11-23 00:34:26 +010048
49
50.. XXX should the asyncio documentation come in several pages, as for logging?
51
52
53.. _event-loop:
54
55Event loops
56-----------
57
58
59.. _protocol:
60
61Protocols
62---------
63
64
65.. _transport:
66
67Transports
68----------
69
70
71.. _sync:
72
73Synchronization primitives
74--------------------------
75
76
77Examples
78--------
79
80A :class:`Protocol` implementing an echo server::
81
82 class EchoServer(asyncio.Protocol):
83
84 TIMEOUT = 5.0
85
86 def timeout(self):
87 print('connection timeout, closing.')
88 self.transport.close()
89
90 def connection_made(self, transport):
91 print('connection made')
92 self.transport = transport
93
94 # start 5 seconds timeout timer
95 self.h_timeout = asyncio.get_event_loop().call_later(
96 self.TIMEOUT, self.timeout)
97
98 def data_received(self, data):
99 print('data received: ', data.decode())
100 self.transport.write(b'Re: ' + data)
101
102 # restart timeout timer
103 self.h_timeout.cancel()
104 self.h_timeout = asyncio.get_event_loop().call_later(
105 self.TIMEOUT, self.timeout)
106
107 def eof_received(self):
108 pass
109
110 def connection_lost(self, exc):
111 print('connection lost:', exc)
112 self.h_timeout.cancel()
113