estoy usando Threading.Event bastante estándar: hilo principal llega a un punto donde su en un bucle que se ejecuta:mejor solución para Python Threading.Event semi-ocupado esperando
event.wait(60)
Los otros bloques de una solicitud hasta que una respuesta se encuentra disponible y luego inicia una:
event.set()
yo esperaría que el hilo principal para seleccionar durante 40 segundos, pero este no es el caso. Desde la fuente de Python 2.7 Lib/threading.py:
# Balancing act: We can't afford a pure busy loop, so we
# have to sleep; but if we sleep the whole timeout time,
# we'll be unresponsive. The scheme here sleeps very
# little at first, longer as time goes on, but never longer
# than 20 times per second (or the timeout time remaining).
endtime = _time() + timeout
delay = 0.0005 # 500 us -> initial delay of 1 ms
while True:
gotit = waiter.acquire(0)
if gotit:
break
remaining = endtime - _time()
if remaining <= 0:
break
delay = min(delay * 2, remaining, .05)
_sleep(delay)
Lo que obtenemos es un selecto syscall pasan cada 500us. Esto causa una carga notable en la máquina con un lazo de selección bastante apretado.
¿Puede alguien explicar por qué hay un acto de equilibrio involucrado y por qué es diferente de un hilo esperando en un descriptor de archivo?
y segundo, ¿hay una mejor manera de implementar un hilo principal que duerme en su mayoría sin un lazo tan apretado?