Skip to content Skip to sidebar Skip to footer

Thread And Asyncio: Task Was Destroyed But It Is Pending

I have a thread that runs an asyncio loop. I start a future task that does things which are irrelevant here. When I stop the thread, I stop the asyncio loop as well. However, I can

Solution 1:

You should not only call cancel() on task, but also await its cancellation instead of just stopping loop as you do.

from contextlib import suppress
from threading import Thread
from time import sleep
import asyncio


class Hardware(Thread):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.loop = None
        self._poll_task = None

    def run(self):
        self.loop = asyncio.new_event_loop()
        loop = self.loop
        asyncio.set_event_loop(loop)
        try:
            # create task:
            self._poll_task = asyncio.ensure_future(self._poll())

            # run loop:
            loop.run_forever()
            loop.run_until_complete(loop.shutdown_asyncgens())

            # cancel task:
            self._poll_task.cancel()
            with suppress(asyncio.CancelledError):
                loop.run_until_complete(self._poll_task)
        finally:
            loop.close()

    def stop(self):
        self.loop.call_soon_threadsafe(self.loop.stop)

    async def _poll(self):
        while True:  # you don't need to create new task each time
            print('ook')
            await asyncio.sleep(1.0)


hw = Hardware()
try:
    hw.start()
    while True:
        sleep(.1)
except KeyboardInterrupt:
    hw.stop()
    hw.join()

Post a Comment for "Thread And Asyncio: Task Was Destroyed But It Is Pending"