Skip to content

Conversation

@abdnh
Copy link
Collaborator

@abdnh abdnh commented Jan 18, 2026

This enables async request handlers for Flask. Useful for the Svelte ports of the editor and reviewer (#4029, #4289). I already use this in my PR, but I thought it'd be nice to extract it into its own PR to share.

To give a usage example from the reviewer port, the PlayAVTags RPC can be implemented like this:

async def play_avtags():
    from anki.frontend_pb2 import PlayAVTagsRequest
    from anki.template import av_tags_to_native
    from aqt import gui_hooks, mw
    from aqt.sound import av_player

    def on_av_player_did_end_playing(*args: Any, **kwargs: Any) -> None:
        def task() -> None:
            if av_player.queue_is_empty():
                request_handler.set_result(None)
                gui_hooks.av_player_did_end_playing.remove(on_av_player_did_end_playing)

        # Allow time for audio queue to update
        mw.taskman.run_on_main(lambda: mw.progress.single_shot(100, task))

    def callback(request_handler: AsyncRequestHandler) -> None:
        gui_hooks.av_player_did_end_playing.append(on_av_player_did_end_playing)

    request_handler: AsyncRequestHandler[None] = AsyncRequestHandler(callback)
    req = PlayAVTagsRequest.FromString(request.data)
    av_player.play_tags(av_tags_to_native(req.tags))
    await request_handler.run()

    return b""

The frontend can then await the request in case "Wait for audio" is enabled.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant