Adding Context Manger to threading.Timer
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 77.2k
- Fork
- 35.9k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
Feature or enhancement
Proposal:
Proposing to add context management to threading.Timer.
I had an issue where I didn’t want to ask the user to be patient unless they had already been waiting a while. Timer works nicely for this:
t = Timer(1, print, ("Trust me, I'm on it...",))
t.start()
time.sleep(2)
t.cancel()
Enclosing calls of start and cancel call out for context management for the obvious reasons. Sub-classing and adding it was trivial:
class Timer(Timer):
def __enter__(self):
self.start()
def __exit__(self, exc_type, exc_val, exc_tb):
self.cancel()
The first example now looks like this:
with Timer(1, print, ("Trust me, I'm on it...",)):
time.sleep(2)
Users of the threading module will probably already be familiar with the context management features of a lock. Chaining the two together is a great way to provide feedback IF it’s needed, gotta reduce that alarm fatigue!
hold_message = Timer(10, print, ("Please hold, your call is important to us...",))
with some_lock, hold_message:
...
Looking to exact those changes on the core library
Has this already been discussed elsewhere?
I have already discussed this feature proposal on Discourse
Links to previous discussion of this feature:
https://discuss.python.org/t/adding-context-manger-to-threading-timer/64083
Linked PRs
- gh-125501
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia dal punto di ingresso threading.Timer e rivedi la discussione collegata su Discourse e la PR gh-125501. Il lavoro è completato quando Timer supporta l’uso proposto per la gestione del contesto, avviandosi all’ingresso e annullandosi all’uscita, con il comportamento coperto come appropriato dai test threading esistenti.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- operating-systems
- Tipo di issue
- Funzionalità
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 25/100