I think the main point here is that, yes, it is known that `await
asyncio.sleep(0)` yields control allowing other tasks to run.

But imagine you have a for loop with 1000 iterations, and which normally
completes in 1 second.  Which means that each iteration takes 1ms to
complete (maybe it does some simple calculation).

Now add `await asyncio.sleep(0)` to the for loop body.  You will notice
that the same loop will take much longer to complete, even if there are no
other tasks running, because of the added overhead of switching context to
the main event loop 1000 times.

So there is no simple alternative here:

1. If you don't yield in the for loop body, then you are blocking the main
loop for 1 second;

2. If you yield in every iteration, you solved the task switch latency
problem, but you make the entire program run much slower.

I don't know for sure if asyncio can solve this problem, but I at least am
willing to admit this is a real problem.

I've been through it myself, many times.  It's a tough problem to solve,
actually.  Requires lots of monitoring in production, to find what are the
tasks that are causing these latency problems.


On Fri, 14 Jun 2019 at 15:23, Andrew Svetlov <andrew.svet...@gmail.com>
wrote:

> We need either both `asyncio.switch()` and `time.switch()`
> (`threading.switch()` maybe) or none of them.
>
> https://docs.python.org/3/library/asyncio-task.html#sleeping has the
> explicit sentence:
>
> sleep() always suspends the current task, allowing other tasks to run.
>
>
> On Fri, Jun 14, 2019 at 5:06 PM Joao S. O. Bueno <jsbu...@python.org.br>
> wrote:
> >
> > > it is very well known feature.
> >
> > Or is it? Just because you do know it, it does not mean it is universal -
> > This is not documented on time.sleep, threading.Thread, or asyncio.sleep
> anyway.
> >
> > I've never worked much on explicitly multi-threaded code, but in 15+
> years
> > this is a pattern I had not seem up to today.
> > The original writer of this Thread, Nikita, also does not seem
> > to have found `asyncio.sleep(0)` in less than 10 minutes looking
> > for what he needed.
> >
> > It allows for "explcit is better than implicit", and really
> > asserts the intention of the code writer, and have
> > a low cost to implement in the stdlib.
> >
> >
> >
> > And it is semantically more correct at the cost of maybe 3loc on the
> stdlib.
> >
> > On Fri, 14 Jun 2019 at 10:57, Andrew Svetlov <andrew.svet...@gmail.com>
> wrote:
> >>
> >> time.sleep(0) is used for a thread context switch, it is very well
> >> known feature.
> >> await asyncio.sleep(0) does the same for async tasks.
> >> Why do we need another API?
> >>
> >> On Fri, Jun 14, 2019 at 4:43 PM Joao S. O. Bueno <jsbu...@python.org.br>
> wrote:
> >> >
> >> > Regardless of a mechanism to counting time, and etc...
> >> >
> >> > Maybe a plain and simple adition to asincio would be a
> >> > context-switching call that does what `asyncio.sleep(0)` does today?
> >> >
> >> > It would  feel better to write something like
> >> > `await asyncio.switch()`  than an arbitrary `sleep`.
> >> >
> >> > On Fri, 14 Jun 2019 at 10:27, Nikita Melentev <
> multisosnoo...@gmail.com> wrote:
> >> >>
> >> >> Fortunately, asyncio provide this good universal default: 100ms,
> when WARING appears. Nested loops can be solved with context manager, which
> will share `last_context_switch_time` between loops. But main thing here is
> that this is strictly optional, and when someone will use this thing he
> will know what it is and why he need this.
> >> >> _______________________________________________
> >> >> Python-ideas mailing list -- python-ideas@python.org
> >> >> To unsubscribe send an email to python-ideas-le...@python.org
> >> >> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> >> >> Message archived at
> https://mail.python.org/archives/list/python-ideas@python.org/message/UT5LEWS4XX5IJ64AMMYKJVG2BNYZXYQY/
> >> >> Code of Conduct: http://python.org/psf/codeofconduct/
> >> >
> >> > _______________________________________________
> >> > Python-ideas mailing list -- python-ideas@python.org
> >> > To unsubscribe send an email to python-ideas-le...@python.org
> >> > https://mail.python.org/mailman3/lists/python-ideas.python.org/
> >> > Message archived at
> https://mail.python.org/archives/list/python-ideas@python.org/message/4ZTLXBAQPFYQNITNB3RLRCW5D2QXKGBI/
> >> > Code of Conduct: http://python.org/psf/codeofconduct/
> >>
> >>
> >>
> >> --
> >> Thanks,
> >> Andrew Svetlov
>
>
>
> --
> Thanks,
> Andrew Svetlov
> _______________________________________________
> Python-ideas mailing list -- python-ideas@python.org
> To unsubscribe send an email to python-ideas-le...@python.org
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at
> https://mail.python.org/archives/list/python-ideas@python.org/message/DZCOVAIV7AJMZK6UGWQOW7UHPW527YUQ/
> Code of Conduct: http://python.org/psf/codeofconduct/
>


-- 
Gustavo J. A. M. Carneiro
Gambit Research
"The universe is always one step beyond logic." -- Frank Herbert
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/XTRPXCYILRQWPX6G3NQXOEWD74UQOTEO/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to