
    sh                         S SK r S SKrS SKrS SKrS SKrS SKJr  S SKJ	r	  SSK
JrJrJrJr  / SQr\ R                   " \5      r " S S\5      r " S	 S
\5      r " S S\5      rg)    N)Empty)Any   )RequestQueueTimerClientTimerRequestTimerServer)LocalTimerClientMultiprocessingRequestQueueLocalTimerServerc                   8   ^  \ rS rSrSrU 4S jrS rS rSrU =r	$ )r
      a.  
Client side of ``LocalTimerServer``. This client is meant to be used
on the same host that the ``LocalTimerServer`` is running on and uses
pid to uniquely identify a worker. This is particularly useful in situations
where one spawns a subprocess (trainer) per GPU on a host with multiple
GPU devices.
c                 .   > [         TU ]  5         Xl        g Nsuper__init__	_mp_queueselfmp_queue	__class__s     /Users/tiagomarins/Projetos/claudeai/copy_bank/venv/lib/python3.13/site-packages/torch/distributed/elastic/timer/local_timer.pyr   LocalTimerClient.__init__        !    c                 |    [         R                  " 5       n[        X1U5      nU R                  R	                  U5        g r   osgetpidr   r   put)r   scope_idexpiration_timepidacquire_requests        r   acquireLocalTimerClient.acquire$   s+    iik&soF?+r   c                 |    [         R                  " 5       n[        X!S5      nU R                  R	                  U5        g )Nr   )r   r"   r$   release_requests       r   releaseLocalTimerClient.release)   s+    iik&sb9?+r   r   )
__name__
__module____qualname____firstlineno____doc__r   r&   r+   __static_attributes____classcell__r   s   @r   r
   r
      s    ",
, ,r   r
   c                   n   ^  \ rS rSrSrS\R                  4U 4S jjrS\4S jr	S\
S\\   4S jrS	rU =r$ )
r   /   z?
A ``RequestQueue`` backed by python ``multiprocessing.Queue``
r   c                 .   > [         TU ]  5         Xl        g r   r   r   s     r   r   $MultiprocessingRequestQueue.__init__4   r   r   returnc                 6    U R                   R                  5       $ r   )r   qsize)r   s    r   size MultiprocessingRequestQueue.size8   s    ~~##%%r   timeoutc                 "   / nUn[        SU5       Hh  n[        R                  " 5       n U R                  R                  SUS9nUR                  U5        U[        R                  " 5       U-
  -
  nUS::  d  Mg    U$    U$ ! [         a       U$ f = f)Nr   T)blockr?   )rangetimer   getr   append)r   r=   r?   requestswait_startrs           r   rD   MultiprocessingRequestQueue.get;   s    q$AIIKENN&&T4&@ OOA499;./Dqy      s   A??
BBr-   )r.   r/   r0   r1   r2   mpQueuer   intr=   floatlistr   rD   r3   r4   r5   s   @r   r   r   /   sA    " "&c & 4+=  r   r   c                      ^  \ rS rSrSr SS\R                  S\S\4U 4S jjjr	S\
\   SS	4S
 jrS\\   SS	4S jrS\S\\\
\   4   4S jrS\S\4S jrSrU =r$ )r   N   a3  
Server that works with ``LocalTimerClient``. Clients are expected to be
subprocesses to the parent process that is running this server. Each host
in the job is expected to start its own timer server locally and each
server instance manages timers for local workers (running on processes
on the same host).
r   max_intervaldaemonc                 F   > [         TU ]  [        U5      X#5        0 U l        g r   )r   r   r   _timers)r   r   rS   rT   r   s       r   r   LocalTimerServer.__init__W   s!     	4X>U<>r   timer_requestsr:   Nc                     U H[  nUR                   nUR                  nUR                  nUS:  a  U R                  R	                  X44S 5        ML  X R                  X44'   M]     g )Nr   )	worker_idr"   r#   rV   pop)r   rX   requestr$   r"   r#   s         r   register_timers LocalTimerServer.register_timers]   s[    %G##C''H%55O "  #$707c_- &r   
worker_idsc                     [        U R                  R                  5       5       H(  u  p#X!;   d  M  U R                  R                  X#45        M*     g r   )rP   rV   keysr[   )r   r_   r$   r"   s       r   clear_timersLocalTimerServer.clear_timersi   s:    !$,,"3"3"56MC   #1 7r   deadlinec                     0 nU R                   R                  5        HB  nUR                  U::  d  M  UR                  UR                  / 5      nUR                  U5        MD     U$ r   )rV   valuesr#   
setdefaultrZ   rE   )r   rd   expired_timersr\   expired_scopess        r   get_expired_timers#LocalTimerServer.get_expired_timersn   sZ    8:||**,G&&(2!/!:!:7;L;Lb!Q%%g. - r   rZ   c                      [         R                  " U[        R                  5        g! [         a    [
        R                  SU5         g[         a    [
        R                  SU5         gf = f)NTz,Process with pid=%s does not exist. SkippingzError terminating pid=%sF)	r   killsignalSIGKILLProcessLookupErrorloggerinfo	Exception	exception)r   rZ   s     r   _reap_workerLocalTimerServer._reap_workerw   s\    	DGGIv~~.! 	KKF	R 	D7C	Ds   %(  A,
A,+A,)rV   )<   T)r.   r/   r0   r1   r2   rL   rM   rO   boolr   rP   r   r]   setrN   rb   dictr   rj   ru   r3   r4   r5   s   @r   r   r   N   s     LP??05?DH? ?
8d<.@ 
8T 
82s3x 2D 2
5 T#tL?Q:Q5R 	c 	d 	 	r   r   )loggingmultiprocessingrL   r   rn   rC   queuer   typingr   apir   r   r   r	   __all__	getLoggerr.   rq   r
   r   r    r   r   <module>r      s^      	     E E R			8	$,{ ,0, >2{ 2r   