Please explain “Task was destroyed but it is pending!”

前端 未结 4 760
难免孤独
难免孤独 2021-02-06 22:54

Python 3.4.2

I am learning asyncio and I use it to continously listen IPC bus, while gbulb listens to the dbus.

Some side notes:

So I created a fun

4条回答
  •  不知归路
    2021-02-06 23:08

    The reasons this happens is as explained by @Yeray Diaz Diaz In my case, I wanted to cancel all the tasks that were not done after the first finished, so I ended up cancelling the extra jobs, then using loop._run_once() to run the loop a bit more and allow them to stop:

        loop = asyncio.get_event_loop()
        job = asyncio.wait(tasks, return_when=asyncio.FIRST_COMPLETED)
        tasks_finished,tasks_pending, = loop.run_until_complete(job)
        tasks_done = [t for t in tasks_finished if t.exception() is None]
        if tasks_done == 0:
            raise Exception("Failed for all tasks.")
        assert len(tasks_done) == 1
        data = tasks_done[0].result()
        for t in tasks_pending:
            t.cancel()
            t.cancel()
        while not all([t.done() for t in tasks_pending]):
            loop._run_once()
    

提交回复
热议问题