o
    eP                     @  s   d dl mZ d dlZd dlZd dlZd dl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mZ d dlmZmZmZmZmZ d d	lmZ d d
lmZ d dlmZmZm Z  G dd dZ!G dd dZ"dS )    )annotationsN)TimeoutError)deque)Any)Literal)route_utilsroutes)
Estimation
LogMessagePredictBodyProgressProgressUnit)Error)TrackedIterable)run_coro_in_backgroundsafe_get_lockset_task_namec                   @  s"   e Zd ZdddZddddZdS )Event	websocketfastapi.WebSocketsession_hashstrfn_indexintc                 C  sP   || _ || _|| _| j d| j | _d | _d | _d | _d | _d| _t	 | _
d S )N_F)r   r   r   _iddataZlost_connection_timeusernameprogressprogress_pendingr   log_messages)selfr   r   r    r"   ?D:\Projects\ConvertPro\env\Lib\site-packages\gradio/queueing.py__init__   s   zEvent.__init__  codec                   s   | j j|dI d H  d S )N)r&   )r   close)r!   r&   r"   r"   r#   
disconnect.   s   zEvent.disconnectN)r   r   r   r   r   r   )r%   )r&   r   )__name__
__module____qualname__r$   r(   r"   r"   r"   r#   r      s    
r   c                   @  s  e Zd ZdeddZdfddZdd Zdd ZdgddZdhddZdiddZ	djdd Z
djd!d"Zdkd%d&Zdld+d,Zdmd0d1Zdnd2d3Zdkd4d5Zdjd6d7Zdodpd9d:Zdjd;d<Zdjd=d>ZdqdBdCZdrdEdFZdsdGdHZdtdLdMZdudQdRZdvdSdTZdwdxdZd[Zdydzd^d_Zd{dbdcZddS )|Queuelive_updatesboolconcurrency_countr   update_intervalsfloatmax_size
int | Noneblocks_dependencieslistc                 C  s   t  | _g | _d| _|| _|| _d g| | _t | _d | _	d| _
d| _d| _d | _d| _|| _d| _d| _|| _|| _g | _d S )NFr      g?g?)r   event_queueZevents_pending_reconnectionstoppedmax_thread_countr0   active_jobsr   delete_lock
server_appduration_history_totalduration_history_countavg_process_timeavg_concurrent_process_timequeue_durationr-   sleep_when_freeprogress_update_sleep_when_freer2   r4   continuous_tasks)r!   r-   r/   r0   r2   r4   r"   r"   r#   r$   3   s&   
zQueue.__init__Tc                   s.   t | j t | j | jst | j d S d S N)r   start_processingstart_log_and_progress_updatesr-   notify_clients)r!   Z
ssl_verifyr"   r"   r#   startO   s   

zQueue.startc                 C  
   d| _ d S )NTr8   r!   r"   r"   r#   r'   U      
zQueue.closec                 C  rJ   NFrK   rL   r"   r"   r#   resumeX   rM   zQueue.resumeapp
routes.Appc                 C  s
   || _ d S rE   )r<   )r!   rP   r"   r"   r#   set_server_app[   rM   zQueue.set_server_appreturnc                 C  s$   d}| j D ]
}|d ur|d7 }q|S )Nr   r6   )r:   )r!   countZworkerr"   r"   r#   get_active_worker_count^   s   
zQueue.get_active_worker_counttuple[list[Event] | None, bool]c                   s   j sdS j  }|g}|j j  d }|r>j  d } fddj D d |d  }|| fdd|D  ||fS )NrN   batchZmax_batch_sizec                   s   g | ]	}|j  kr|qS r"   )r   .0event)event_fn_indexr"   r#   
<listcomp>q   s    z-Queue.get_events_in_batch.<locals>.<listcomp>r6   c                   s   g | ]} j |qS r"   )r7   removerX   rL   r"   r#   r\   u   s    )r7   popleftr   r4   extend)r!   Zfirst_eventeventsrW   Z
batch_sizeZrest_of_batchr"   )r[   r!   r#   get_events_in_batche   s    



zQueue.get_events_in_batchNonec              	     s   | j sl| jst| jI d H  qd | jvr t| jI d H  q| j4 I d H  |  \}}W d   I d H  n1 I d H s>w   Y  |rg|| j| jd < t	| j
||}t	| j t||d j|d j| | j rd S d S )Nr   )r8   r7   asynciosleeprB   r:   r;   ra   indexr   process_eventsbroadcast_live_estimationsr   r   r   )r!   r`   rW   taskr"   r"   r#   rF   y   s"   
(
zQueue.start_processingc                   s   | j sYdd | jD | j }t|dkrt| jI d H  q|D ])}|jrB|jrBd|_| 	||j
 I d H }|sB| |I d H  | |I d H  q!t| jI d H  | j rd S d S )Nc                 S  "   g | ]}|d ur|D ]}|q
qS rE   r"   rY   jobevtr"   r"   r#   r\          z8Queue.start_log_and_progress_updates.<locals>.<listcomp>r   F)r8   r:   rD   lenrc   rd   rC   r   r   send_messagedictclean_eventsend_log_updates_for_event)r!   r`   rZ   client_awaker"   r"   r#   rG      s&   z$Queue.start_log_and_progress_updatesrZ   r   c                   sT   	 z|j  }W n
 ty   Y d S w | || I d H }|s)| |I d H  qrE   )r    r^   
IndexErrorro   rp   rq   )r!   rZ   messagers   r"   r"   r#   rr      s   z Queue.send_log_updates_for_eventevent_idr   	iterableslist[TrackedIterable] | Nonec              
   C  s   |d u rd S | j D ]4}|d u rq	|D ]*}|j|kr<g }|D ]}t|j|j|j|j|jd}|| qt	|d|_d|_
qq	d S )N)re   lengthunitr   desc)progress_dataT)r:   r   r   re   ry   rz   r   r{   appendr   r   )r!   rv   rw   rk   rl   r|   iterableZprogress_unitr"   r"   r#   set_progress   s,   

zQueue.set_progressloglevelLiteral['info', 'warning']c                 C  sF   dd | j D | j }|D ]}|j|kr t||d}|j| qd S )Nc                 S  ri   rE   r"   rj   r"   r"   r#   r\      rm   z%Queue.log_message.<locals>.<listcomp>)r   r   )r:   rD   r   r
   r    r}   )r!   rv   r   r   r`   rZ   log_messager"   r"   r#   r      s   
zQueue.log_messagec                 C  s2   t | j}| jdur|| jkrdS | j| |S )z
        Add event to queue, or return None if Queue is full
        Parameters:
            event: Event to add to Queue
        Returns:
            rank of submitted Event
        N)rn   r7   r2   r}   )r!   rZ   Z	queue_lenr"   r"   r#   push   s
   
z
Queue.pushc              	     s\   || j v r,| j4 I d H  | j | W d   I d H  d S 1 I d H s%w   Y  d S d S rE   )r7   r;   r]   )r!   rZ   r"   r"   r#   rq      s   
.zQueue.clean_eventc                   s   | j r|  I dH  dS dS )z
        Runs 2 functions sequentially instead of concurrently. Otherwise dced clients are tried to get deleted twice.
        N)r-   broadcast_estimationsrL   r"   r"   r#   rg      s   z Queue.broadcast_live_estimations<   c                   sn   |j s5| |ddiI dH }|sdS | j||dI dH \}}|s2| |dddidd	I dH  dS ||_ d
S )z
        Gather data for the event
        Parameters:
            event: the Event to gather data for
            receive_timeout: how long to wait for data to be received from frontend
        msgZ	send_dataNFtimeoutprocess_completederrorz!Time out uploading data to serverr   outputsuccessT)r   ro   get_message)r!   rZ   Zreceive_timeoutrs   r   r"   r"   r#   gather_event_data   s"   
zQueue.gather_event_datac                   s<   | j st| jI dH  | jr|  I dH  | j rdS dS )zQ
        Notify clients about events statuses in the queue periodically.
        N)r8   rc   rd   r0   r7   r   rL   r"   r"   r#   rH   	  s   zQueue.notify_clientsc                   s4      tj fddtjD  I d H  d S )Nc                   s   g | ]\}} | |qS r"   )send_estimation)rY   rankrZ   
estimationr!   r"   r#   r\     s    z/Queue.broadcast_estimations.<locals>.<listcomp>)get_estimationrc   gather	enumerater7   rL   r"   r   r#   r     s   zQueue.broadcast_estimationsr   r	   r   c                   sn   ||_ | jdur |j | j | j |_d| jvr | j| j7  _| || I dH }|s5| |I dH  |S )z
        Send estimation about ETA to the client.

        Parameters:
            event:
            estimation:
            rank:
        N)r   r@   r?   Zrank_etar:   ro   rp   rq   )r!   rZ   r   r   rs   r"   r"   r#   r     s   


zQueue.send_estimationdurationc                 C  sV   |  j |7  _ |  jd7  _| j | j | _| jt| j| j | _| jt| j | _dS )zt
        Update estimation by last x element's average duration.

        Parameters:
            duration:
        r6   N)	r=   r>   r?   minr9   r@   rn   r7   rA   )r!   r   r"   r"   r#   update_estimation6  s   
zQueue.update_estimationc                 C  s   t t| j| j| j| jdS )N)Z
queue_sizeZavg_event_process_timeZ!avg_event_concurrent_process_timeZ	queue_eta)r	   rn   r7   r?   r@   rA   rL   r"   r"   r#   r   G  s   zQueue.get_estimationr   r   dict[str, Any]c                 C  s\   t |jt|jt|jt|j|jj|jjdd}z|j	|d< W |S  t
y-   Y |S w )N)hostport)urlheadersquery_paramspath_paramsclientsession)r   r   rp   r   r   r   r   r   r   r   	Exception)r!   r   paramsr"   r"   r#   get_request_paramsO  s    zQueue.get_request_paramsr`   list[Event]rW   c              
     s  |d j }|d usJ d|d j}|s|d jnd |_z |d j|_W n	 ty1   Y nw |rMtt	dd |D  |_  fdd|D |_d|_
 j}|d u rXtdd}tj|||d	}tj||||d d
}ztj||||dI d H }	W n' ty }
 z| jpt|
t}t  t|rt|
|
d |
d }
~
ww |jj}t|tjjr|j}n|}||	}t|j ! }|S )Nr   zNo event datac                 S  s   g | ]	}|j r|j j qS r"   r   rX   r"   r"   r#   r\   j  s    z)Queue.call_prediction.<locals>.<listcomp>c                   s   g | ]}|j r |jqS r"   )r   r   r   rX   rL   r"   r#   r\   k  s    
TServer app has not been set.Zpredict)rP   api_namebody)rP   r   fn_index_inferredr   request)rP   r   
gr_requestr   )"r   r   r   rv   r   r   r   
ValueErrorr5   zipZbatchedr<   r   r   Zinfer_fn_indexZcompile_gr_requestZcall_process_apiZ
get_blocks
show_error
isinstancer   	traceback	print_excr   ZrouterZdefault_response_classfastapiZdatastructuresZDefaultPlaceholdervaluejsonloadsr   decode)r!   r`   rW   r   r   rP   r   r   r   r   r   r   response_classZactual_response_classhttp_responseZresponse_jsonr"   rL   r#   call_prediction_  sh   


	zQueue.call_predictionc                   s  g }z1z|D ]}|  |I d H }|r | |ddiI d H }|r'|| q	|sfW W |D ]}z	| I d H  W q. tyB   Y q.w d | j| j|< |D ]}| |I d H  | |j	|j
I d H  qNd S t }z| ||I d H }d }W n, ty }	 z d }|	}|D ]}| |ddt|	iddI d H  qW Y d }	~	nd }	~	ww |r|ddr|}
|}|rT|ddrT|}
|}g }|D ]}| |d|
|
d udI d H }|| qd	d
 t||D }|s&W W |D ]}z	| I d H  W q ty   Y qw d | j| j|< |D ]}| |I d H  | |j	|j
I d H  qd S z| ||I d H }d }W n tyJ }	 z
d }|	}W Y d }	~	nd }	~	ww |rT|dds|D ]8}|d u r`|}n|
pd|}| |I d H  | |dt|tr}dt|in||ot|t dI d H  qVn>|rt|}t|D ]1\}	}|rd|v rtt|d |	 |d< | |I d H  | |d||d udI d H  qt }|d ur| ||  W n ty }	 z
t|	 W Y d }	~	nd }	~	ww W |D ]}z
| I d H  W q ty   Y qw d | j| j|< |D ]}| |I d H  | |j	|j
I d H  qd S |D ]}z
| I d H  W q8 tyO   Y q8w d | j| j|< |D ]}| |I d H  | |j	|j
I d H  q[w )Nr   Zprocess_startsr   r   Fr   Zis_generatingZprocess_generatingc                 S  s   g | ]\}}|r|qS r"   r"   )rY   eZis_openr"   r"   r#   r\     s
    z(Queue.process_events.<locals>.<listcomp>r   )r   ro   r}   r(   r   r:   re   rq   reset_iteratorsr   r   timer   r   getr   rr   r   copydeepcopyr   r5   r   print)r!   r`   rW   Zawake_eventsrZ   rs   Z
begin_timeresponseerrr   Zold_responseZold_errZopen_wsopenZrelevant_responser   end_timer"   r"   r#   rf     s$  

T

.





zQueue.process_eventsr6   r   rp   r   float | intc                   sL   zt j|jj|d|dI d H  W dS  ty%   | |I d H  Y dS w )Nr   r   TF)rc   wait_forr   Z	send_jsonr   rq   )r!   rZ   r   r   r"   r"   r#   ro     s   zQueue.send_message   tuple[PredictBody | None, bool]c                   sV   zt j|j |dI d H }tdi |dfW S  ty*   | |I d H  Y dS w )Nr   TrN   r"   )rc   r   r   Zreceive_jsonr   AsyncTimeOutErrorrq   )r!   rZ   r   r   r"   r"   r#   r     s   
zQueue.get_messager   r   c              	     s   | j }|d u rtd||jvrd S |j4 I d H  d |j| |< |j| | W d   I d H  d S 1 I d H s;w   Y  d S )Nr   )r<   r   	iteratorslockZiterators_to_resetadd)r!   r   r   rP   r"   r"   r#   r   "  s   
zQueue.reset_iteratorsN)
r-   r.   r/   r   r0   r1   r2   r3   r4   r5   )T)rP   rQ   )rS   r   )rS   rV   )rS   rb   )rZ   r   rS   rb   )rv   r   rw   rx   )rv   r   r   r   r   r   )rZ   r   rS   r3   )r   )rZ   r   rS   r.   )rZ   r   r   r	   r   r   rS   r	   )r   r1   rS   rb   )rS   r	   )r   r   rS   r   )r`   r   rW   r.   )r`   r   rW   r.   rS   rb   )r6   )r   rp   r   r   rS   r.   )r   )rS   r   )r   r   r   r   )r)   r*   r+   r$   rI   r'   rO   rR   rU   ra   rF   rG   rr   r   r   r   rq   rg   r   rH   r   r   r   r   r   r   rf   ro   r   r   r"   r"   r"   r#   r,   2   s8    















	






An

r,   )#
__future__r   rc   r   r   r   r   r   r   collectionsr   typingr   r   Ztyping_extensionsr   Zgradior   r   Zgradio.data_classesr	   r
   r   r   r   Zgradio.exceptionsr   Zgradio.helpersr   Zgradio.utilsr   r   r   r   r,   r"   r"   r"   r#   <module>   s$    