Small tweaks and clarifications.

This commit is contained in:
Guido van Rossum 2012-12-19 19:35:42 -08:00
parent 8edbfcfdf1
commit f1881e8a43
1 changed files with 38 additions and 37 deletions

View File

@ -148,66 +148,69 @@ loop per thread.
Event Loop Interface Event Loop Interface
-------------------- --------------------
A conforming event loop object has the following methods: (A note about times: as usual in Python, all timeouts, intervals and
delays are measured in seconds, and may be ints or floats. The
accuracy and precision of the clock are up to the implementation; the
default implementation uses ``time.monotonic()``.)
.. A conforming event loop object has the following methods:
Look for a better way to format method docs. PEP 12 doesn't seem to
have one. PEP 418 uses ^^^, which makes sub-headings. PEP 3148
uses a markup which generates rather heavy layout using blockquote,
causing a blank line between each method heading and its
description. Also think of adding subheadings for different
categories of methods.
- ``run()``. Runs the event loop until there is nothing left to do. - ``run()``. Runs the event loop until there is nothing left to do.
This means, in particular: This means, in particular:
- No more calls scheduled with ``call_later()`` (except for canceled - No more calls scheduled with ``call_later()``,
calls). ``call_repeatedly()``, ``call_soon()``, or
``call_soon_threadsafe()``, except for canceled calls.
- No more registered file descriptors. It is up to the registering - No more registered file descriptors. It is up to the registering
party to unregister a file descriptor when it is closed. party to unregister a file descriptor when it is closed.
Note: run() blocks until the termination condition is met. Note: ``run()`` blocks until the termination condition is met,
or until ``stop()`` is called.
Note: if you schedule a call with ``call_repeatedly()``, ``run()``
will not exit until you cancel it.
- ``stop()``. Stops the event loop as soon as it is convenient. It - ``stop()``. Stops the event loop as soon as it is convenient. It
is fine to restart the loop with ``run()`` (or one of its variants) is fine to restart the loop with ``run()`` (or one of its variants)
subsequently. (TBD: How should this interact with ``run_once()`` subsequently.
and ``run_until_complete()``?)
Note: How soon exactly is up to the implementation. It is Note: How soon exactly is up to the implementation. All immediate
reasonable to implement this so that ``run()`` simply stops calling callbacks that were already scheduled to run before ``stop()`` is
``run_once()`` when this is called. called must still be run, but callbacks scheduled after it is called
(or scheduled to be run later) will not be run.
- ``run_until_complete(future, timeout=None)``. Runs the event loop - ``run_until_complete(future, timeout=None)``. Runs the event loop
until a Future is done. If a timeout is given, it waits at most until the Future is done. If a timeout is given, it waits at most
that long. If the Future is done, its result is returned, or its that long. If the Future is done, its result is returned, or its
exception is raised; if the timeout expires before the Future is exception is raised; if the timeout expires before the Future is
done, TimeoutError is raised (but the Future is not cancelled). done, or if ``stop()`` is called, ``TimeoutError`` is raised (but
This cannot be called when the event loop is already running. the Future is not cancelled). This cannot be called when the event
loop is already running.
Note: This API is most useful for tests and the like. It should not Note: This API is most useful for tests and the like. It should not
be used as a substitute for ``yield from future`` or other ways to be used as a substitute for ``yield from future`` or other ways to
wait for a Future (e.g. registering a done callback). wait for a Future (e.g. registering a done callback).
- ``run_once(timeout=None)``. Run the event loop for a little while. - ``run_once(timeout=None)``. Run the event loop for a little while.
If a timeout is given, an I/O poll will be given at most that timeout; If a timeout is given, an I/O poll made will block at most that
otherwise, an I/O poll is not constrained in time. long; otherwise, an I/O poll is not constrained in time.
Note: Exactlly what this does is up to the implementation. One Note: Exactlly how much work this does is up to the implementation.
constraint: if a callback immediately schedules itself using One constraint: if a callback immediately schedules itself using
``call_soon()``, ``run_once()`` should still return. ``call_soon()``, causing an infinite loop, ``run_once()`` should
still return.
- ``call_later(delay, callback, *args)``. Arrange for - ``call_later(delay, callback, *args)``. Arrange for
``callback(*args)`` to be called approximately ``delay`` seconds in ``callback(*args)`` to be called approximately ``delay`` seconds in
the future, once, unless canceled. As usual in Python, ``delay`` may the future, once, unless canceled. Returns
be a floating point number to represent smaller intervals. Returns
a ``Handler`` object representing the callback, whose a ``Handler`` object representing the callback, whose
``cancel()`` method can be used to cancel the callback. ``cancel()`` method can be used to cancel the callback.
- ``call_repeatedly(interval, callback, **args)``. Like ``call_later()`` - ``call_repeatedly(interval, callback, **args)``. Like ``call_later()``
but calls the callback repeatedly, every ``interval`` seconds, but calls the callback repeatedly, every ``interval`` seconds,
until the ``Handler`` returned is cancelled. The first call is in until the ``Handler`` returned is cancelled. The first call is in
``interval`` seconds. May be a float. ``interval`` seconds.
- ``call_soon(callback, *args)``. Equivalent to ``call_later(0, - ``call_soon(callback, *args)``. Equivalent to ``call_later(0,
callback, *args)``. callback, *args)``.
@ -225,8 +228,6 @@ A conforming event loop object has the following methods:
``isinstance(callback, Handler)``? It should silently skip ``isinstance(callback, Handler)``? It should silently skip
a canceled callback. a canceled callback.
- TBD: Repeatable timers.
Some methods in the standard conforming interface return Futures: Some methods in the standard conforming interface return Futures:
- ``wrap_future(future)``. This takes a PEP 3148 Future (i.e., an - ``wrap_future(future)``. This takes a PEP 3148 Future (i.e., an
@ -336,10 +337,8 @@ i.e. no disk files.
TBD: What about multiple callbacks per fd? The current semantics is TBD: What about multiple callbacks per fd? The current semantics is
that ``add_reader()/add_writer()`` replace a previously registered that ``add_reader()/add_writer()`` replace a previously registered
callback. callback. Change this to raise an exception if a callback is already
registered.
TBD: Should ``remove_*()`` return a bool telling is whether it removed
anything?
The following methods for doing async I/O on sockets are optional. The following methods for doing async I/O on sockets are optional.
They are alternative to the previous set of optional methods, intended They are alternative to the previous set of optional methods, intended
@ -353,7 +352,8 @@ supports it). The socket argument has to be a non-blocking socket.
- ``sock_sendall(sock, data)``. Send bytes ``data`` to the socket - ``sock_sendall(sock, data)``. Send bytes ``data`` to the socket
``sock``. Returns a Future whose result on success will be ``sock``. Returns a Future whose result on success will be
``None``. (TBD: Is it better to emulate ``sendall()`` or ``send()`` ``None``. (TBD: Is it better to emulate ``sendall()`` or ``send()``
semantics?) semantics? I think ``sendall()`` -- but perhaps it should still
be *named* ``send()``?)
- ``sock_connect(sock, address)``. Connect to the given address. - ``sock_connect(sock, address)``. Connect to the given address.
Returns a Future whose result on success will be ``None``. Returns a Future whose result on success will be ``None``.
@ -605,9 +605,10 @@ Transports have the following public methods:
TBD: Provide flow control the other way -- the transport may need to TBD: Provide flow control the other way -- the transport may need to
suspend the protocol if the amount of data buffered becomes a burden. suspend the protocol if the amount of data buffered becomes a burden.
One option: let the transport call ``protocol.pause()`` and Proposal: let the transport call ``protocol.pause()`` and
``protocol.resume()`` if they exist; if they don't exist, the protocol ``protocol.resume()`` if they exist; if they don't exist, the
doesn't support flow control. protocol doesn't support flow control. (Perhaps different names
to avoid confusion between protocols and transports?)
Protocols Protocols
--------- ---------