peterhinch / micropython-async

Application of uasyncio to hardware interfaces. Tutorial and code.
MIT License
726 stars 166 forks source link

TypeError: 'WaitAny' object isn't iterable #92

Closed obdevel closed 1 year ago

obdevel commented 1 year ago

Trying to wait on multiple events, which are set by the two coroutines that are created. These coroutines work fine if created elsewhere, and I can wait on their events individually. I'm working from the example code in TUTORIAL.md.

    ...
    from events import WaitAny
    ...

    async def sequence_test_coro(self):
        evp = asyncio.Event()
        evs = asyncio.Event()

        tp = asyncio.create_task(self.pubsub_test_coro(evp))
        ts = asyncio.create_task(self.sensor_test_coro(evs))

        while True:
            evt = await WaitAny((evp, evs))

            if evt is evp:
                self.logger.log("pubsub is active")
                evp.clear()
            else:
                self.logger.log("sensor is active")
                evs.clear()

Error output is:

Task exception wasn't retrieved future: coro= <generator object 'sequence_test_coro' at 2000a460> Traceback (most recent call last): File "uasyncio/core.py", line 1, in run_until_complete File "module_asyncio.py", line 231, in sequence_test_coro TypeError: 'WaitAny' object isn't iterable

obdevel commented 1 year ago

Solved.

The example given in TUTORIAL.md is incorrect. The correct usage is shown in V3/doc/EVENTS.md. e.g.

evt = await WaitAny((evp, evs))  # IS INCORRECT
evt = await WaitAny((evp, evs)).wait()  # IS CORRECT
peterhinch commented 1 year ago

Apologies, you are right. Tutorial now fixed.