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 (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.
    N)	monotonic)heappushheappop)AnyCallableListOptional   )Task)LockEventc                   @   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 )	SchedulerreturnNc                 C   s   g | _ t | _t | _d S )N)_queuer   _lockr   _event)self r   K/home/ertert/spirit/venv/lib/python3.10/site-packages/psycopg_pool/sched.py__init__   s   zScheduler.__init__g     @delayactionc                 C   s   t  | }| ||S )zpEnter a new task in the queue delayed in the future.

        Schedule a `!None` to stop the execution.
        )r   enterabs)r   r   r   timer   r   r   enter%   s   
zScheduler.enterr   c                 C   s\   t ||}| j t| j| | jd |u }W d   n1 s w   Y  |r,| j  |S )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firstr   r   r   r   -   s   

zScheduler.enterabsc              
   C   s   | j }	 | j/ t }|r|d nd}|r'|j|krt| n|j| }d}n| j}| j  W d   n1 s9w   Y  |ri|jsEdS z|  W n# t	yh } zt
d|j|jj| W Y d}~nd}~ww | j| q)zExecute the events scheduled.Tr   Nz$scheduled task run %s failed: %s: %s)r   r   r   r   r   EMPTY_QUEUE_TIMEOUTr   clearr   	Exceptionloggerwarning	__class____name__wait)r   qnowr   r   er   r   r   run<   s:   


	zScheduler.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   
