
    i
                         d Z ddlZddlmZ ddlmZmZ ddlmZm	Z	m
Z
mZ ddlmZ ddlmZmZ  ej"                  e      Z G d	 d
      Zy)a  
A minimal scheduler to schedule tasks to run in the future (async 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.
    N)	monotonic)heappushheappop)AnyCallableListOptional   )Task)ALockAEventc                   h    e Zd ZddZdZdedeeg ef      de	fdZ
dedeeg ef      de	fd	Zdd
Zy)AsyncSchedulerreturnNc                 N    g | _         t               | _        t               | _        y )N)_queuer   _lockr   _event)selfs    x/var/www/html/hwPaymentPortal-be-dev/hw-payment-portal-api/venv/lib/python3.12/site-packages/psycopg_pool/sched_async.py__init__zAsyncScheduler.__init__   s    "$W
h    g     @delayactionc                 \   K   t               |z   }| j                  ||       d{   S 7 w)zpEnter a new task in the queue delayed in the future.

        Schedule a `!None` to stop the execution.
        N)r   enterabs)r   r   r   times       r   enterzAsyncScheduler.enter"   s+     
 {U"]]40000s   #,*,r   c                 (  K   t        ||      }| j                  4 d{    t        | j                  |       | j                  d   |u }ddd      d{    r| j                  j                          |S 7 Y7 $# 1 d{  7  sw Y   4xY ww)znEnter a new task in the queue at an absolute time.

        Schedule a `!None` to stop the execution.
        Nr   )r   r   r   r   r   set)r   r   r   taskfirsts        r   r   zAsyncScheduler.enterabs*   s     
 D&!:: 	+ 	+T[[$'KKNd*E	+ 	+ KKOO	+ 	+ 	+ 	+ 	+sD   BA9B(A=BA;#B;B=BBBBc                   K   | j                   }	 | j                  4 d{    t               }|r|d   nd}|r-|j                  |k  rt	        |       n|j                  |z
  }d}n| j
                  }| j                  j                          ddd      d{    r'|j                  sy	 |j                          d{    n#| j                  j                         d{    7 7 S# 1 d{  7  sw Y   cxY w7 B# t        $ r@}t        j                  d|j                  |j                  j                  |       Y d}~`d}~ww xY w7 lw)zExecute the events scheduled.Nr   z$scheduled task run %s failed: %s: %s)r   r   r   r   r   EMPTY_QUEUE_TIMEOUTr   clearr   	Exceptionloggerwarning	__class____name__wait_timeout)r   qnowr!   r   es         r   runzAsyncScheduler.run9   s'    KKzz $ $k qtdyyC'
 $		C# 44E!!#$ $ {{++-'' kk..u5559 $ $ $ $ $" (  NN>,,	  6s   EC*EA)C.EC,E-D  DD E$E%E,E.D 4C75D <ED 	E6E	E	EE)r   N)r*   
__module____qualname__r   r$   floatr	   r   r   r   r   r   r/    r   r   r   r      sf    
  1 1"c'9J0K 1PT 15 (8BG;L2M RV 6r   r   )__doc__loggingr   r   heapqr   r   typingr   r   r   r	   _taskr   _acompatr   r   	getLoggerr*   r'   r   r3   r   r   <module>r;      s=   
   # 0 0  #			8	$>6 >6r   