summaryrefslogtreecommitdiffstatshomepage
path: root/tests/basics/async_for.py
blob: 6b4e136d593d493b005712417e493fe7259c8024 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# test basic async for execution
# example taken from PEP0492

class AsyncIteratorWrapper:
    def __init__(self, obj):
        print('init')
        self._it = iter(obj)

    async def __aiter__(self):
        print('aiter')
        return self

    async def __anext__(self):
        print('anext')
        try:
            value = next(self._it)
        except StopIteration:
            raise StopAsyncIteration
        return value

async def coro():
    async for letter in AsyncIteratorWrapper('abc'):
        print(letter)

o = coro()
try:
    o.send(None)
except StopIteration:
    print('finished')