Python Forum
Thread Rating:
  • 2 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Async for loop
#5
You can replace this code:
class Aiter :
    def __init__(self, iterable):
        self.iter_ = iter(iterable)
 
    async def __aiter__(self):
        return self
 
    async def __anext__(self):
        await asyncio .sleep(0)
        try:
            object = next(self.iter_)
        except StopIteration:
            raise StopAsyncIteration # :-) PEP492 - "To stop iteration __anext__ must raise a StopAsyncIteration exception"
 
        return object
With this code:
async def aiter(msg):
    for char in msg:
        yield char
You can't use yield from in async function, but you can use yield to use async generator, which could be used with async for.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
Async for loop - by wavic - Sep-09-2017, 02:38 PM
RE: Async for loop - by nilamo - Sep-10-2017, 05:03 PM
RE: Async for loop - by micseydel - Sep-10-2017, 08:00 PM
RE: Async for loop - by programmerguy - Dec-08-2019, 06:03 PM
RE: Async for loop - by DeaD_EyE - Dec-08-2019, 09:30 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Async for making requests DangDuong 3 1,097 Mar-04-2024, 03:50 AM
Last Post: sarahlly
  async for wavic 7 6,266 Jun-07-2017, 06:07 AM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020