blob: e867c80d4ed60a155bb106aa165ae3196cf78671 [file] [log] [blame]
R David Murray6a143812013-12-20 14:37:39 -05001.. currentmodule:: asyncio
Victor Stinnerea3183f2013-12-03 01:08:00 +01002
Victor Stinner9592edb2014-02-02 15:03:02 +01003.. _asyncio-event-loop:
Victor Stinnerea3183f2013-12-03 01:08:00 +01004
Victor Stinneraea82292014-07-08 23:42:38 +02005Base Event Loop
6===============
Victor Stinnerea3183f2013-12-03 01:08:00 +01007
8The event loop is the central execution device provided by :mod:`asyncio`.
Zachary Ware5e580da2015-08-27 15:54:39 -05009It provides multiple facilities, including:
Victor Stinnerea3183f2013-12-03 01:08:00 +010010
Eli Benderskyb73c8332014-02-09 06:07:47 -080011* Registering, executing and cancelling delayed calls (timeouts).
Victor Stinnerea3183f2013-12-03 01:08:00 +010012
Victor Stinner9592edb2014-02-02 15:03:02 +010013* Creating client and server :ref:`transports <asyncio-transport>` for various
Eli Benderskyb73c8332014-02-09 06:07:47 -080014 kinds of communication.
Victor Stinnerea3183f2013-12-03 01:08:00 +010015
Eli Bendersky136fea22014-02-09 06:55:58 -080016* Launching subprocesses and the associated :ref:`transports
17 <asyncio-transport>` for communication with an external program.
Victor Stinnerea3183f2013-12-03 01:08:00 +010018
Eli Benderskyb73c8332014-02-09 06:07:47 -080019* Delegating costly function calls to a pool of threads.
Victor Stinnerea3183f2013-12-03 01:08:00 +010020
Victor Stinneraea82292014-07-08 23:42:38 +020021.. class:: BaseEventLoop
Eli Bendersky136fea22014-02-09 06:55:58 -080022
Victor Stinneraea82292014-07-08 23:42:38 +020023 Base class of event loops.
Victor Stinnerea3183f2013-12-03 01:08:00 +010024
Victor Stinner83704962015-02-25 14:24:15 +010025 This class is :ref:`not thread safe <asyncio-multithreading>`.
26
Victor Stinnerea3183f2013-12-03 01:08:00 +010027Run an event loop
28-----------------
29
30.. method:: BaseEventLoop.run_forever()
31
32 Run until :meth:`stop` is called.
33
34.. method:: BaseEventLoop.run_until_complete(future)
35
Victor Stinner99c2ab42013-12-03 19:17:25 +010036 Run until the :class:`Future` is done.
Victor Stinnerea3183f2013-12-03 01:08:00 +010037
Victor Stinner530ef2f2014-07-08 12:39:10 +020038 If the argument is a :ref:`coroutine object <coroutine>`, it is wrapped by
39 :func:`async`.
Victor Stinnerea3183f2013-12-03 01:08:00 +010040
41 Return the Future's result, or raise its exception.
42
43.. method:: BaseEventLoop.is_running()
44
45 Returns running status of event loop.
46
Victor Stinnerafbf8272013-12-03 02:05:42 +010047.. method:: BaseEventLoop.stop()
Victor Stinnerea3183f2013-12-03 01:08:00 +010048
49 Stop running the event loop.
50
51 Every callback scheduled before :meth:`stop` is called will run.
Andrew Svetlovca4f3432014-07-24 11:36:33 +030052 Callbacks scheduled after :meth:`stop` is called will not run.
53 However, those callbacks will run if :meth:`run_forever` is called
54 again later.
Victor Stinnerea3183f2013-12-03 01:08:00 +010055
Victor Stinnerbb2fc5b2014-06-10 10:23:10 +020056.. method:: BaseEventLoop.is_closed()
57
58 Returns ``True`` if the event loop was closed.
59
60 .. versionadded:: 3.4.2
61
Victor Stinnerea3183f2013-12-03 01:08:00 +010062.. method:: BaseEventLoop.close()
63
Terry Jan Reedy9ff41802014-07-24 02:59:02 -040064 Close the event loop. The loop must not be running.
Victor Stinnerea3183f2013-12-03 01:08:00 +010065
66 This clears the queues and shuts down the executor, but does not wait for
67 the executor to finish.
68
69 This is idempotent and irreversible. No other methods should be called after
70 this one.
71
Victor Stinner8464c242014-11-28 13:15:41 +010072.. _asyncio-pass-keywords:
Victor Stinnerea3183f2013-12-03 01:08:00 +010073
74Calls
75-----
76
Victor Stinner8464c242014-11-28 13:15:41 +010077Most :mod:`asyncio` functions don't accept keywords. If you want to pass
78keywords to your callback, use :func:`functools.partial`. For example,
79``loop.call_soon(functools.partial(print, "Hello", flush=True))`` will call
80``print("Hello", flush=True)``.
81
82.. note::
83 :func:`functools.partial` is better than ``lambda`` functions, because
84 :mod:`asyncio` can inspect :func:`functools.partial` object to display
85 parameters in debug mode, whereas ``lambda`` functions have a poor
86 representation.
87
Victor Stinnerea3183f2013-12-03 01:08:00 +010088.. method:: BaseEventLoop.call_soon(callback, \*args)
89
Victor Stinner4d5115c2014-12-15 17:50:55 +010090 Arrange for a callback to be called as soon as possible. The callback is
91 called after :meth:`call_soon` returns, when control returns to the event
92 loop.
Victor Stinnerea3183f2013-12-03 01:08:00 +010093
94 This operates as a FIFO queue, callbacks are called in the order in
95 which they are registered. Each callback will be called exactly once.
96
97 Any positional arguments after the callback will be passed to the
98 callback when it is called.
99
Yury Selivanov1096f762015-06-25 13:49:52 -0400100 An instance of :class:`asyncio.Handle` is returned, which can be
101 used to cancel the callback.
Yury Selivanov43ee1c12014-02-19 20:58:44 -0500102
Victor Stinner8464c242014-11-28 13:15:41 +0100103 :ref:`Use functools.partial to pass keywords to the callback
104 <asyncio-pass-keywords>`.
105
Victor Stinnerea3183f2013-12-03 01:08:00 +0100106.. method:: BaseEventLoop.call_soon_threadsafe(callback, \*args)
107
108 Like :meth:`call_soon`, but thread safe.
109
Victor Stinner83704962015-02-25 14:24:15 +0100110 See the :ref:`concurrency and multithreading <asyncio-multithreading>`
111 section of the documentation.
112
Victor Stinnerea3183f2013-12-03 01:08:00 +0100113
Victor Stinner45b27ed2014-02-01 02:36:43 +0100114.. _asyncio-delayed-calls:
115
Victor Stinnerea3183f2013-12-03 01:08:00 +0100116Delayed calls
117-------------
118
119The event loop has its own internal clock for computing timeouts.
120Which clock is used depends on the (platform-specific) event loop
121implementation; ideally it is a monotonic clock. This will generally be
122a different clock than :func:`time.time`.
123
Victor Stinnerfd9d3742014-02-18 09:37:43 +0100124.. note::
125
126 Timeouts (relative *delay* or absolute *when*) should not exceed one day.
127
Victor Stinner45b27ed2014-02-01 02:36:43 +0100128
Victor Stinnerea3183f2013-12-03 01:08:00 +0100129.. method:: BaseEventLoop.call_later(delay, callback, *args)
130
131 Arrange for the *callback* to be called after the given *delay*
132 seconds (either an int or float).
133
Yury Selivanov1096f762015-06-25 13:49:52 -0400134 An instance of :class:`asyncio.Handle` is returned, which can be
135 used to cancel the callback.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100136
137 *callback* will be called exactly once per call to :meth:`call_later`.
138 If two callbacks are scheduled for exactly the same time, it is
139 undefined which will be called first.
140
141 The optional positional *args* will be passed to the callback when it
142 is called. If you want the callback to be called with some named
143 arguments, use a closure or :func:`functools.partial`.
144
Victor Stinner8464c242014-11-28 13:15:41 +0100145 :ref:`Use functools.partial to pass keywords to the callback
146 <asyncio-pass-keywords>`.
147
Victor Stinnerea3183f2013-12-03 01:08:00 +0100148.. method:: BaseEventLoop.call_at(when, callback, *args)
149
150 Arrange for the *callback* to be called at the given absolute timestamp
Berker Peksagb5563992014-11-07 19:51:07 +0200151 *when* (an int or float), using the same time reference as
152 :meth:`BaseEventLoop.time`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100153
154 This method's behavior is the same as :meth:`call_later`.
155
Yury Selivanov1096f762015-06-25 13:49:52 -0400156 An instance of :class:`asyncio.Handle` is returned, which can be
157 used to cancel the callback.
158
Victor Stinner8464c242014-11-28 13:15:41 +0100159 :ref:`Use functools.partial to pass keywords to the callback
160 <asyncio-pass-keywords>`.
161
Victor Stinnerea3183f2013-12-03 01:08:00 +0100162.. method:: BaseEventLoop.time()
163
164 Return the current time, as a :class:`float` value, according to the
165 event loop's internal clock.
166
Victor Stinner3e09e322013-12-03 01:22:06 +0100167.. seealso::
168
169 The :func:`asyncio.sleep` function.
170
Victor Stinnerea3183f2013-12-03 01:08:00 +0100171
Yury Selivanovbb961342015-06-25 11:54:34 -0400172Tasks
173-----
Victor Stinner530ef2f2014-07-08 12:39:10 +0200174
175.. method:: BaseEventLoop.create_task(coro)
176
177 Schedule the execution of a :ref:`coroutine object <coroutine>`: wrap it in
178 a future. Return a :class:`Task` object.
179
180 Third-party event loops can use their own subclass of :class:`Task` for
181 interoperability. In this case, the result type is a subclass of
182 :class:`Task`.
183
Victor Stinner337e03f2014-08-11 01:11:13 +0200184 This method was added in Python 3.4.2. Use the :func:`async` function to
185 support also older Python versions.
Victor Stinner530ef2f2014-07-08 12:39:10 +0200186
187 .. versionadded:: 3.4.2
188
Yury Selivanov71854612015-05-11 16:28:27 -0400189.. method:: BaseEventLoop.set_task_factory(factory)
190
191 Set a task factory that will be used by
192 :meth:`BaseEventLoop.create_task`.
193
194 If *factory* is ``None`` the default task factory will be set.
195
196 If *factory* is a *callable*, it should have a signature matching
197 ``(loop, coro)``, where *loop* will be a reference to the active
198 event loop, *coro* will be a coroutine object. The callable
199 must return an :class:`asyncio.Future` compatible object.
200
201 .. versionadded:: 3.4.4
202
203.. method:: BaseEventLoop.get_task_factory()
204
205 Return a task factory, or ``None`` if the default one is in use.
206
207 .. versionadded:: 3.4.4
208
Victor Stinner530ef2f2014-07-08 12:39:10 +0200209
Victor Stinnerea3183f2013-12-03 01:08:00 +0100210Creating connections
Victor Stinner0c6f1ca2013-12-03 01:46:39 +0100211--------------------
Victor Stinnerea3183f2013-12-03 01:08:00 +0100212
Victor Stinnerbdd574d2015-02-12 22:49:18 +0100213.. coroutinemethod:: BaseEventLoop.create_connection(protocol_factory, host=None, port=None, \*, ssl=None, family=0, proto=0, flags=0, sock=None, local_addr=None, server_hostname=None)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100214
215 Create a streaming transport connection to a given Internet *host* and
Victor Stinnera6919aa2014-02-19 13:32:34 +0100216 *port*: socket family :py:data:`~socket.AF_INET` or
217 :py:data:`~socket.AF_INET6` depending on *host* (or *family* if specified),
218 socket type :py:data:`~socket.SOCK_STREAM`. *protocol_factory* must be a
219 callable returning a :ref:`protocol <asyncio-protocol>` instance.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100220
Yury Selivanov37f15bc2014-02-20 16:20:44 -0500221 This method is a :ref:`coroutine <coroutine>` which will try to
Victor Stinnerea3183f2013-12-03 01:08:00 +0100222 establish the connection in the background. When successful, the
223 coroutine returns a ``(transport, protocol)`` pair.
224
225 The chronological synopsis of the underlying operation is as follows:
226
Victor Stinner9592edb2014-02-02 15:03:02 +0100227 #. The connection is established, and a :ref:`transport <asyncio-transport>`
Victor Stinnerea3183f2013-12-03 01:08:00 +0100228 is created to represent it.
229
230 #. *protocol_factory* is called without arguments and must return a
Victor Stinner9592edb2014-02-02 15:03:02 +0100231 :ref:`protocol <asyncio-protocol>` instance.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100232
233 #. The protocol instance is tied to the transport, and its
234 :meth:`connection_made` method is called.
235
236 #. The coroutine returns successfully with the ``(transport, protocol)``
237 pair.
238
239 The created transport is an implementation-dependent bidirectional stream.
240
241 .. note::
242 *protocol_factory* can be any kind of callable, not necessarily
243 a class. For example, if you want to use a pre-created
244 protocol instance, you can pass ``lambda: my_protocol``.
245
246 Options allowing to change how the connection is created:
247
248 * *ssl*: if given and not false, a SSL/TLS transport is created
249 (by default a plain TCP transport is created). If *ssl* is
250 a :class:`ssl.SSLContext` object, this context is used to create
251 the transport; if *ssl* is :const:`True`, a context with some
252 unspecified default settings is used.
253
Berker Peksag9c1dba22014-09-28 00:00:58 +0300254 .. seealso:: :ref:`SSL/TLS security considerations <ssl-security>`
Antoine Pitrouc5e075f2014-03-22 18:19:11 +0100255
Victor Stinnerea3183f2013-12-03 01:08:00 +0100256 * *server_hostname*, is only for use together with *ssl*,
257 and sets or overrides the hostname that the target server's certificate
258 will be matched against. By default the value of the *host* argument
259 is used. If *host* is empty, there is no default and you must pass a
260 value for *server_hostname*. If *server_hostname* is an empty
261 string, hostname matching is disabled (which is a serious security
262 risk, allowing for man-in-the-middle-attacks).
263
264 * *family*, *proto*, *flags* are the optional address family, protocol
265 and flags to be passed through to getaddrinfo() for *host* resolution.
266 If given, these should all be integers from the corresponding
267 :mod:`socket` module constants.
268
269 * *sock*, if given, should be an existing, already connected
270 :class:`socket.socket` object to be used by the transport.
271 If *sock* is given, none of *host*, *port*, *family*, *proto*, *flags*
272 and *local_addr* should be specified.
273
274 * *local_addr*, if given, is a ``(local_host, local_port)`` tuple used
275 to bind the socket to locally. The *local_host* and *local_port*
276 are looked up using getaddrinfo(), similarly to *host* and *port*.
277
Victor Stinner41f3c3f2014-08-31 14:47:37 +0200278 On Windows with :class:`ProactorEventLoop`, SSL/TLS is not supported.
279
Victor Stinnerc8ea8132014-01-23 11:02:09 +0100280 .. seealso::
281
282 The :func:`open_connection` function can be used to get a pair of
283 (:class:`StreamReader`, :class:`StreamWriter`) instead of a protocol.
284
Victor Stinnerea3183f2013-12-03 01:08:00 +0100285
Guido van Rossumb9bf9132015-10-05 09:15:28 -0700286.. coroutinemethod:: BaseEventLoop.create_datagram_endpoint(protocol_factory, local_addr=None, remote_addr=None, \*, family=0, proto=0, flags=0, reuse_address=None, reuse_port=None, allow_broadcast=None, sock=None)
Victor Stinnera6919aa2014-02-19 13:32:34 +0100287
288 Create datagram connection: socket family :py:data:`~socket.AF_INET` or
289 :py:data:`~socket.AF_INET6` depending on *host* (or *family* if specified),
Guido van Rossumb9bf9132015-10-05 09:15:28 -0700290 socket type :py:data:`~socket.SOCK_DGRAM`. *protocol_factory* must be a
291 callable returning a :ref:`protocol <asyncio-protocol>` instance.
Victor Stinnera6919aa2014-02-19 13:32:34 +0100292
Yury Selivanov37f15bc2014-02-20 16:20:44 -0500293 This method is a :ref:`coroutine <coroutine>` which will try to
Victor Stinnera6919aa2014-02-19 13:32:34 +0100294 establish the connection in the background. When successful, the
295 coroutine returns a ``(transport, protocol)`` pair.
296
Guido van Rossumb9bf9132015-10-05 09:15:28 -0700297 Options changing how the connection is created:
298
299 * *local_addr*, if given, is a ``(local_host, local_port)`` tuple used
300 to bind the socket to locally. The *local_host* and *local_port*
301 are looked up using :meth:`getaddrinfo`.
302
303 * *remote_addr*, if given, is a ``(remote_host, remote_port)`` tuple used
304 to connect the socket to a remote address. The *remote_host* and
305 *remote_port* are looked up using :meth:`getaddrinfo`.
306
307 * *family*, *proto*, *flags* are the optional address family, protocol
308 and flags to be passed through to :meth:`getaddrinfo` for *host*
309 resolution. If given, these should all be integers from the
310 corresponding :mod:`socket` module constants.
311
312 * *reuse_address* tells the kernel to reuse a local socket in
313 TIME_WAIT state, without waiting for its natural timeout to
314 expire. If not specified will automatically be set to True on
315 UNIX.
316
317 * *reuse_port* tells the kernel to allow this endpoint to be bound to the
318 same port as other existing endpoints are bound to, so long as they all
319 set this flag when being created. This option is not supported on Windows
320 and some UNIX's. If the :py:data:`~socket.SO_REUSEPORT` constant is not
321 defined then this capability is unsupported.
322
323 * *allow_broadcast* tells the kernel to allow this endpoint to send
324 messages to the broadcast address.
325
326 * *sock* can optionally be specified in order to use a preexisting,
327 already connected, :class:`socket.socket` object to be used by the
328 transport. If specified, *local_addr* and *remote_addr* should be omitted
329 (must be :const:`None`).
Victor Stinnera6919aa2014-02-19 13:32:34 +0100330
Victor Stinner41f3c3f2014-08-31 14:47:37 +0200331 On Windows with :class:`ProactorEventLoop`, this method is not supported.
332
Victor Stinnerc7edffd2014-10-12 11:24:26 +0200333 See :ref:`UDP echo client protocol <asyncio-udp-echo-client-protocol>` and
334 :ref:`UDP echo server protocol <asyncio-udp-echo-server-protocol>` examples.
335
Victor Stinnera6919aa2014-02-19 13:32:34 +0100336
Victor Stinnerbdd574d2015-02-12 22:49:18 +0100337.. coroutinemethod:: BaseEventLoop.create_unix_connection(protocol_factory, path, \*, ssl=None, sock=None, server_hostname=None)
Victor Stinnera6919aa2014-02-19 13:32:34 +0100338
339 Create UNIX connection: socket family :py:data:`~socket.AF_UNIX`, socket
340 type :py:data:`~socket.SOCK_STREAM`. The :py:data:`~socket.AF_UNIX` socket
341 family is used to communicate between processes on the same machine
342 efficiently.
343
Yury Selivanov37f15bc2014-02-20 16:20:44 -0500344 This method is a :ref:`coroutine <coroutine>` which will try to
Victor Stinnera6919aa2014-02-19 13:32:34 +0100345 establish the connection in the background. When successful, the
346 coroutine returns a ``(transport, protocol)`` pair.
347
348 See the :meth:`BaseEventLoop.create_connection` method for parameters.
349
350 Availability: UNIX.
351
352
Victor Stinnerea3183f2013-12-03 01:08:00 +0100353Creating listening connections
354------------------------------
355
Guido van Rossumb9bf9132015-10-05 09:15:28 -0700356.. coroutinemethod:: BaseEventLoop.create_server(protocol_factory, host=None, port=None, \*, family=socket.AF_UNSPEC, flags=socket.AI_PASSIVE, sock=None, backlog=100, ssl=None, reuse_address=None, reuse_port=None)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100357
Victor Stinner33f6abe2014-10-12 20:36:04 +0200358 Create a TCP server (socket type :data:`~socket.SOCK_STREAM`) bound to
359 *host* and *port*.
360
361 Return a :class:`Server` object, its :attr:`~Server.sockets` attribute
362 contains created sockets. Use the :meth:`Server.close` method to stop the
363 server: close listening sockets.
364
365 Parameters:
366
Victor Stinner5e4a7d82015-09-21 18:33:43 +0200367 * The *host* parameter can be a string, in that case the TCP server is
368 bound to *host* and *port*. The *host* parameter can also be a sequence
369 of strings and in that case the TCP server is bound to all hosts of the
370 sequence. If *host* is an empty string or ``None``, all interfaces are
371 assumed and a list of multiple sockets will be returned (most likely one
372 for IPv4 and another one for IPv6).
Victor Stinner33f6abe2014-10-12 20:36:04 +0200373
374 * *family* can be set to either :data:`socket.AF_INET` or
375 :data:`~socket.AF_INET6` to force the socket to use IPv4 or IPv6. If not set
376 it will be determined from host (defaults to :data:`socket.AF_UNSPEC`).
377
378 * *flags* is a bitmask for :meth:`getaddrinfo`.
379
380 * *sock* can optionally be specified in order to use a preexisting
381 socket object. If specified, *host* and *port* should be omitted (must be
382 :const:`None`).
383
384 * *backlog* is the maximum number of queued connections passed to
385 :meth:`~socket.socket.listen` (defaults to 100).
386
387 * *ssl* can be set to an :class:`~ssl.SSLContext` to enable SSL over the
388 accepted connections.
389
390 * *reuse_address* tells the kernel to reuse a local socket in
391 TIME_WAIT state, without waiting for its natural timeout to
392 expire. If not specified will automatically be set to True on
393 UNIX.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100394
Guido van Rossumb9bf9132015-10-05 09:15:28 -0700395 * *reuse_port* tells the kernel to allow this endpoint to be bound to the
396 same port as other existing endpoints are bound to, so long as they all
397 set this flag when being created. This option is not supported on
398 Windows.
399
Victor Stinnerd1432092014-06-19 17:11:49 +0200400 This method is a :ref:`coroutine <coroutine>`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100401
Victor Stinner41f3c3f2014-08-31 14:47:37 +0200402 On Windows with :class:`ProactorEventLoop`, SSL/TLS is not supported.
403
Victor Stinnerc8ea8132014-01-23 11:02:09 +0100404 .. seealso::
405
406 The function :func:`start_server` creates a (:class:`StreamReader`,
407 :class:`StreamWriter`) pair and calls back a function with this pair.
408
Victor Stinner5e4a7d82015-09-21 18:33:43 +0200409 .. versionchanged:: 3.4.4
410
411 The *host* parameter can now be a sequence of strings.
412
Victor Stinnerea3183f2013-12-03 01:08:00 +0100413
Victor Stinnerbdd574d2015-02-12 22:49:18 +0100414.. coroutinemethod:: BaseEventLoop.create_unix_server(protocol_factory, path=None, \*, sock=None, backlog=100, ssl=None)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100415
Victor Stinnera6919aa2014-02-19 13:32:34 +0100416 Similar to :meth:`BaseEventLoop.create_server`, but specific to the
417 socket family :py:data:`~socket.AF_UNIX`.
418
Victor Stinnerbdd574d2015-02-12 22:49:18 +0100419 This method is a :ref:`coroutine <coroutine>`.
420
Victor Stinnera6919aa2014-02-19 13:32:34 +0100421 Availability: UNIX.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100422
423
Victor Stinnerc1567df2014-02-08 23:22:58 +0100424Watch file descriptors
425----------------------
426
Victor Stinner41f3c3f2014-08-31 14:47:37 +0200427On Windows with :class:`SelectorEventLoop`, only socket handles are supported
428(ex: pipe file descriptors are not supported).
429
430On Windows with :class:`ProactorEventLoop`, these methods are not supported.
431
Victor Stinnerc1567df2014-02-08 23:22:58 +0100432.. method:: BaseEventLoop.add_reader(fd, callback, \*args)
433
434 Start watching the file descriptor for read availability and then call the
435 *callback* with specified arguments.
436
Victor Stinner8464c242014-11-28 13:15:41 +0100437 :ref:`Use functools.partial to pass keywords to the callback
438 <asyncio-pass-keywords>`.
439
Victor Stinnerc1567df2014-02-08 23:22:58 +0100440.. method:: BaseEventLoop.remove_reader(fd)
441
442 Stop watching the file descriptor for read availability.
443
444.. method:: BaseEventLoop.add_writer(fd, callback, \*args)
445
446 Start watching the file descriptor for write availability and then call the
447 *callback* with specified arguments.
448
Victor Stinner8464c242014-11-28 13:15:41 +0100449 :ref:`Use functools.partial to pass keywords to the callback
450 <asyncio-pass-keywords>`.
451
Victor Stinnerc1567df2014-02-08 23:22:58 +0100452.. method:: BaseEventLoop.remove_writer(fd)
453
454 Stop watching the file descriptor for write availability.
455
Victor Stinner04e6df32014-10-11 16:16:27 +0200456The :ref:`watch a file descriptor for read events <asyncio-watch-read-event>`
457example uses the low-level :meth:`BaseEventLoop.add_reader` method to register
458the file descriptor of a socket.
459
Victor Stinnerc1567df2014-02-08 23:22:58 +0100460
461Low-level socket operations
462---------------------------
463
Victor Stinnerbdd574d2015-02-12 22:49:18 +0100464.. coroutinemethod:: BaseEventLoop.sock_recv(sock, nbytes)
Victor Stinnerc1567df2014-02-08 23:22:58 +0100465
466 Receive data from the socket. The return value is a bytes object
467 representing the data received. The maximum amount of data to be received
468 at once is specified by *nbytes*.
469
Victor Stinnerd84fd732014-08-26 01:01:59 +0200470 With :class:`SelectorEventLoop` event loop, the socket *sock* must be
471 non-blocking.
Victor Stinnerec2ce092014-07-29 23:12:22 +0200472
Yury Selivanov37f15bc2014-02-20 16:20:44 -0500473 This method is a :ref:`coroutine <coroutine>`.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100474
475 .. seealso::
476
477 The :meth:`socket.socket.recv` method.
478
Victor Stinnerbdd574d2015-02-12 22:49:18 +0100479.. coroutinemethod:: BaseEventLoop.sock_sendall(sock, data)
Victor Stinnerc1567df2014-02-08 23:22:58 +0100480
481 Send data to the socket. The socket must be connected to a remote socket.
482 This method continues to send data from *data* until either all data has
483 been sent or an error occurs. ``None`` is returned on success. On error,
484 an exception is raised, and there is no way to determine how much data, if
Yury Selivanov37f15bc2014-02-20 16:20:44 -0500485 any, was successfully processed by the receiving end of the connection.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100486
Victor Stinnerd84fd732014-08-26 01:01:59 +0200487 With :class:`SelectorEventLoop` event loop, the socket *sock* must be
488 non-blocking.
Victor Stinnerec2ce092014-07-29 23:12:22 +0200489
Yury Selivanov37f15bc2014-02-20 16:20:44 -0500490 This method is a :ref:`coroutine <coroutine>`.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100491
492 .. seealso::
493
494 The :meth:`socket.socket.sendall` method.
495
Victor Stinnerbdd574d2015-02-12 22:49:18 +0100496.. coroutinemethod:: BaseEventLoop.sock_connect(sock, address)
Victor Stinnerc1567df2014-02-08 23:22:58 +0100497
498 Connect to a remote socket at *address*.
499
Victor Stinner1b0580b2014-02-13 09:24:37 +0100500 The *address* must be already resolved to avoid the trap of hanging the
501 entire event loop when the address requires doing a DNS lookup. For
502 example, it must be an IP address, not an hostname, for
503 :py:data:`~socket.AF_INET` and :py:data:`~socket.AF_INET6` address families.
504 Use :meth:`getaddrinfo` to resolve the hostname asynchronously.
505
Victor Stinnerd84fd732014-08-26 01:01:59 +0200506 With :class:`SelectorEventLoop` event loop, the socket *sock* must be
507 non-blocking.
Victor Stinnerec2ce092014-07-29 23:12:22 +0200508
Yury Selivanov37f15bc2014-02-20 16:20:44 -0500509 This method is a :ref:`coroutine <coroutine>`.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100510
511 .. seealso::
512
513 The :meth:`BaseEventLoop.create_connection` method, the
514 :func:`open_connection` function and the :meth:`socket.socket.connect`
515 method.
516
517
Victor Stinnerbdd574d2015-02-12 22:49:18 +0100518.. coroutinemethod:: BaseEventLoop.sock_accept(sock)
Victor Stinnerc1567df2014-02-08 23:22:58 +0100519
520 Accept a connection. The socket must be bound to an address and listening
521 for connections. The return value is a pair ``(conn, address)`` where *conn*
522 is a *new* socket object usable to send and receive data on the connection,
523 and *address* is the address bound to the socket on the other end of the
524 connection.
525
Victor Stinnerec2ce092014-07-29 23:12:22 +0200526 The socket *sock* must be non-blocking.
527
Yury Selivanov37f15bc2014-02-20 16:20:44 -0500528 This method is a :ref:`coroutine <coroutine>`.
Victor Stinnerc1567df2014-02-08 23:22:58 +0100529
530 .. seealso::
531
532 The :meth:`BaseEventLoop.create_server` method, the :func:`start_server`
533 function and the :meth:`socket.socket.accept` method.
534
535
536Resolve host name
537-----------------
Victor Stinnerea3183f2013-12-03 01:08:00 +0100538
Victor Stinnerbdd574d2015-02-12 22:49:18 +0100539.. coroutinemethod:: BaseEventLoop.getaddrinfo(host, port, \*, family=0, type=0, proto=0, flags=0)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100540
Yury Selivanov37f15bc2014-02-20 16:20:44 -0500541 This method is a :ref:`coroutine <coroutine>`, similar to
542 :meth:`socket.getaddrinfo` function but non-blocking.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100543
Victor Stinnerbdd574d2015-02-12 22:49:18 +0100544.. coroutinemethod:: BaseEventLoop.getnameinfo(sockaddr, flags=0)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100545
Yury Selivanov37f15bc2014-02-20 16:20:44 -0500546 This method is a :ref:`coroutine <coroutine>`, similar to
547 :meth:`socket.getnameinfo` function but non-blocking.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100548
549
Victor Stinner984600f2014-03-25 09:40:26 +0100550Connect pipes
551-------------
Victor Stinnerea3183f2013-12-03 01:08:00 +0100552
Victor Stinner41f3c3f2014-08-31 14:47:37 +0200553On Windows with :class:`SelectorEventLoop`, these methods are not supported.
554Use :class:`ProactorEventLoop` to support pipes on Windows.
555
Victor Stinnerbdd574d2015-02-12 22:49:18 +0100556.. coroutinemethod:: BaseEventLoop.connect_read_pipe(protocol_factory, pipe)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100557
Victor Stinnerd84fd732014-08-26 01:01:59 +0200558 Register read pipe in eventloop.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100559
560 *protocol_factory* should instantiate object with :class:`Protocol`
Victor Stinnera5b257a2014-05-29 00:14:03 +0200561 interface. *pipe* is a :term:`file-like object <file object>`.
562 Return pair ``(transport, protocol)``, where *transport* supports the
Victor Stinnerea3183f2013-12-03 01:08:00 +0100563 :class:`ReadTransport` interface.
564
Victor Stinnerd84fd732014-08-26 01:01:59 +0200565 With :class:`SelectorEventLoop` event loop, the *pipe* is set to
566 non-blocking mode.
567
Yury Selivanov37f15bc2014-02-20 16:20:44 -0500568 This method is a :ref:`coroutine <coroutine>`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100569
Victor Stinnerbdd574d2015-02-12 22:49:18 +0100570.. coroutinemethod:: BaseEventLoop.connect_write_pipe(protocol_factory, pipe)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100571
572 Register write pipe in eventloop.
573
574 *protocol_factory* should instantiate object with :class:`BaseProtocol`
Victor Stinner2cef3002014-10-23 22:38:46 +0200575 interface. *pipe* is :term:`file-like object <file object>`.
576 Return pair ``(transport, protocol)``, where *transport* supports
Victor Stinnerea3183f2013-12-03 01:08:00 +0100577 :class:`WriteTransport` interface.
578
Victor Stinnerd84fd732014-08-26 01:01:59 +0200579 With :class:`SelectorEventLoop` event loop, the *pipe* is set to
580 non-blocking mode.
581
Yury Selivanov37f15bc2014-02-20 16:20:44 -0500582 This method is a :ref:`coroutine <coroutine>`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100583
Victor Stinner08444382014-02-02 22:43:39 +0100584.. seealso::
585
Victor Stinner984600f2014-03-25 09:40:26 +0100586 The :meth:`BaseEventLoop.subprocess_exec` and
587 :meth:`BaseEventLoop.subprocess_shell` methods.
Victor Stinner08444382014-02-02 22:43:39 +0100588
Victor Stinnerea3183f2013-12-03 01:08:00 +0100589
Victor Stinner8b863482014-01-27 10:07:50 +0100590UNIX signals
591------------
592
593Availability: UNIX only.
594
595.. method:: BaseEventLoop.add_signal_handler(signum, callback, \*args)
596
597 Add a handler for a signal.
598
599 Raise :exc:`ValueError` if the signal number is invalid or uncatchable.
600 Raise :exc:`RuntimeError` if there is a problem setting up the handler.
601
Victor Stinner8464c242014-11-28 13:15:41 +0100602 :ref:`Use functools.partial to pass keywords to the callback
603 <asyncio-pass-keywords>`.
604
Victor Stinner8b863482014-01-27 10:07:50 +0100605.. method:: BaseEventLoop.remove_signal_handler(sig)
606
607 Remove a handler for a signal.
608
609 Return ``True`` if a signal handler was removed, ``False`` if not.
610
611.. seealso::
612
613 The :mod:`signal` module.
614
615
Victor Stinnerea3183f2013-12-03 01:08:00 +0100616Executor
617--------
618
619Call a function in an :class:`~concurrent.futures.Executor` (pool of threads or
620pool of processes). By default, an event loop uses a thread pool executor
621(:class:`~concurrent.futures.ThreadPoolExecutor`).
622
Andrew Svetlov1c62b522015-10-01 09:48:08 +0300623.. coroutinemethod:: BaseEventLoop.run_in_executor(executor, func, \*args)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100624
Andrew Svetlov1c62b522015-10-01 09:48:08 +0300625 Arrange for a *func* to be called in the specified executor.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100626
Larry Hastings3732ed22014-03-15 21:13:56 -0700627 The *executor* argument should be an :class:`~concurrent.futures.Executor`
628 instance. The default executor is used if *executor* is ``None``.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100629
Andrew Svetlov1c62b522015-10-01 09:48:08 +0300630 :ref:`Use functools.partial to pass keywords to the *func*
Victor Stinner8464c242014-11-28 13:15:41 +0100631 <asyncio-pass-keywords>`.
632
Yury Selivanov37f15bc2014-02-20 16:20:44 -0500633 This method is a :ref:`coroutine <coroutine>`.
634
Victor Stinnerea3183f2013-12-03 01:08:00 +0100635.. method:: BaseEventLoop.set_default_executor(executor)
636
637 Set the default executor used by :meth:`run_in_executor`.
638
639
Yury Selivanov43ee1c12014-02-19 20:58:44 -0500640Error Handling API
641------------------
642
643Allows to customize how exceptions are handled in the event loop.
644
645.. method:: BaseEventLoop.set_exception_handler(handler)
646
647 Set *handler* as the new event loop exception handler.
648
649 If *handler* is ``None``, the default exception handler will
650 be set.
651
652 If *handler* is a callable object, it should have a
653 matching signature to ``(loop, context)``, where ``loop``
654 will be a reference to the active event loop, ``context``
655 will be a ``dict`` object (see :meth:`call_exception_handler`
656 documentation for details about context).
657
658.. method:: BaseEventLoop.default_exception_handler(context)
659
660 Default exception handler.
661
662 This is called when an exception occurs and no exception
663 handler is set, and can be called by a custom exception
664 handler that wants to defer to the default behavior.
665
666 *context* parameter has the same meaning as in
667 :meth:`call_exception_handler`.
668
669.. method:: BaseEventLoop.call_exception_handler(context)
670
671 Call the current event loop exception handler.
672
673 *context* is a ``dict`` object containing the following keys
674 (new keys may be introduced later):
675
676 * 'message': Error message;
677 * 'exception' (optional): Exception object;
678 * 'future' (optional): :class:`asyncio.Future` instance;
679 * 'handle' (optional): :class:`asyncio.Handle` instance;
680 * 'protocol' (optional): :ref:`Protocol <asyncio-protocol>` instance;
681 * 'transport' (optional): :ref:`Transport <asyncio-transport>` instance;
682 * 'socket' (optional): :class:`socket.socket` instance.
683
684 .. note::
685
686 Note: this method should not be overloaded in subclassed
687 event loops. For any custom exception handling, use
688 :meth:`set_exception_handler()` method.
689
Victor Stinner0f3e6bc2014-02-19 23:15:02 +0100690Debug mode
691----------
692
693.. method:: BaseEventLoop.get_debug()
694
Victor Stinner7b7120e2014-06-23 00:12:14 +0200695 Get the debug mode (:class:`bool`) of the event loop.
696
697 The default value is ``True`` if the environment variable
698 :envvar:`PYTHONASYNCIODEBUG` is set to a non-empty string, ``False``
699 otherwise.
Victor Stinner0f3e6bc2014-02-19 23:15:02 +0100700
Victor Stinner64d750b2014-06-18 03:25:23 +0200701 .. versionadded:: 3.4.2
702
Victor Stinner0f3e6bc2014-02-19 23:15:02 +0100703.. method:: BaseEventLoop.set_debug(enabled: bool)
704
705 Set the debug mode of the event loop.
706
Victor Stinner64d750b2014-06-18 03:25:23 +0200707 .. versionadded:: 3.4.2
708
Victor Stinner0f3e6bc2014-02-19 23:15:02 +0100709.. seealso::
710
Victor Stinner62511fd2014-06-23 00:36:11 +0200711 The :ref:`debug mode of asyncio <asyncio-debug-mode>`.
Victor Stinner0f3e6bc2014-02-19 23:15:02 +0100712
Victor Stinner8c462c52014-01-24 18:11:43 +0100713Server
714------
715
Victor Stinner8ebeb032014-07-11 23:47:40 +0200716.. class:: Server
Victor Stinner8c462c52014-01-24 18:11:43 +0100717
Victor Stinner8ebeb032014-07-11 23:47:40 +0200718 Server listening on sockets.
719
720 Object created by the :meth:`BaseEventLoop.create_server` method and the
R David Murray756f0b12015-01-29 19:53:33 -0500721 :func:`start_server` function. Don't instantiate the class directly.
Victor Stinner8c462c52014-01-24 18:11:43 +0100722
723 .. method:: close()
724
Victor Stinner4bfb14a2014-07-12 03:20:24 +0200725 Stop serving: close listening sockets and set the :attr:`sockets`
726 attribute to ``None``.
727
728 The sockets that represent existing incoming client connections are
729 leaved open.
Victor Stinner8ebeb032014-07-11 23:47:40 +0200730
731 The server is closed asynchonously, use the :meth:`wait_closed` coroutine
732 to wait until the server is closed.
Victor Stinner8c462c52014-01-24 18:11:43 +0100733
Victor Stinnerbdd574d2015-02-12 22:49:18 +0100734 .. coroutinemethod:: wait_closed()
Victor Stinner8c462c52014-01-24 18:11:43 +0100735
Victor Stinner8ebeb032014-07-11 23:47:40 +0200736 Wait until the :meth:`close` method completes.
737
738 This method is a :ref:`coroutine <coroutine>`.
739
740 .. attribute:: sockets
741
742 List of :class:`socket.socket` objects the server is listening to, or
743 ``None`` if the server is closed.
Victor Stinner8c462c52014-01-24 18:11:43 +0100744
745
Yury Selivanov43ee1c12014-02-19 20:58:44 -0500746Handle
747------
748
749.. class:: Handle
750
751 A callback wrapper object returned by :func:`BaseEventLoop.call_soon`,
752 :func:`BaseEventLoop.call_soon_threadsafe`, :func:`BaseEventLoop.call_later`,
753 and :func:`BaseEventLoop.call_at`.
754
755 .. method:: cancel()
756
Yury Selivanov1096f762015-06-25 13:49:52 -0400757 Cancel the call. If the callback is already canceled or executed,
758 this method has no effect.
Victor Stinneraea82292014-07-08 23:42:38 +0200759
Yury Selivanov43ee1c12014-02-19 20:58:44 -0500760
Victor Stinner6888b962014-10-11 16:15:58 +0200761Event loop examples
Victor Stinnera092a612015-01-09 15:58:41 +0100762-------------------
Yury Selivanov43ee1c12014-02-19 20:58:44 -0500763
Victor Stinner3e09e322013-12-03 01:22:06 +0100764.. _asyncio-hello-world-callback:
Victor Stinnerea3183f2013-12-03 01:08:00 +0100765
Victor Stinner7f314ed2014-10-15 18:49:16 +0200766Hello World with call_soon()
Victor Stinnera092a612015-01-09 15:58:41 +0100767^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinnerea3183f2013-12-03 01:08:00 +0100768
Victor Stinner7f314ed2014-10-15 18:49:16 +0200769Example using the :meth:`BaseEventLoop.call_soon` method to schedule a
770callback. The callback displays ``"Hello World"`` and then stops the event
771loop::
Victor Stinnerea3183f2013-12-03 01:08:00 +0100772
773 import asyncio
774
Victor Stinner7f314ed2014-10-15 18:49:16 +0200775 def hello_world(loop):
Victor Stinnerea3183f2013-12-03 01:08:00 +0100776 print('Hello World')
Victor Stinner7f314ed2014-10-15 18:49:16 +0200777 loop.stop()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100778
779 loop = asyncio.get_event_loop()
Victor Stinner7f314ed2014-10-15 18:49:16 +0200780
781 # Schedule a call to hello_world()
782 loop.call_soon(hello_world, loop)
783
784 # Blocking call interrupted by loop.stop()
785 loop.run_forever()
786 loop.close()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100787
Victor Stinner3e09e322013-12-03 01:22:06 +0100788.. seealso::
Victor Stinnerea3183f2013-12-03 01:08:00 +0100789
Victor Stinner6888b962014-10-11 16:15:58 +0200790 The :ref:`Hello World coroutine <asyncio-hello-world-coroutine>` example
791 uses a :ref:`coroutine <coroutine>`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100792
Victor Stinner8b863482014-01-27 10:07:50 +0100793
Victor Stinner7f314ed2014-10-15 18:49:16 +0200794.. _asyncio-date-callback:
795
796Display the current date with call_later()
Victor Stinnera092a612015-01-09 15:58:41 +0100797^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinner7f314ed2014-10-15 18:49:16 +0200798
799Example of callback displaying the current date every second. The callback uses
800the :meth:`BaseEventLoop.call_later` method to reschedule itself during 5
801seconds, and then stops the event loop::
802
803 import asyncio
804 import datetime
805
806 def display_date(end_time, loop):
807 print(datetime.datetime.now())
808 if (loop.time() + 1.0) < end_time:
809 loop.call_later(1, display_date, end_time, loop)
810 else:
811 loop.stop()
812
813 loop = asyncio.get_event_loop()
814
815 # Schedule the first call to display_date()
816 end_time = loop.time() + 5.0
817 loop.call_soon(display_date, end_time, loop)
818
819 # Blocking call interrupted by loop.stop()
820 loop.run_forever()
821 loop.close()
822
823.. seealso::
824
825 The :ref:`coroutine displaying the current date
826 <asyncio-date-coroutine>` example uses a :ref:`coroutine
827 <coroutine>`.
828
829
Victor Stinner04e6df32014-10-11 16:16:27 +0200830.. _asyncio-watch-read-event:
Victor Stinner8b863482014-01-27 10:07:50 +0100831
Victor Stinner04e6df32014-10-11 16:16:27 +0200832Watch a file descriptor for read events
Victor Stinnera092a612015-01-09 15:58:41 +0100833^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinner04e6df32014-10-11 16:16:27 +0200834
835Wait until a file descriptor received some data using the
836:meth:`BaseEventLoop.add_reader` method and then close the event loop::
837
838 import asyncio
Victor Stinnerccd8e342014-10-11 16:30:02 +0200839 try:
840 from socket import socketpair
841 except ImportError:
842 from asyncio.windows_utils import socketpair
Victor Stinner04e6df32014-10-11 16:16:27 +0200843
844 # Create a pair of connected file descriptors
Victor Stinnerccd8e342014-10-11 16:30:02 +0200845 rsock, wsock = socketpair()
Victor Stinner04e6df32014-10-11 16:16:27 +0200846 loop = asyncio.get_event_loop()
847
848 def reader():
849 data = rsock.recv(100)
850 print("Received:", data.decode())
Victor Stinner2cef3002014-10-23 22:38:46 +0200851 # We are done: unregister the file descriptor
Victor Stinner04e6df32014-10-11 16:16:27 +0200852 loop.remove_reader(rsock)
853 # Stop the event loop
854 loop.stop()
855
Victor Stinner2cef3002014-10-23 22:38:46 +0200856 # Register the file descriptor for read event
Victor Stinner04e6df32014-10-11 16:16:27 +0200857 loop.add_reader(rsock, reader)
858
859 # Simulate the reception of data from the network
860 loop.call_soon(wsock.send, 'abc'.encode())
861
862 # Run the event loop
863 loop.run_forever()
864
865 # We are done, close sockets and the event loop
866 rsock.close()
867 wsock.close()
868 loop.close()
869
870.. seealso::
871
872 The :ref:`register an open socket to wait for data using a protocol
873 <asyncio-register-socket>` example uses a low-level protocol created by the
874 :meth:`BaseEventLoop.create_connection` method.
875
876 The :ref:`register an open socket to wait for data using streams
877 <asyncio-register-socket-streams>` example uses high-level streams
878 created by the :func:`open_connection` function in a coroutine.
879
880
881Set signal handlers for SIGINT and SIGTERM
Victor Stinnera092a612015-01-09 15:58:41 +0100882^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Victor Stinner04e6df32014-10-11 16:16:27 +0200883
884Register handlers for signals :py:data:`SIGINT` and :py:data:`SIGTERM` using
885the :meth:`BaseEventLoop.add_signal_handler` method::
Victor Stinner8b863482014-01-27 10:07:50 +0100886
887 import asyncio
888 import functools
889 import os
890 import signal
891
892 def ask_exit(signame):
893 print("got signal %s: exit" % signame)
894 loop.stop()
895
896 loop = asyncio.get_event_loop()
897 for signame in ('SIGINT', 'SIGTERM'):
898 loop.add_signal_handler(getattr(signal, signame),
899 functools.partial(ask_exit, signame))
900
Serhiy Storchaka0424eaf2015-09-12 17:45:25 +0300901 print("Event loop running forever, press Ctrl+C to interrupt.")
Victor Stinner8b863482014-01-27 10:07:50 +0100902 print("pid %s: send SIGINT or SIGTERM to exit." % os.getpid())
Victor Stinner63b21a82014-07-05 15:38:59 +0200903 try:
904 loop.run_forever()
905 finally:
906 loop.close()
Victor Stinner2cef3002014-10-23 22:38:46 +0200907
908This example only works on UNIX.