This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: asyncio: call_soon() documentation unclear on timing
Type: Stage:
Components: asyncio, Documentation Versions: Python 3.4
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: docs@python Nosy List: Corbin.Simpson, docs@python, gvanrossum, martin.panter, r.david.murray, vstinner, yselivanov
Priority: normal Keywords: patch

Created on 2014-11-14 20:01 by Corbin.Simpson, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
call_soon.patch martin.panter, 2014-12-13 06:53 review
Messages (8)
msg231180 - (view) Author: Corbin Simpson (Corbin.Simpson) Date: 2014-11-14 20:01
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.BaseEventLoop.call_soon
[2]: http://twistedmatrix.com/documents/current/core/howto/time.html
[3]: http://twistedmatrix.com/documents/current/api/twisted.internet.interfaces.IReactorTime.html#callLater
msg231182 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2014-11-14 20:03
"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!)
msg231183 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2014-11-14 20:08
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.
msg232597 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2014-12-13 06:53
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.
msg232620 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2014-12-13 20:50
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>
> _______________________________________
>
msg232669 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2014-12-15 16:52
Thanks Martin for your change, I commited it.
msg232670 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2014-12-15 16:52
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.
msg232672 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2014-12-15 16:56
FYI I added a new test to the aiotest project (test suite for asyncio), for this issue:
https://bitbucket.org/haypo/aiotest/commits/17dd11fc2f4bafad623f940b1a33a3b8b0d39ccd
History
Date User Action Args
2022-04-11 14:58:10adminsetgithub: 67064
2014-12-15 16:56:13vstinnersetmessages: + msg232672
2014-12-15 16:52:44vstinnersetmessages: + msg232670
2014-12-15 16:52:12vstinnersetstatus: open -> closed
resolution: fixed
messages: + msg232669
2014-12-13 20:50:53gvanrossumsetmessages: + msg232620
2014-12-13 06:53:02martin.pantersetfiles: + call_soon.patch
versions: + Python 3.4
nosy: + martin.panter

messages: + msg232597

keywords: + patch
2014-11-14 20:08:49r.david.murraysetnosy: + r.david.murray
messages: + msg231183
2014-11-14 20:03:51gvanrossumsetmessages: + msg231182
2014-11-14 20:01:44Corbin.Simpsoncreate