
    3fi~                        d Z ddlmZ ddlZddlmZ ddlmZmZ ddl	m
Z
 ddlmZ dd	lmZ dd
lmZmZ  ej$                  e      ZeZ G d d      Zy)a  
A minimal scheduler to schedule tasks to run in the future (sync version).

Inspired to the standard library `sched.scheduler`, but designed for
multi-thread usage from the ground up, not as an afterthought. Tasks can be
scheduled in front of the one currently running and `Scheduler.run()` can be
left running without any tasks scheduled.

Tasks are called "Task", not "Event", here, because we actually make use of
`[threading/asyncio].Event` and the two would be confusing.
    )annotationsN)	monotonic)heappopheappush)Any)Callable   )Task)EventLockc                  0    e Zd ZddZdZddZd	dZddZy)
	Schedulerc                N    g | _         t               | _        t               | _        y )N)_queuer   _lockr   _event)selfs    P/var/www/auto_recruiter/arenv/lib/python3.12/site-packages/psycopg_pool/sched.py__init__zScheduler.__init__$   s    "$V
g    g     @c                @    t               |z   }| j                  ||      S )zpEnter a new task in the queue delayed in the future.

        Schedule a `!None` to stop the execution.
        )r   enterabs)r   delayactiontimes       r   enterzScheduler.enter+   s!    
 {U"}}T6**r   c                    t        ||      }| j                  5  t        | j                  |       | j                  d   |u }ddd       r| j                  j                          |S # 1 sw Y   'xY w)znEnter a new task in the queue at an absolute time.

        Schedule a `!None` to stop the execution.
        r   N)r
   r   r   r   r   set)r   r   r   taskfirsts        r   r   zScheduler.enterabs3   sd    
 D&!ZZ 	+T[[$'KKNd*E	+ KKOO	+ 	+s   (A''A0c                @   | j                   }	 | j                  5  t               }|r|d   ndx}r-|j                  |k  rt	        |       n|j                  |z
  }d}n| j
                  }| j                  j                          ddd       r|j                  sy	 |j                          n| j                  j                         # 1 sw Y   FxY w# t        $ r@}t        j                  d|j                  |j                  j                  |       Y d}~Qd}~ww xY w)zExecute the events scheduled.r   Nz$scheduled task run %s failed: %s: %s)r   r   r   r   r   EMPTY_QUEUE_TIMEOUTr   clearr   CLIENT_EXCEPTIONSloggerwarning	__class____name__wait)r   qnowr   r   es         r   runzScheduler.runB   s    KK 
$k$%AaD4040yyC'
 $		C# 44E!!#
$ {{KKM   '7 
$ 
$" ) NN>,,	 s$   A)CC C	D6DDN)returnNone)r   floatr   Callable[[], Any] | Noner.   r
   )r   r0   r   r1   r.   r
   )r(   
__module____qualname__r   r"   r   r   r-    r   r   r   r   "   s    
  +(r   r   )__doc__
__future__r   loggingr   r   heapqr   r   typingr   collections.abcr   _taskr
   _acompatr   r   	getLoggerr(   r%   	Exceptionr$   r   r4   r   r   <module>r?      sF   
 #   #  $  !			8	$ >( >(r   