a
    Ad+                     @   s\   d dl Z d dlZd dlZd dlZd dlmZmZmZ d dlm	Z	 e
eZG dd dZdS )    N)errorsshareddevices)Optionalc                   @   s4  e Zd ZU dZdZdZdZdZdZdZ	dZ
dZdZdZdZdZdZdZdZe ZdZee ed< dd Zeed	d
dZejeddddZedd Zejee ddddZd*ee  ee dddZ!dd	ddZ"dd Z#dd Z$dd Z%dd Z&d+edd d!Z'd"d# Z(d$d% Z)d&d' Z*d(d) Z+dS ),StateF r   0N_server_commandc                 C   s   t   | _d S N)timeserver_startself r   </var/www/html/stable-diffusion-webui/modules/shared_state.py__init__    s    zState.__init__)returnc                 C   s
   | j dkS Nrestartserver_commandr   r   r   r   need_restart#   s    zState.need_restart)valuer   c                 C   s   |r
d| _ d S r   r   r   r   r   r   r   r   (   s    c                 C   s   | j S r
   )r	   r   r   r   r   r   .   s    zState.server_commandc                 C   s   || _ | j  dS )zR
        Set the server command to `value` and signal that it's been set.
        N)r	   _server_command_signalsetr   r   r   r   r   2   s    )timeoutr   c                 C   s*   | j |r&| j   | j}d| _|S dS )z\
        Wait for server command to get set; return and clear the value and signal.
        N)r   waitclearr	   )r   r   reqr   r   r   wait_for_server_command:   s    
zState.wait_for_server_commandc                 C   s   |    d| _td d S )Nr   zReceived restart request)	interruptr   loginfor   r   r   r   request_restartE   s    zState.request_restartc                 C   s   d| _ td d S )NTzReceived skip request)skippedr"   r#   r   r   r   r   skipJ   s    z
State.skipc                 C   s   d| _ td d S )NTzReceived interrupt request)interruptedr"   r#   r   r   r   r   r!   N   s    zState.interruptc                 C   s:   t jjrt jjdkr|   |  jd7  _d| _d| _d S )N   r   )r   optslive_previews_enableshow_progress_every_n_stepsdo_set_current_imagejob_nosampling_stepcurrent_image_sampling_stepr   r   r   r   nextjobR   s
    zState.nextjobc              	   C   s*   | j | j| j| j| j| j| j| jd}|S )Nr%   r'   job	job_countjob_timestampr.   r/   sampling_stepsr2   )r   objr   r   r   dictZ   s    z
State.dict	(unknown))r3   c                 C   s|   d| _ d| _d| _d| _tj d| _d | _d | _	d| _
d| _d| _d| _d | _t | _|| _t  td| d S )Nr   r(   Fz%Y%m%d%H%M%SzStarting job %s)r/   r4    processing_has_refined_job_countr.   datetimenowstrftimer5   current_latentcurrent_imager0   id_live_previewr%   r'   textinfor   
time_startr3   r   torch_gcr"   r#   )r   r3   r   r   r   beginh   s     
zState.beginc                 C   s6   t   | j }td| j| d| _d| _t  d S )NzEnding job %s (%.2f seconds)r   r   )r   rB   r"   r#   r3   r4   r   rC   )r   durationr   r   r   endz   s
    z	State.endc                 C   s>   t js
dS | j| j t jjkr:t jjr:t jjdkr:|   dS )zif enough sampling steps have been made after the last call to this, sets self.current_image from self.current_latent, and modifies self.id_live_preview accordinglyNr(   )r   parallel_processing_allowedr/   r0   r*   r,   r+   r-   r   r   r   r   set_current_image   s    (zState.set_current_imagec                 C   st   | j d u rd S dd l}z>tjjr6| |j| j  n| |j| j  | j	| _
W n tyn   t  Y n0 d S )Nr   )r>   modules.sd_samplersr   r*   show_progress_gridassign_current_imagesd_samplersZsamples_to_image_gridZsample_to_imager/   r0   	Exceptionr   record_exception)r   modulesr   r   r   r-      s    
zState.do_set_current_imagec                 C   s   || _ |  jd7  _d S )Nr)   )r?   r@   )r   imager   r   r   rK      s    zState.assign_current_image)N)r9   ),__name__
__module____qualname__r%   r'   r3   r.   r4   r:   r5   r/   r6   r>   r?   r0   r@   rA   rB   r   	threadingEventr   r	   r   str__annotations__r   propertyboolr   setterr   floatr    r$   r&   r!   r1   r8   rD   rF   rH   r-   rK   r   r   r   r   r      sL   

r   )r;   loggingrT   r   rO   r   r   r   typingr   	getLoggerrQ   r"   r   r   r   r   r   <module>   s   
