Public `JobContext.create_task()` for background tasks that end with the job

Aperta Adatta ai principianti
#8,366 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

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

Direzione di ricerca

Parti dal contesto del job e dal suo metodo privato _track_pending_task(), oltre che dal percorso di shutdown che annulla i task tracciati dopo i callback di shutdown. Leggi come delete_room e add_sip_participant usano questo tracciamento. Il lavoro è completo quando esiste un create_task() pubblico che mantiene un riferimento, registra i fallimenti e viene annullato allo shutdown, con test per l'ordine di cancellazione e il logging degli errori.

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

Descrizione

Feature Type: Would make my life easier

What we're trying to do

Our voice agent starts many small background tasks during a call. For example: a silence
watchdog, a loop that saves call quality stats, and a prefetch of some data.

When the job ends, these tasks should stop too. If a task fails, we want to see the error
in the logs.

What we do today

The framework already does this for its own tasks (delete_room, add_sip_participant).
It uses JobContext._track_pending_task(). That method is private, so we copy the idea in
our own code:

bg_tasks: set[asyncio.Task] = set()

def track_task(coro):
    task = asyncio.ensure_future(coro)
    bg_tasks.add(task)
    task.add_done_callback(bg_tasks.discard)
    return task

# later, in our shutdown code
for task in bg_tasks:
    task.cancel()

We have this in two entrypoints. It works, but every user has to write it, and it is easy
to get wrong. Ours does not log errors, for example.

The small API we'd like
task = ctx.create_task(watch_silence(), name="silence_watchdog")

It would just call asyncio.create_task() and then self._track_pending_task(). So the job:

  • keeps a reference to the task, so it is not garbage collected,
  • logs the error if the task fails,
  • cancels the task if it is still running when the job ends.

It returns the normal asyncio.Task, so you can still await or cancel it yourself.

The job already cancels these tasks at the end of shutdown, after the shutdown callbacks.
This new method uses that same step. Nothing about shutdown changes.

utils.aio.TaskSet is close, but it is not tied to the job, so nothing cancels its tasks
at the end.

Question

Would you accept a PR for this? I have a small change ready: the method with a
docstring, plus two tests (the task is cancelled after the shutdown callbacks, and a failing
task is logged). Happy to change the name or put it somewhere else if you prefer.

Lingua principale
Python
Stelle
14.6k
Fork
3.9k
Merge medio
2g 23h
PR unite (30g)
263

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

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 livekit/agents

Tutte le issue di livekit/agents

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.