[issue22875] asyncio: call_soon() documentation unclear on timing
New submission from Corbin Simpson: Hi there! While assisting somebody on #python (where manners go to die), I was consulting asyncio's documentation. Given my unfamiliarity with asyncio, I was surprised to read BaseEventLoop.call_soon()'s documented behavior: "Arrange for a callback to be called as soon as possible." [1] The question in mind is whether call_soon() waits for control to return to the event loop before calling the first callback in the queue. After all, given the documented behavior, it would not be unreasonable for call_soon() to always call callbacks immediately if no other callbacks were enqueued. I can imagine scenarios where a previous queued callback from e.g. call_soon_threadsafe() creates a callback that causes queueing behavior, but otherwise I would expect "as soon as possible" to mean "immediately". It's true that in other event loops, this kind of callback registration waits for control to return to the event loop before running callbacks. However, many event loops that I've worked with indicate that call_later() is the correct API for adding callbacks that respect the event loop. In fact, call_later(0, callback, args) looks quite a bit like the familiar Twisted callLater(0, callback, args) pattern! Additionally, there is no indication as to whether call_later() and call_soon() use the same queue. I'd like clarification on these points in the Python 3 documentation, please. I'd like to know whether call_soon() can be immediate, and whether call_soon() preempts or interleaves with call_later(). (Yes, I know that Twisted's documentation is missing this too [2] [3]. I'll get to that soon enough.) Thanks! ~ C. [1]: https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.BaseEventLo... [2]: http://twistedmatrix.com/documents/current/core/howto/time.html [3]: http://twistedmatrix.com/documents/current/api/twisted.internet.interfaces.I... ---------- assignee: docs@python components: Documentation, asyncio messages: 231180 nosy: Corbin.Simpson, docs@python, gvanrossum, haypo, yselivanov priority: normal severity: normal status: open title: asyncio: call_soon() documentation unclear on timing _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue22875> _______________________________________
Guido van Rossum added the comment: "As soon as possible" is constrained by "all callbacks are called from the event loop". Feel free to suggest a doc patch (asyncio docs are in dire need of more help!) ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue22875> _______________________________________
R. David Murray added the comment: Personally, when I read that I thought "arrange for" made it pretty clear it was when control returned to the event loop, but I agree that if it can be made clearer it should be. ---------- nosy: +r.david.murray _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue22875> _______________________________________
Martin Panter added the comment: I have been bitten by this when attempting to implement my own event loops. Parts of the “asyncio” code itself expects that the callback is not invoked directly after call_soon() returns. Here is a simple patch. ---------- keywords: +patch nosy: +vadmium versions: +Python 3.4 Added file: http://bugs.python.org/file37437/call_soon.patch _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue22875> _______________________________________
Guido van Rossum added the comment: Docfix LGTM. On Fri, Dec 12, 2014 at 10:53 PM, Martin Panter <report@bugs.python.org> wrote:
Martin Panter added the comment:
I have been bitten by this when attempting to implement my own event loops. Parts of the “asyncio” code itself expects that the callback is not invoked directly after call_soon() returns. Here is a simple patch.
---------- keywords: +patch nosy: +vadmium versions: +Python 3.4 Added file: http://bugs.python.org/file37437/call_soon.patch
_______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue22875> _______________________________________
---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue22875> _______________________________________
STINNER Victor added the comment: Thanks Martin for your change, I commited it. ---------- resolution: -> fixed status: open -> closed _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue22875> _______________________________________
STINNER Victor added the comment: Oops, I forgot to mention the issue number in my commit. changeset: 93893:a59fed8c710b branch: 3.4 parent: 93891:1da9e9eaeae8 user: Victor Stinner <victor.stinner@gmail.com> date: Mon Dec 15 17:50:55 2014 +0100 files: Doc/library/asyncio-eventloop.rst description: asyncio doc: call_soon() does not call immediatly the callback. Patch written by Martin Panter. ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue22875> _______________________________________
STINNER Victor added the comment: FYI I added a new test to the aiotest project (test suite for asyncio), for this issue: https://bitbucket.org/haypo/aiotest/commits/17dd11fc2f4bafad623f940b1a33a3b8... ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue22875> _______________________________________
participants (5)
-
Corbin Simpson
-
Guido van Rossum
-
Martin Panter
-
R. David Murray
-
STINNER Victor