|
| 1 | +.. currentmodule:: asyncio |
| 2 | + |
| 3 | +Event loops |
| 4 | +=========== |
| 5 | + |
| 6 | +Event loop functions |
| 7 | +-------------------- |
| 8 | + |
| 9 | +The following functions are convenient shortcuts to accessing the methods of the |
| 10 | +global policy. Note that this provides access to the default policy, unless an |
| 11 | +alternative policy was set by calling :func:`set_event_loop_policy` earlier in |
| 12 | +the execution of the process. |
| 13 | + |
| 14 | +.. function:: get_event_loop() |
| 15 | + |
| 16 | + Equivalent to calling ``get_event_loop_policy().get_event_loop()``. |
| 17 | + |
| 18 | +.. function:: set_event_loop(loop) |
| 19 | + |
| 20 | + Equivalent to calling ``get_event_loop_policy().set_event_loop(loop)``. |
| 21 | + |
| 22 | +.. function:: new_event_loop() |
| 23 | + |
| 24 | + Equivalent to calling ``get_event_loop_policy().new_event_loop()``. |
| 25 | + |
| 26 | + |
| 27 | +Available event loops |
| 28 | +--------------------- |
| 29 | + |
| 30 | +asyncio currently provides two implementations of event loops: |
| 31 | +:class:`SelectorEventLoop` and :class:`ProactorEventLoop`. |
| 32 | + |
| 33 | +.. class:: SelectorEventLoop |
| 34 | + |
| 35 | + Event loop based on the :mod:`selectors` module. Subclass of |
| 36 | + :class:`BaseEventLoop`. |
| 37 | + |
| 38 | + Use the most efficient selector available on the platform. |
| 39 | + |
| 40 | +.. class:: ProactorEventLoop |
| 41 | + |
| 42 | + Proactor event loop for Windows using "I/O Completion Ports" aka IOCP. |
| 43 | + Subclass of :class:`BaseEventLoop`. |
| 44 | + |
| 45 | + Availability: Windows. |
| 46 | + |
| 47 | + .. seealso:: |
| 48 | + |
| 49 | + `MSDN documentation on I/O Completion Ports |
| 50 | + <http://msdn.microsoft.com/en-us/library/windows/desktop/aa365198%28v=vs.85%29.aspx>`_. |
| 51 | + |
| 52 | +Example to use a :class:`ProactorEventLoop` on Windows:: |
| 53 | + |
| 54 | + import asyncio, os |
| 55 | + |
| 56 | + if os.name == 'nt': |
| 57 | + loop = asyncio.ProactorEventLoop() |
| 58 | + asyncio.set_event_loop(loop) |
| 59 | + |
| 60 | + |
| 61 | +Platform support |
| 62 | +---------------- |
| 63 | + |
| 64 | +The :mod:`asyncio` module has been designed to be portable, but each platform |
| 65 | +still has subtle differences and may not support all :mod:`asyncio` features. |
| 66 | + |
| 67 | +Windows |
| 68 | +^^^^^^^ |
| 69 | + |
| 70 | +Common limits of Windows event loops: |
| 71 | + |
| 72 | +- :meth:`~BaseEventLoop.create_unix_server` and |
| 73 | + :meth:`~BaseEventLoop.create_unix_server` are not supported: specific to UNIX |
| 74 | +- :meth:`~BaseEventLoop.add_signal_handler` and |
| 75 | + :meth:`~BaseEventLoop.remove_signal_handler` are not supported |
| 76 | +- Pipes are not supported: :meth:`~BaseEventLoop.connect_read_pipe` and |
| 77 | + :meth:`~BaseEventLoop.connect_write_pipe` |
| 78 | +- :meth:`EventLoopPolicy.set_child_watcher` is not supported |
| 79 | + |
| 80 | +:class:`SelectorEventLoop` specific limits: |
| 81 | + |
| 82 | +- :class:`~selectors.SelectSelector` is used but it only supports sockets, |
| 83 | + see the `MSDN documentation of select |
| 84 | + <http://msdn.microsoft.com/en-us/library/windows/desktop/ms740141%28v=vs.85%29.aspx>`_. |
| 85 | +- it is not possible to execute subprocesses |
| 86 | +- :meth:`~BaseEventLoop.add_reader` and :meth:`~BaseEventLoop.add_writer` only |
| 87 | + accept file descriptors of sockets |
| 88 | + |
| 89 | +:class:`ProactorEventLoop` specific limits: |
| 90 | + |
| 91 | +- SSL is not supported: :meth:`~BaseEventLoop.create_connection` and |
| 92 | + :meth:`~BaseEventLoop.create_server` cannot be used with SSL for example |
| 93 | +- :meth:`~BaseEventLoop.create_datagram_endpoint` (UDP) is not supported |
| 94 | +- :meth:`~BaseEventLoop.add_reader` and :meth:`~BaseEventLoop.add_writer` are |
| 95 | + not supported |
| 96 | + |
| 97 | +The resolution of the monotonic clock on Windows is usually around 15.6 msec. |
| 98 | +The best resolution is 0.5 msec. The exact resolution depends on the hardware |
| 99 | +(availability of HPET) and the Windows configuration. See :ref:`asyncio delayed |
| 100 | +calls <asyncio-delayed-calls>`. |
| 101 | + |
| 102 | + |
| 103 | +Mac OS X |
| 104 | +^^^^^^^^ |
| 105 | + |
| 106 | +Character devices like PTY are only well supported since Mavericks (Mac OS |
| 107 | +10.9). They are not supported at all on Mac OS 10.5 and older. |
| 108 | + |
| 109 | +On Mac OS 10.6, 10.7 and 10.8, the default event loop is |
| 110 | +:class:`SelectorEventLoop` which uses :class:`selectors.KqueueSelector`. |
| 111 | +:class:`selectors.KqueueSelector` does not support character devices on these |
| 112 | +versions. The :class:`SelectorEventLoop` can be used with |
| 113 | +:class:`~selectors.SelectSelector` or :class:`~selectors.PollSelector` to |
| 114 | +support character devices on these versions of Mac OS X. Example:: |
| 115 | + |
| 116 | + import asyncio |
| 117 | + import selectors |
| 118 | + |
| 119 | + selector = selectors.SelectSelector() |
| 120 | + loop = asyncio.SelectorEventLoop(selector) |
| 121 | + asyncio.set_event_loop(loop) |
| 122 | + |
| 123 | + |
| 124 | +Event loop policies and the default policy |
| 125 | +------------------------------------------ |
| 126 | + |
| 127 | +Event loop management is abstracted with a *policy* pattern, to provide maximal |
| 128 | +flexibility for custom platforms and frameworks. Throughout the execution of a |
| 129 | +process, a single global policy object manages the event loops available to the |
| 130 | +process based on the calling context. A policy is an object implementing the |
| 131 | +:class:`AbstractEventLoopPolicy` interface. |
| 132 | + |
| 133 | +For most users of :mod:`asyncio`, policies never have to be dealt with |
| 134 | +explicitly, since the default global policy is sufficient. |
| 135 | + |
| 136 | +The default policy defines context as the current thread, and manages an event |
| 137 | +loop per thread that interacts with :mod:`asyncio`. The module-level functions |
| 138 | +:func:`get_event_loop` and :func:`set_event_loop` provide convenient access to |
| 139 | +event loops managed by the default policy. |
| 140 | + |
| 141 | +Event loop policy interface |
| 142 | +--------------------------- |
| 143 | + |
| 144 | +An event loop policy must implement the following interface: |
| 145 | + |
| 146 | +.. class:: AbstractEventLoopPolicy |
| 147 | + |
| 148 | + .. method:: get_event_loop() |
| 149 | + |
| 150 | + Get the event loop for the current context. Returns an event loop object |
| 151 | + implementing the :class:`BaseEventLoop` interface, or raises an exception in case |
| 152 | + no event loop has been set for the current context and the current policy |
| 153 | + does not specify to create one. It should never return ``None``. |
| 154 | + |
| 155 | + .. method:: set_event_loop(loop) |
| 156 | + |
| 157 | + Set the event loop for the current context to *loop*. |
| 158 | + |
| 159 | + .. method:: new_event_loop() |
| 160 | + |
| 161 | + Create and return a new event loop object according to this policy's rules. |
| 162 | + If there's need to set this loop as the event loop for the current context, |
| 163 | + :meth:`set_event_loop` must be called explicitly. |
| 164 | + |
| 165 | +Access to the global loop policy |
| 166 | +-------------------------------- |
| 167 | + |
| 168 | +.. function:: get_event_loop_policy() |
| 169 | + |
| 170 | + Get the current event loop policy. |
| 171 | + |
| 172 | +.. function:: set_event_loop_policy(policy) |
| 173 | + |
| 174 | + Set the current event loop policy. If *policy* is ``None``, the default |
| 175 | + policy is restored. |
| 176 | + |
0 commit comments