
Hello everyone, I'm quite new working with asyncio and thus maybe missing some things about it, but wouldn't it be quite easier to have some iterables to support async for loops "natively", since asyncio is now part of the Stdlib? I've tried to work with asyncio while using discord.py, and has some struggle with an "async for" loop on a dictionary, so I had to implement a new dict subclass that would just reimplement items(), keys() and values() functions. I think that it would be a cool improvement to implement some of those in some standard way. There's some code I wrote on a CodeReview thread but I still haven't got any feedback on it. Here's the link of the thread : https://codereview.stackexchange.com/questions/197551/asynchronous-dictionar... Regards,

On Mon, Aug 20, 2018 at 12:19 AM, Simon De Greve <degrevesim@gmail.com> wrote:
You can do this, but I don't see what it accomplishes... Are you aware that you can use regular 'for' loops inside 'async def' functions? -n -- Nathaniel J. Smith -- https://vorpus.org

Do you mean that for loops inside an "async def" statements are always executed as 'async for' loops? That's what I wanted to acheive by writing the AsyncDict class (c.f. the CodeReview link). As I said, I'm pretty new to Asyncio and thus may be missing some immportant feature of the lib (which is apparently the case here). Le lun. 20 août 2018 à 09:29, Nathaniel Smith <njs@pobox.com> a écrit :

On Mon, Aug 20, 2018 at 12:34 AM, Simon De Greve <degrevesim@gmail.com> wrote:
The only difference between an 'async for' and a regular 'for' is that the former works on async iterables, and the latter works on regular iterables. So "executed as 'async for'" doesn't really mean anything, I think? If you have an async iterable, use 'async for', and if you have a regular iterable, use 'for'. -n -- Nathaniel J. Smith -- https://vorpus.org

On Mon, 20 Aug 2018 at 04:49, Chris Angelico <rosuav@gmail.com> wrote:
Maybe it is worth to further clarify that iterating on dicts and lists is _not_ blocking at all. That would only be the case if to retrieve its own keys the dictionary would have to perform some I/O access or heavy computation - which, besides been of very little value in practice, would only be possible in a specialized class anyway.

On 8/20/2018 3:19 AM, Simon De Greve wrote:
One purpose of asynchronous programming is to pause a task that is waiting for input from an external system, so as to not waste CPU time. As other noted, this is not an issue with iterating through in-memory collections. It is worth noting that 'async' and 'await' are syntax keywords for working with coroutines (how is underdocumented) and that the asyncio module is just one event-loop system that can drive coroutines. The tkinter module, over 20 years old, can also.
Another purpose of asynchonous programming is to keep a system responsive, for instance, to user input by not letting a compute-bound task tie-up a cpu indefinitely. This issue *can* apply to iterating through sufficiently large collection in, for instance, a tkinter gui program. But pausing iteration after *each* iteration is usually a terrible waste of overhead. So one wants to do some number of iterations at full speed and then pause to allow other events to be handled.
I know how to do the above with tkinter callback loops, but I have not yet worked out how to do so with async def coroutines. -- Terry Jan Reedy

On Mon, Aug 20, 2018 at 12:19 AM, Simon De Greve <degrevesim@gmail.com> wrote:
You can do this, but I don't see what it accomplishes... Are you aware that you can use regular 'for' loops inside 'async def' functions? -n -- Nathaniel J. Smith -- https://vorpus.org

Do you mean that for loops inside an "async def" statements are always executed as 'async for' loops? That's what I wanted to acheive by writing the AsyncDict class (c.f. the CodeReview link). As I said, I'm pretty new to Asyncio and thus may be missing some immportant feature of the lib (which is apparently the case here). Le lun. 20 août 2018 à 09:29, Nathaniel Smith <njs@pobox.com> a écrit :

On Mon, Aug 20, 2018 at 12:34 AM, Simon De Greve <degrevesim@gmail.com> wrote:
The only difference between an 'async for' and a regular 'for' is that the former works on async iterables, and the latter works on regular iterables. So "executed as 'async for'" doesn't really mean anything, I think? If you have an async iterable, use 'async for', and if you have a regular iterable, use 'for'. -n -- Nathaniel J. Smith -- https://vorpus.org

On Mon, 20 Aug 2018 at 04:49, Chris Angelico <rosuav@gmail.com> wrote:
Maybe it is worth to further clarify that iterating on dicts and lists is _not_ blocking at all. That would only be the case if to retrieve its own keys the dictionary would have to perform some I/O access or heavy computation - which, besides been of very little value in practice, would only be possible in a specialized class anyway.

On 8/20/2018 3:19 AM, Simon De Greve wrote:
One purpose of asynchronous programming is to pause a task that is waiting for input from an external system, so as to not waste CPU time. As other noted, this is not an issue with iterating through in-memory collections. It is worth noting that 'async' and 'await' are syntax keywords for working with coroutines (how is underdocumented) and that the asyncio module is just one event-loop system that can drive coroutines. The tkinter module, over 20 years old, can also.
Another purpose of asynchonous programming is to keep a system responsive, for instance, to user input by not letting a compute-bound task tie-up a cpu indefinitely. This issue *can* apply to iterating through sufficiently large collection in, for instance, a tkinter gui program. But pausing iteration after *each* iteration is usually a terrible waste of overhead. So one wants to do some number of iterations at full speed and then pause to allow other events to be handled.
I know how to do the above with tkinter callback loops, but I have not yet worked out how to do so with async def coroutines. -- Terry Jan Reedy
participants (5)
-
Chris Angelico
-
Joao S. O. Bueno
-
Nathaniel Smith
-
Simon De Greve
-
Terry Reedy