Josh Rosenberg <shadowranger+pyt...@gmail.com> added the comment:

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?

----------
nosy: +josh.r

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue43119>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to