Command palette waits for a 0.1 s timeout after the last provider finishes before showing results

Aperta
#6,781 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
35/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python
Ambito
cli

Direzione di ricerca

Start with CommandPalette._search_for, especially its wait on commands.get() and how provider tasks finish; the issue includes a reproduction script using InstantProvider and TimedPalette. Verify that the list refreshes promptly after the final provider finishes, without the roughly 0.1-second delay.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Hi! I hit this while writing generated tests against Textual's widgets.

CommandPalette._search_for loops while any provider's task is running, waiting on wait_for(commands.get(), 0.1). When a provider finishes, it puts nothing on the queue, so if the loop is already waiting it sleeps until the timeout before it notices. The list is filled at the end of the search, so with a provider that does any async work after its last hit, the results appear about 0.1 s late on every keystroke.

import asyncio
from time import perf_counter

import textual
from textual.app import App
from textual.command import CommandPalette, Hit, Hits, Provider

provider_done: list[float] = []
list_filled: list[float] = []

class InstantProvider(Provider):
    async def search(self, query: str) -> Hits:
        yield Hit(1.0, f"Result for {query!r}", lambda: None)
        await asyncio.sleep(0.01)  # e.g. checking a source for more results
        provider_done.append(perf_counter())

class TimedPalette(CommandPalette):
    def _refresh_command_list(self, command_list, commands, clear_current) -> None:
        super()._refresh_command_list(command_list, commands, clear_current)
        list_filled.append(perf_counter())

class PaletteApp(App):
    COMMANDS = {InstantProvider}

async def main() -> None:
    app = PaletteApp()
    async with app.run_test() as pilot:
        app.push_screen(TimedPalette())
        await pilot.pause()
        for key in "abcde":
            provider_done.clear()
            list_filled.clear()
            await pilot.press(key)
            await pilot.pause(0.3)
            delay = list_filled[-1] - provider_done[-1]
            print(f"query {key!r}: list filled {delay * 1000:.0f} ms after the provider finished")
            await pilot.press("backspace")
            await pilot.pause(0.3)
    print("Textual", textual.__version__)

asyncio.run(main())

Output on Textual 8.2.7:

query 'a': list filled 92 ms after the provider finished
query 'b': list filled 93 ms after the provider finished
query 'c': list filled 92 ms after the provider finished
query 'd': list filled 91 ms after the provider finished
query 'e': list filled 92 ms after the provider finished

Main at 5e5b7ef gives 91 to 92 ms. (TimedPalette only records when the list is refreshed.)

A provider that finishes in the same step as its last put doesn't see the delay, which is probably why it isn't obvious with the built-in providers. I expected the list to fill as soon as the last provider is done. A small fix is to wake the loop when a provider's task finishes, using a done callback that puts a None marker on the queue. With that, the same script gives 2 to 3 ms. I have a PR with a test ready.

Lingua principale
Python
Stelle
37.7k
Fork
1.5k
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di Textualize/textual

Tutte le issue di Textualize/textual

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.