python / python/cpython

Adding Context Manger to threading.Timer

Aberta
#125,500 0 comentários 0 reações 0 responsáveis Ver no GitHub

Ninguém assumiu esta issue ainda.

stdlib type-feature
Linguagem predominante
Python
Estrelas
77.2k
Forks
35.9k
Métricas de merge de PRs
Métricas de PR pendentes

Descrição

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

Guia de contribuição

Abrir o guia de contribuição

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Direção de pesquisa

Comece pelo ponto de entrada threading.Timer e revise a discussão vinculada no Discourse e a PR gh-125501. O trabalho estará concluído quando Timer oferecer suporte ao uso proposto de gerenciamento de contexto, iniciando na entrada e sendo cancelado na saída, com o comportamento coberto conforme apropriado pelos testes existentes de threading.

Escrita pelo modelo de indexação a partir do texto da issue.

Avaliação

Stack de tecnologia
python
Domínio
operating-systems
Tipo de issue
Funcionalidade
Dificuldade
3/5
Tempo estimado
1-2 dias
Status de atividade
Estagnada
Clareza
Razoavelmente clara
Facilidade para iniciantes
25/100

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.