
    0Fie?              &         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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 d dlZd dlmZ d d	lmZ d d
lmZ d dlmZ d dlmZ d dlm Z  d dl!m"Z" d dl#m$Z$ d dl%m&Z&m'Z' d dl(m)Z)m*Z*  ej+        d          Z, ej-        dd          Z. ej/        d e0d                     ej1        de2d           ej3        de.dd           ej3        de.dd           ej3        d e.dd!           ej3        d"dd#$           ej3        d%dd&$           ej3        d'e2d(d)           ej3        d*d+ddd,-           ej3        d.e2dd/           ej3        d0e2dd1           ej3        d2e2dd3           ej3        d4e2dd5           ej3        d6e2dd7           ej3        d8e4d d9           ej3        d:d;e2ddd<=           ej3        d>e2dd?           ej3        d@dAddBC           ej3        dDddE$           ej3        dFe2dGdH           ej3        dIde2dJK           ej3        dLe2ddM           ej3        dNe2ddO           ej3        dPe2ddQ           ej3        dRe2dGdS           ej3        dTe2ddU           ej3        dVe2ddW           ej3        dXe2dYddZ=           ej3        d[d\ddd]-           ej3        d^e2ddd_`           ej1        dadbej5        e$c           ej3        dde2ddde`           ej3        dfe2ddg           ej6                    dsdk                                                                                                                                                                                                                                                                                                                                                                                                                        Z7dtdqZ8e9drk    r e7             dS dS )u    )annotationsN)Iterator)suppress)Any)valmap)TimeoutError)	CPU_COUNT)Nanny)wait_for_signals)get_address_host_port)asyncio_run)get_loop_factory)nprocesses_nthreads)validate_preload_argv)enable_proctitle_on_childrenenable_proctitle_on_current)import_termparse_portszdistributed.dask_workerT)existsresolve_pathworker)ignore_unknown_options)namecontext_settings	schedulerF)typerequiredz--tls-ca-filez'CA cert(s) file for TLS (in PEM format))r   defaulthelpz
--tls-certz(certificate file for TLS (in PEM format)z	--tls-keyz(private key file for TLS (in PEM format)z--worker-porta-  Serving computation port, defaults to random. When creating multiple workers with --nworkers, a sequential range of worker ports may be used by specifying the first and last available ports like <first-port>:<last-port>. For example, --worker-port=3000:3026 will use ports 3000, 3001, ..., 3025, 3026.)r   r   z--nanny-porta%  Serving nanny port, defaults to random. When creating multiple nannies with --nworkers, a sequential range of nanny ports may be used by specifying the first and last available ports like <first-port>:<last-port>. For example, --nanny-port=3000:3026 will use ports 3000, 3001, ..., 3025, 3026.z--dashboard-addressz:0z4Address on which to listen for diagnostics dashboardz--dashboard/--no-dashboard	dashboardz+Launch the Dashboard [default: --dashboard])r   r   r   z--listen-addressz_The address to which the worker binds. Example: tcp://0.0.0.0:9000 or tcp://:9000 for IPv4+IPv6z--contact-addressz}The address the worker advertises to the scheduler for communication with it and other workers. Example: tcp://127.0.0.1:9000z--hostzServing host. Should be an ip address that is visible to the scheduler and other workers. See --listen-address and --contact-address if you need different listen and contact addresses. See --interface.z--interfacez&Network interface like 'eth0' or 'ib0'z
--protocolzProtocol like tcp, tls, or ucxz
--nthreadszNumber of threads per process.z
--nworkers	n_workerszNumber of worker processes to launch. If negative, then (CPU_COUNT + 1 + nworkers) is used. Set to 'auto' to set nworkers and nthreads dynamically based on CPU_COUNT)r   r   show_defaultr   z--namezA unique name for this worker like 'worker-1'. If used with --nworkers then the process number will be appended like name-0, name-1, name-2, ...z--memory-limitautoa-  
    Bytes of memory per process that the worker can use.
    This can be:
    - an integer (bytes), note 0 is a special case for no memory management.
    - a float (fraction of total system memory).
    - a string (like 5GB or 5000M).
    - 'auto' for automatically computing the memory limit.
    )r   r"   r   z--nanny/--no-nannyz@Start workers in nanny process for management [default: --nanny]z
--pid-file zFile to write the process PIDz--local-directoryzDirectory to place worker files)r   r   r   z--resourceszResources for task constraints like "GPU=2 MEM=10e9". Resources are applied separately to each worker process (only relevant when starting multiple worker processes with '--nworkers').z--scheduler-filezXFilename to JSON encoded scheduler information. Use with dask scheduler --scheduler-filez--death-timeoutz.Seconds to wait for a scheduler before closingz--dashboard-prefixzPrefix for the dashboardz
--lifetimez6If provided, shut down the worker after this duration.z--lifetime-staggerz1Random amount by which to stagger lifetime valuesz--worker-classzdask.distributed.Workerz.Worker class used to instantiate workers from.z(--lifetime-restart/--no-lifetime-restartlifetime_restartzWhether or not to restart the worker after the lifetime lapses. This assumes that you are using the --lifetime and --nanny keywordsz	--preloadzWModule that should be loaded by each worker process like "foo.bar" or "/path/to/foo.py")r   multipleis_eagerr   preload_argv)nargsr   callbackz--preload-nannyzNModule that should be loaded by each nanny like "foo.bar" or "/path/to/foo.py"z--scheduler-sniz4Scheduler SNI (if different from scheduler hostname)worker_port
str | None
nanny_portc                	   	
 ! dt           j        d         v rt          j        dt          d           t          j                    \  }}}t          j        |dz  |dz  |dz             t                       t                       d d|fd	|fd
|ffD             dk    rt                      \  ndnt                    dk     rt          dz   z   dk    r.t                              d           t          j        d           dk    r0s.t                              d           t          j        d           r0|s.t                              d           t          j        d           dk    r0|r.t                              d           t          j        d           |sr0|r.t                              d           t          j        d           	 |r-t!          |d          \  }t#          |          }dv rd drt!          d          \  }}n|nU# t$          $ rH}t                              dt#          |          z              t          j        d           Y d}~nd}~ww xY ws
t          z  
rtt'          
d          5 }|                    t#          t+          j                                         ddd           n# 1 swxY w Y   
fd}t/          j        |           rW                    dd                                          t7          d D                       t9          t:                    ndt=          |          }t?          ||          tA                    k    sJ r|d<   |d<   |d<   tB          !n|! s1s/tD          j#        $                    d d          t%          d!          tK          tL          t$                    5  t          	          	ddd           n# 1 swxY w Y   d" 	  !fd#}	 tO           |            tQ                      $           nL# tR          tT          j)        f$ r3  st          +                    d%           t          j        d           Y nw xY wt          +                    d&           dS # t          +                    d&           w xY w)'z6Launch a Dask worker attached to an existing schedulerzdask-workerr   z\dask-worker is deprecated and will be removed in a future release; use `dask worker` instead   )
stacklevel   c                    i | ]
\  }}|||S N ).0kvs      ;lib/python3.11/site-packages/distributed/cli/dask_worker.py
<dictcomp>zmain.<locals>.<dictcomp>  s/       Aq
 = 	
1 ==    tls_ca_filetls_worker_certtls_worker_keyr#   NzVFailed to launch worker. Must specify --nworkers so that there's at least one process.zTFailed to launch worker.  You cannot use the --no-nanny argument when n_workers > 1.zVFailed to launch worker. Must specify --listen-address when --contact-address is givenzPFailed to launch worker. You cannot specify --listen-address when n_workers > 1.zcFailed to launch worker. You cannot specify --listen-address when --worker-port or --host is given.T)strict:[]zFailed to launch worker. wc                 r    t           j                                       rt          j                    d S d S r4   )ospathr   remove)pid_files   r9   del_pid_filezmain.<locals>.del_pid_filed  s9    w~~h'' $	(#####$ $r;   , c              3  @   K   | ]}|                     d           V  dS )=N)split)r6   pairs     r9   	<genexpr>zmain.<locals>.<genexpr>l  s,      ??TC??????r;   worker_classpreload_nannylisten_addresszscheduler-addresszINeed to provide scheduler address like
dask worker SCHEDULER_ADDRESS:8786Fc                 R  K   	
fdt                    D             fd} fd}t          j         |                      }t          j         |                       }t          j        ||gt          j                   d {V \  }}d |D              d S )Nc                    g | ]J\  }} f
d k    s		dk    r	n!t          	          dz   t          |          z   d	|KS )r0   Nr$   -)	scheduler_filenthreads	resourcessecuritycontact_addresshostr    dashboard_addressr   )str)r6   iport_kwargs_ir[   r    r]   r\   kwargsr!   r   rX   rY   r   rW   sects      r9   
<listcomp>z%main.<locals>.run.<locals>.<listcomp>  s     
 
 
" !=! A-!# /#"3>>T\TRZZ TYY_s1vv-     
 
 
r;   c                 p   K   t          j           d{V  t          j        d  D               d{V  dS )z-Wait for all nannies to initialize and finishNc              3  >   K   | ]}|                                 V  d S r4   )finished)r6   ns     r9   rP   zHmain.<locals>.run.<locals>.wait_for_nannies_to_finish.<locals>.<genexpr>  s*      "A"AA1::<<"A"A"A"A"A"Ar;   )asynciogather)nanniess   r9   wait_for_nannies_to_finishz5main.<locals>.run.<locals>.wait_for_nannies_to_finish  sZ      .'********."A"A"A"A"ABBBBBBBBBBr;   c                     K   t                       d{V  dr"t          j         fdD               d{V  dS dS )zTWait for SIGINT or SIGTERM and close all nannies upon receiving one of those signalsNTc              3  L   K   | ]}|                     d d           V  dS )
   zsignal-)timeoutreasonN)close)r6   rh   signums     r9   rP   zHmain.<locals>.run.<locals>.wait_for_signals_and_close.<locals>.<genexpr>  s<      VVaggb1C61C1CgDDVVVVVVr;   )r   ri   rj   )rs   rk   nannysignal_fireds   @r9   wait_for_signals_and_closez5main.<locals>.run.<locals>.wait_for_signals_and_close  s       ,--------FL nVVVVgVVV          r;   )return_whenc                6    g | ]}|                                 S r5   )result)r6   tasks     r9   rd   z%main.<locals>.run.<locals>.<listcomp>  s     (((4(((r;   )	enumerateri   create_taskwaitFIRST_COMPLETED)rl   rv   wait_for_signals_and_close_taskwait_for_nannies_to_finish_taskdone_rk   r[   r    r]   r\   ra   r!   r   rt   rX   port_kwargsrY   r   rW   rb   ru   rc   s         @r9   runzmain.<locals>.run  s^     
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
" %.k$:$:#
 
 
(	C 	C 	C 	C 	C

	 
	 
	 
	 
	 
	 
	 +2*=&&((+
 +
' +2*=&&((+
 +
'  ,.MN/
 
 
 
 
 
 
 
 
a
 	)(4((((((r;   )loop_factoryzTimed out starting workerz
End worker),sysargvwarningswarnFutureWarninggcget_thresholdset_thresholdr   r   r   intr	   loggererrorexitr   r^   
ValueErroropenwriterE   getpidatexitregisterreplacerN   dictr   floatr   _apportion_portslenr
   daskconfiggetr   	TypeErrorr   r   r   ri   info)"r   r\   r,   rS   r[   r.   rX   r!   rt   r   rH   rY   r    rW   dashboard_prefixr<   tls_certtls_keyr]   rQ   rR   ra   g0g1g2r   efrI   r   r   rb   ru   rc   s"   ``  ` ````````    `  `        @@@@r9   mainr   (   sR   B ##j	
 	
 	
 	
 !##JBBR!VR!VR!V,,,!!! """  K()w'
  C F133	88					NN	1}}MI-	A~~d	
 	
 	
 	1}}U}b	
 	
 	
 	 ~ L	
 	
 	
 	1}}}F	
 	
 	
 	 t  Y	
 	
 	
 	 	#+N4HHHGD!a&&Kd{{"4{{{ 	-*?4HHHFQ -O   03q669:::  *	) &(C   	&AGGC	$$%%%	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	&	$ 	$ 	$ 	$ 	$ 	%%% %%c3//5577	??Y?????	5),,			|,,L";
IuMMK{y(((( !-~"/#1  

 KOO/66>1
 
 	

 
)Z	(	(  4yy               L2) 2) 2) 2) 2) 2) 2) 2) 2) 2) 2) 2) 2) 2) 2) 2) 2) 2) 2) 2)h"CCEE(8(:(:;;;;;'./    	5KK3444	 	L!!!!!L!!!!sh   /AH8 8
J
>JJ
+4K++K/2K/P$$P(+P(%Q, +S ,AR52S 4R55S S0r   rt   boolreturnlist[dict[str, Any]]c           
     n   t                      dfd} ||           } ||          }d t                    D             }t          |          }d}	d}
|	s|
r	 t          |          \  }}n# t          $ r t          |          }Y 3w xY w	 |                    t          |                     n# t          $ r d}	Y nw xY w	 |                    t          |                     n# t          $ r d}
Y nw xY w|	|
g }|D ]\  }}|r|s1|rt          d	|  d
| d d          t          d	|  d d          t          |          }t          |          dk    r|d         }|r0t          |          }t          |          dk    r|d         }||d}nd|i}|	                    |           |S )zSpread out evenly --worker-port and/or --nanny-port ranges to the workers and
    nannies, avoiding overlap.

    Returns
    =======
    List of kwargs to pass to the Worker or Nanny constructors
    sr-   r   Iterator[int | None]c              3     K   t          |           }|dgd gfv rt                    D ]}|d         V  d S |D ]}|vr                    |           |V   d S )Nr   )r   rangeadd)r   portsr   portr!   seens       r9   parse_uniquez&_apportion_ports.<locals>.parse_unique  s      AaS4&M!!9%%  Ah    t##HHTNNNJJJ r;   c                F    g | ]}t                      t                      fS r5   )set)r6   r   s     r9   rd   z$_apportion_ports.<locals>.<listcomp>  s1     < < << < <r;   TFz(Not enough ports in range --worker_port z --nanny_port z for z workersr0   r   )r   r,   r   )r   r-   r   r   )
r   r   iternextStopIterationr   r   sortedr   append)r,   r.   r!   rt   r   worker_ports_iternanny_ports_iterr   
ports_itermore_wpsmore_npsworker_ports_inanny_ports_ira   wpnpkwargs_ir   s     `              @r9   r   r     s    55D	 	 	 	 	 	 	 %[11#|J//< < %i 0 0< < <E eJHH
 h 	,0,<,<)NMM 	 	 	eJH	
	t$5667777 	 	 	HHH		d#3445555 	 	 	HHH	  h " F).    % 
	] 
	 	 I{ I I$.I I5>I I I  
 !/{ / /$/ / /   ((r77a<<AB 	$]++B2ww!||U "266HH|HhMs6   A2 2BB"B5 5CC"C+ +C:9C:__main__)r,   r-   r.   r-   )
r,   r-   r.   r-   r!   r   rt   r   r   r   ):
__future__r   ri   r   r   loggingrE   r   r   collections.abcr   
contextlibr   typingr   clicktlzr   tornado.ioloopr   r   dask.systemr	   distributedr
   distributed._signalsr   distributed.commr   distributed.compatibilityr   distributed.configr   distributed.deploy.utilsr   distributed.preloadingr   distributed.proctitler   r   distributed.utilsr   r   	getLoggerr   Pathpem_file_option_typecommandr   argumentr^   optionr   UNPROCESSEDversion_optionr   r   __name__r5   r;   r9   <module>r      s   " " " " " "   				  				 



  $ $ $ $ $ $                    ' ' ' ' ' '  ! ! ! ! ! !       1 1 1 1 1 1 2 2 2 2 2 2 1 1 1 1 1 1 / / / / / / 8 8 8 8 8 8 8 8 8 8 8 8        7 6 6 6 6 6 6 6		4	5	5 "uzDAAA  Htt4/P/P/PQQQ#666		2	   		3	   		3	   
2   
2   		?	    	6   		j	   	
$	   	
		 	 	 T0X   sD/O   la6VWWW	
P	 	 	 	
8	   
	   	K  
 lb7VWWWC6W   	
Q	   	
/	   		9	   sB5O   		A	   		<	   	%	9   .
J   	
*   "5#4?T   	
*   		?	   Z" Z" Z"               XW   	 	 XW  	 	          76 RQNZ"zP P P Pf zDFFFFF r;   