Skip to content Skip to sidebar Skip to footer

Is There Any Way To Cancel Asynchronous Functions Automatically After A Specified Time?

I am trying to use discord.py to create a Discord bot. One issue I have run into is that I want an asynchronous function to be run, but after a specified amount of time (e.g. 5 sec

Solution 1:

If you only need to run one coroutine, use wait_for instead.

await asyncio.wait_for(testfunc(), timeout=5)

Note that this will throw an asyncio.TimeoutError that you will need to catch

Looking at the docs for wait it states:

Unlike wait_for(), wait() does not cancel the futures when a timeout occurs.

Post a Comment for "Is There Any Way To Cancel Asynchronous Functions Automatically After A Specified Time?"