o
    -wi                     @  s   d dl mZ d dlZd dlmZmZmZmZmZm	Z	 d dl
mZ ddlmZ edZdddZdddZdddZdddZdS )     )annotationsN)AnyCallable	CoroutineListTypeVarcast)asyncio_compat   )MailboxHandle_ThandleMailboxHandle[_T]timeoutfloat | Noneprogress_afterfloatdisplay_progress'Callable[[], Coroutine[Any, Any, None]]returnc                C  s   t | g|||dd S )zWait for a handle, possibly displaying progress to the user.

    Equivalent to passing a single handle to `wait_all_with_progress`.
    )r   r   r   r   )wait_all_with_progress)r   r   r   r    r   a/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/wandb/sdk/mailbox/wait_with_progress.pywait_with_progress   s   r   handle_listlist[MailboxHandle[_T]]list[_T]c                  sl   sg S dur|krt dS t zt |dW S  ty&   Y nw d fdd}t|S )a  Wait for multiple handles, possibly displaying progress to the user.

    Args:
        handle_list: The handles to wait for.
        timeout: A number of seconds after which to raise a TimeoutError,
            or None if this should never timeout.
        progress_after: A number of seconds after which to start the
            display_progress callback. Starting the callback creates a thread
            and starts an asyncio loop, so we want to avoid doing it if
            the handle is resolved quickly.
        display_progress: An asyncio function that displays progress to
            the user. This function is executed on a new thread and cancelled
            if the timeout is exceeded.

    Returns:
        A list where the Nth item is the Nth handle's result.

    Raises:
        TimeoutError: If the overall timeout expires.
        HandleAbandonedError: If any handle becomes abandoned.
        Exception: Any exception from the display function is propagated.
    Nr   r   r   c                    sf   t   " d urt  } |  }nd }t|dI d H W  d    S 1 s,w   Y  d S Nr   )r	   cancel_on_exittime	monotonic_wait_handles_async)elapsed_timeremaining_timeoutr   r   
start_timer   r   r   progress_loop_with_timeoutJ   s   

$z:wait_all_with_progress.<locals>.progress_loop_with_timeout)r   r   )_wait_handlesr    r!   TimeoutErrorr	   run)r   r   r   r   r'   r   r%   r   r       s   
r   c                C  s@   g }t  }| D ]}t  | }|| }||j|d q|S )zWait for multiple mailbox handles.

    Returns:
        Each handle's result, in the same order as the given handles.

    Raises:
        TimeoutError: If the overall timeout expires.
        HandleAbandonedError: If any handle becomes abandoned.
    r   )r    r!   appendwait_or)r   r   resultsr&   r   r#   r$   r   r   r   r(   Z   s   r(   c             	     s   dd  D d
 fdd}t  4 I d	H }tt D ]	}||| q W d	  I d	H  n1 I d	H s:w   Y  ttt S )zTAsynchronously wait for multiple mailbox handles.

    Just like _wait_handles.
    c                 S  s   g | ]}d qS )Nr   ).0_r   r   r   
<listcomp>|   s    z'_wait_handles_async.<locals>.<listcomp>indexintr   Nonec                   s$    |  }|j dI d H | < d S r   )
wait_async)r1   r   r   r-   r   r   r   wait_single~   s   z(_wait_handles_async.<locals>.wait_singleN)r1   r2   r   r3   )r	   open_task_grouprangelen
start_soonr   r   r   )r   r   r6   
task_groupr1   r   r5   r   r"   s   s   	(r"   )
r   r   r   r   r   r   r   r   r   r   )
r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   )r   r   r   r   r   r   )
__future__r   r    typingr   r   r   r   r   r   wandb.sdk.libr	   mailbox_handler   r   r   r   r(   r"   r   r   r   r   <module>   s     


: