o
    zit	                     @   sT   d dl mZ d dlmZmZ d dlmZ d dlmZ d dl	m
Z
 G dd de
ZdS )	    )ThreadPoolExecutor)AnyOptional)override)CheckpointIO)_WrappingCheckpointIOc                       sX   e Zd ZdZdded ddf fddZeded	eddfd
dZedddZ	  Z
S )AsyncCheckpointIOa  ``AsyncCheckpointIO`` enables saving the checkpoints asynchronously in a thread.

    .. warning::  This is an :ref:`experimental <versioning:Experimental API>` feature.

    Args:
        checkpoint_io: A checkpoint IO plugin that is used as the basis for async checkpointing.

    Ncheckpoint_ior   returnc                    s"   t  | tdd| _d | _d S )N   )max_workers)super__init__r   	_executor_error)selfr	   	__class__ ]/home/ubuntu/.local/lib/python3.10/site-packages/pytorch_lightning/plugins/io/async_plugin.pyr   "   s   
zAsyncCheckpointIO.__init__argskwargsc                    sD   dt dt ddf fdd} jj|g|R i |  jr  jdS )zYUses the ``ThreadPoolExecutor`` to save the checkpoints using the base ``checkpoint_io``.r   r   r
   Nc               
      sT   z j d usJ  j j| i | W d S  ty) } z
| _W Y d }~d S d }~ww N)r	   save_checkpointBaseExceptionr   )r   r   exr   r   r   _save_checkpoint,   s   z;AsyncCheckpointIO.save_checkpoint.<locals>._save_checkpoint)r   r   submitr   )r   r   r   r   r   r   r   r   (   s
   z!AsyncCheckpointIO.save_checkpointc                 C   s   | j jdd | jr| jdS )z+This method is called to close the threads.T)waitN)r   shutdownr   r   r   r   r   teardown:   s   zAsyncCheckpointIO.teardownr   )r
   N)__name__
__module____qualname____doc__r   r   r   r   r   r!   __classcell__r   r   r   r   r      s    	r   N)concurrent.futuresr   typingr   r   typing_extensionsr   lightning_fabric.pluginsr   $pytorch_lightning.plugins.io.wrapperr   r   r   r   r   r   <module>   s   