python / python/cpython

Adding Context Manger to threading.Timer

Abierto
#125,500 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

stdlib type-feature
Lenguaje dominante
Python
Estrellas
77.2k
Forks
35.9k
Métricas de merge de PR
Métricas de PR pendientes

Descripción

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

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Comienza con el punto de entrada threading.Timer y revisa la discusión vinculada en Discourse y el PR gh-125501. Se considera terminado cuando Timer admite el uso propuesto de gestión de contexto, iniciándose al entrar y cancelándose al salir, con el comportamiento cubierto según corresponda por las pruebas existentes de threading.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
python
Área
operating-systems
Tipo de issue
Nueva funcionalidad
Dificultad
3/5
Tiempo estimado
1-2 días
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
25/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.