o
    ^f
                     @   sx   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eZG d	d
 d
ZdS )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                   @   sh   e Zd ZdddZdZdedeeg ef  de	fdd	Z
d
edeeg ef  de	fddZdddZdS )AsyncSchedulerreturnNc                 C   s   g | _ t | _t | _d S )N)_queuer   _lockr   _event)self r   Q/home/ertert/spirit/venv/lib/python3.10/site-packages/psycopg_pool/sched_async.py__init__   s   zAsyncScheduler.__init__g     @delayactionc                    s   t  | }| ||I dH S )zpEnter a new task in the queue delayed in the future.

        Schedule a `!None` to stop the execution.
        N)r   enterabs)r   r   r   timer   r   r   enter"   s   
zAsyncScheduler.enterr   c              	      sr   t ||}| j4 I dH  t| j| | jd |u }W d  I dH  n1 I dH s+w   Y  |r7| j  |S )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firstr   r   r   r   *   s   
(
zAsyncScheduler.enterabsc              
      s  | j }	 | j4 I dH 2 t }|r|d nd}|r,|j|kr$t| n|j| }d}n| j}| j  W d  I dH  n1 I dH sDw   Y  |rw|jsPdS z	| I dH  W n& t	yv } zt
d|j|jj| W Y d}~nd}~ww | j|I dH  q)zExecute the events scheduled.TNr   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   er   r   r   run9   s<   


(	zAsyncScheduler.run)r   N)r$   
__module____qualname__r   r   floatr   r   r   r
   r   r   r)   r   r   r   r   r      s    
""r   )__doc__loggingr   r   heapqr   r   typingr   r   r   r   _taskr
   _acompatr   r   	getLoggerr$   r!   r   r   r   r   r   <module>   s    
