Asynchronous calls from an addon

I want to make calls to multiple LLM APIs and process the responses as soon as they are returned.

I’ve tried various methods including the aqt.operations method designed in the documentation.

Whatever I do, the addon displays the spinning circle until the slowest API call is complete. It then updates the UI with all of the data. How can I update the UI as the calls are returned? Thanks.

Are you trying to update the UI in background thread? The following works just fine for me:

def api_request():
    # Any operation that do not interact with the UI
    response = request()
    response = handler(response)
    return response

def on_success(response):
    # Update UI here
    aqt.utils.showInfo("Response: " + str(response))

QueryOp(
    parent=parent_window,
    op=lambda _: api_request(),
    success=on_success,
).run_in_background()

You can also use taskman to directly acess threads:

aqt.mw.taskman.run_in_background(task=api_request, on_done=process_response)
aqt.mw.taskman.run_in_background(
    response = api_request()
    aqt.mw.taskman.run_in_main_thread(
        process_response(response)
    )
)
1 Like

Do you not see a spinning line until the longest running task completes using that method? I’ve tried it and the debugging statements indicate that the UI is being updated, but that’s not visible because the spinning line is shown until the last background task completes.

Please see the note about without_collection() I’ve added to Background Operations - Writing Anki Add-ons

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.