Author josh.r
Recipients asvetlov, josh.r, kj, spenczar, yselivanov
Date 2021-02-05.04:46:45
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1612500407.16.0.769286007772.issue43119@roundup.psfhosted.org>
In-reply-to
Content
Making literally every await equivalent to:

await asyncio.sleep(0)

followed by the actual await (which is effectively what you're proposing when you expect all await to be preemptible) means adding non-trivial overhead to all async operations (asyncio is based on system calls of the select/poll/epoll/kpoll variety, which add meaningful overhead when we're talking about an operation that is otherwise equivalent to an extremely cheap simple collections.deque.append call). It also breaks many reasonable uses of asyncio.wait and asyncio.as_completed, where the caller can reasonably expect to be able to await the known-complete tasks without being preempted (if you know the coroutine is actually done, it could be quite surprising/problematic when you await it and get preempted, potentially requiring synchronization that wouldn't be necessary otherwise).

Making all await yield to the event loop would be like releasing the GIL before acquiring an uncontended lock; it makes an extremely cheap operation *much* higher overhead to, at best, fix a problem with poorly designed code. In real life, if whatever you're feeding the queue with is infinite and requires no awaiting to produce each value, you should probably just avoid the queue and have the consumer consume the iterable directly. Or just apply a maximum size to the queue; since the source of data to put is infinite and not-awaitable, there's no benefit to an unbounded queue, you may as well use a bound roughly fitted to the number of consumers, because any further items are just wasting memory well ahead of when it's needed.

Point is, regular queue puts only block (and potentially release the GIL early) when they're full or, as a necessary consequence of threading being less predictable than asyncio, when there is contention on the lock protecting the queue internals (which is usually resolved quickly); why would asyncio queues go out of their way to block when they don't need to?
History
Date User Action Args
2021-02-05 04:46:47josh.rsetrecipients: + josh.r, asvetlov, yselivanov, kj, spenczar
2021-02-05 04:46:47josh.rsetmessageid: <1612500407.16.0.769286007772.issue43119@roundup.psfhosted.org>
2021-02-05 04:46:47josh.rlinkissue43119 messages
2021-02-05 04:46:45josh.rcreate