
    (^i                    :   d dl mZ d dlZd dlmZ d dlZd dlmZ d dlmZ d dl	m
Z
 d dlZd dlmZ d dlmZ d d	lmZ d d
lmZ  G d dej&                        Z G d dej&                        Z G d de      Z G d de      ZddZ ed      dd       ZddZy)    )annotationsN)Callable)Event)Thread)TracebackType)experimental_func)BaseStorage)FrozenTrial)
TrialStatec                      e Zd ZdZej
                  dd       Zej
                  dd       Zej
                  d	d       Zej
                  d
d       Z	y)BaseHeartbeatal  Base class for heartbeat.

    This class is not supposed to be directly accessed by library users.

    The heartbeat mechanism periodically checks whether each trial process is alive during an
    optimization loop. To support this mechanism, the methods of
    :class:`~optuna.storages._heartbeat.BaseHeartbeat` is implemented for the target database
    backend, typically with multiple inheritance of :class:`~optuna.storages._base.BaseStorage`
    and :class:`~optuna.storages._heartbeat.BaseHeartbeat`.

    .. seealso::
        See :class:`~optuna.storages.RDBStorage`, where the backend supports heartbeat.
    c                    t               )zqRecord the heartbeat of the trial.

        Args:
            trial_id:
                ID of the trial.
        NotImplementedError)selftrial_ids     [/var/www/html/hubwallet-dev/venv/lib/python3.12/site-packages/optuna/storages/_heartbeat.pyrecord_heartbeatzBaseHeartbeat.record_heartbeat    s     "##    c                    t               )zGet the stale trial ids of the study.

        Args:
            study_id:
                ID of the study.
        Returns:
            List of IDs of trials whose heartbeat has not been updated for a long time.
        r   )r   study_ids     r   _get_stale_trial_idsz"BaseHeartbeat._get_stale_trial_ids*   s     "##r   c                    t               )zGet the heartbeat interval if it is set.

        Returns:
            The heartbeat interval if it is set, otherwise :obj:`None`.
        r   r   s    r   get_heartbeat_intervalz$BaseHeartbeat.get_heartbeat_interval6        "##r   c                    t               )zGet the failed trial callback function.

        Returns:
            The failed trial callback function if it is set, otherwise :obj:`None`.
        r   r   s    r   get_failed_trial_callbackz'BaseHeartbeat.get_failed_trial_callback?   r   r   N)r   intreturnNone)r   r   r    z	list[int])r    z
int | None)r    z4Callable[['optuna.Study', FrozenTrial], None] | None)
__name__
__module____qualname____doc__abcabstractmethodr   r   r   r    r   r   r   r      so     	$ $ 		$ 	$ 	$ $ 	$ $r   r   )	metaclassc                  x    e Zd ZddZ	 	 	 	 	 	 	 	 ddZej                  dd       Zej                  dd       Zy)BaseHeartbeatThreadc                $    | j                          y N)startr   s    r   	__enter__zBaseHeartbeatThread.__enter__J   s    

r   c                $    | j                          y r-   )join)r   exc_type	exc_value	tracebacks       r   __exit__zBaseHeartbeatThread.__exit__M   s     			r   c                    t               r-   r   r   s    r   r.   zBaseHeartbeatThread.startU       !##r   c                    t               r-   r   r   s    r   r1   zBaseHeartbeatThread.joinY   r7   r   Nr    r!   )r2   ztype[Exception] | Noner3   zException | Noner4   zTracebackType | Noner    r!   )	r"   r#   r$   r/   r5   r&   r'   r.   r1   r(   r   r   r+   r+   I   sf    ( $ (	
 
 	$ $ 	$ $r   r+   c                  $    e Zd ZddZddZddZy)NullHeartbeatThreadc                     y r-   r(   r   s    r   __init__zNullHeartbeatThread.__init___       r   c                     y r-   r(   r   s    r   r.   zNullHeartbeatThread.startb   r>   r   c                     y r-   r(   r   s    r   r1   zNullHeartbeatThread.joine   r>   r   Nr9   )r"   r#   r$   r=   r.   r1   r(   r   r   r;   r;   ^   s    r   r;   c                  6    e Zd ZddZddZddZedd       Zy)	HeartbeatThreadc                <    || _         || _        d | _        d | _        y r-   )	_trial_id
_heartbeat_thread_stop_event)r   r   	heartbeats      r   r=   zHeartbeatThread.__init__j   s    !#&*)-r   c                    t               | _        t        | j                  | j                  | j
                  | j                  f      | _        | j                  j                          y )N)targetargs)r   rG   r   _record_heartbeatrD   rE   rF   r.   r   s    r   r.   zHeartbeatThread.startp   sK     7))RVRbRb0c
 	r   c                    | j                   J | j                  J | j                   j                          | j                  j                          y r-   )rG   rF   setr1   r   s    r   r1   zHeartbeatThread.joinw   sF    +++||'''r   c                v    |j                         }|J 	 |j                  |        |j                  |      ry %)N)timeout)r   r   wait)r   rH   
stop_eventheartbeat_intervals       r   rL   z!HeartbeatThread._record_heartbeat}   sE    &==?!---&&x0'9: r   N)r   r   rH   r   r    r!   r9   )r   r   rH   r   rR   r   r    r!   )r"   r#   r$   r=   r.   r1   staticmethodrL   r(   r   r   rB   rB   i   s%    .  r   rB   c                h    t        |      rt        |t              sJ t        | |      S t	               S r-   )is_heartbeat_enabled
isinstancer   rB   r;   )r   storages     r   get_heartbeat_threadrY      s0    G$'=111x11"$$r   z2.9.0c                   | j                   }t        |t              syt        |      syg }|j	                  | j
                        D ]5  }	 |j                  |t        j                        r|j                  |       7 |j                         }|5|D ]/  }t        j                  |j!                  |            } || |       1 yy# t        j                  j                  $ r Y w xY w)au  Fail stale trials and run their failure callbacks.

    The running trials whose heartbeat has not been updated for a long time will be failed,
    that is, those states will be changed to :obj:`~optuna.trial.TrialState.FAIL`.

    .. seealso::

        See :class:`~optuna.storages.RDBStorage`.

    Args:
        study:
            Study holding the trials to check.
    N)state)_storagerW   r   rV   r   	_study_idset_trial_state_valuesr   FAILappendoptuna
exceptionsUpdateFinishedTrialErrorr   copydeepcopy	get_trial)studyrX   failed_trial_idsr   failed_trial_callbackfailed_trials         r   fail_stale_trialsrk      s     nnGg}-(00A 	--hjoo-N ''1 $==?(( 	7H==):):8)DEL!%6	7 )   99 	 	s   2CC'&C'c                J    t        | t              xr | j                         duS )a2  Check whether the storage enables the heartbeat.

    Returns:
        :obj:`True` if the storage also inherits :class:`~optuna.storages._heartbeat.BaseHeartbeat`
        and the return value of :meth:`~optuna.storages.BaseStorage.get_heartbeat_interval` is an
        integer, otherwise :obj:`False`.
    N)rW   r   r   )rX   s    r   rV   rV      s%     g}-^'2P2P2RZ^2^^r   )r   r   rX   r	   r    r+   )rg   z'optuna.Study'r    r!   )rX   r	   r    bool)
__future__r   r&   collections.abcr   rd   	threadingr   r   typesr   ra   optuna._experimentalr   optuna.storagesr	   optuna.trialr
   r   ABCMetar   r+   r;   rB   rY   rk   rV   r(   r   r   <module>rv      s    " 
 $      2 ' $ #5$ckk 5$p$CKK $*- ) <% 7$7 $7N_r   