L i:ddlmZddlZddlmZddlZddlmZddlmZddl m Z ddl Z ddl m Z ddlmZdd lmZdd lmZGd d ej& ZGddej& ZGddeZGddeZddZe dddZddZy)) annotationsN)Callable)Event)Thread) TracebackType)experimental_func) BaseStorage) FrozenTrial) TrialStateceZdZdZej ddZej ddZej d dZej d dZ y) BaseHeartbeatalBase 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. ct)zqRecord the heartbeat of the trial. Args: trial_id: ID of the trial. NotImplementedError)selftrial_ids `/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/optuna/storages/_heartbeat.pyrecord_heartbeatzBaseHeartbeat.record_heartbeat s "##ct)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)rstudy_ids r_get_stale_trial_idsz"BaseHeartbeat._get_stale_trial_ids*s "##rct)zGet the heartbeat interval if it is set. Returns: The heartbeat interval if it is set, otherwise :obj:`None`. rrs rget_heartbeat_intervalz$BaseHeartbeat.get_heartbeat_interval6 "##rct)zGet the failed trial callback function. Returns: The failed trial callback function if it is set, otherwise :obj:`None`. rrs rget_failed_trial_callbackz'BaseHeartbeat.get_failed_trial_callback?rrN)rintreturnNone)rrr z list[int])r z int | None)r z4Callable[['optuna.Study', FrozenTrial], None] | None) __name__ __module__ __qualname____doc__abcabstractmethodrrrrrrr r so  $$  $ $ $$ $$rr ) metaclasscxeZdZddZ ddZej ddZej ddZy)BaseHeartbeatThreadc$|jyN)startrs r __enter__zBaseHeartbeatThread.__enter__Js  rc$|jyr-)join)rexc_type exc_value tracebacks r__exit__zBaseHeartbeatThread.__exit__Ms rctr-rrs rr.zBaseHeartbeatThread.startU !##rctr-rrs rr1zBaseHeartbeatThread.joinYr7rNr r!)r2ztype[Exception] | Noner3zException | Noner4zTracebackType | Noner r!) r"r#r$r/r5r&r'r.r1r(rrr+r+Isf($(    $$ $$rr+c$eZdZddZddZddZy)NullHeartbeatThreadcyr-r(rs r__init__zNullHeartbeatThread.__init___ rcyr-r(rs rr.zNullHeartbeatThread.startbr>rcyr-r(rs rr1zNullHeartbeatThread.joiner>rNr9)r"r#r$r=r.r1r(rrr;r;^s   rr;c6eZdZddZddZddZeddZy) HeartbeatThreadc<||_||_d|_d|_yr-) _trial_id _heartbeat_thread _stop_event)rr heartbeats rr=zHeartbeatThread.__init__js!#&* )-rct|_t|j|j|j |jf|_|j jy)N)targetargs)rrGr_record_heartbeatrDrErFr.rs rr.zHeartbeatThread.startpsK 7))RVRbRb0c   rc|jJ|jJ|jj|jjyr-)rGrFsetr1rs rr1zHeartbeatThread.joinwsF+++||'''  rcv|j}|J |j||j|ry%)N)timeout)rrwait)rrH stop_eventheartbeat_intervals rrLz!HeartbeatThread._record_heartbeat}sE&==?!---  & &x 0'9:rN)rrrHr r r!r9)rrrHr rRrr r!)r"r#r$r=r.r1 staticmethodrLr(rrrBrBis%.  rrBcht|rt|tsJt||St Sr-)is_heartbeat_enabled isinstancer rBr;)rstorages rget_heartbeat_threadrYs0G$'=111x11"$$rz2.9.0c|j}t|tsyt|syg}|j |j D]5} |j |tjr|j|7|j}|5|D]/}tj|j!|}|||1yy#tjj$rYwxYw)auFail 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)_storagerWr rVr _study_idset_trial_state_valuesr FAILappendoptuna exceptionsUpdateFinishedTrialErrorrcopydeepcopy get_trial)studyrXfailed_trial_idsrfailed_trial_callback failed_trials rfail_stale_trialsrksnnG g} -  (00A --hjoo-N ''1$==?(( 7H==):):8)DEL !% 6 7)   99   s 2CC'&C'cJt|txr|jduS)a2Check 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)rWr r)rXs rrVrVs% g} - ^'2P2P2RZ^2^^r)rrrXr r r+)rgz'optuna.Study'r r!)rXr r bool) __future__rr&collections.abcrrd threadingrrtypesrraoptuna._experimentalroptuna.storagesr optuna.trialr r ABCMetar r+r;rBrYrkrVr(rrrvs" $  2'$#5$ckk5$p$CKK$* - )<%7$7$7N_r