Skip to content

How do I use an async function? #1153

Answered by Sharp-Eyes
Xalwer asked this question in Q&A
Discussion options

You must be logged in to vote

I personally find something like this a fairly elegant solution:

event: asyncio.Future[None] = asyncio.Future()

def after(exc: Exception | None):
    event.set_exception(exc) if exc else event.set_result(None)

vc.play(src, after=after)
await event  # wait until the player is done

await vc.disconnect()

The trick is to use something you can synchronously set a flag on and asynchronously wait for that flag to be set, which is something asyncio.Future is particularly well-suited for.

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@Xalwer
Comment options

@Sharp-Eyes
Comment options

Answer selected by Xalwer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants