o
    Nrf!                     @  s  U 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
m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mZ d d	lmZmZ d d
lmZ d dlmZ d dl m!Z! dd Z"ee#e$j%e" eej&ej'dZ(ej)Z*dd Z+G dd de,Z-i Z.de/d< d0ddZ0zd dl1Z2e2j34  dd Z5W n e6y   dd Z5d1ddZY nw d d! Z7d"Z8d#d$ Z9				%			d2d3d*d+Z:d,d- Z;d1d.d/Z<dS )4    )annotationsN)MappingSequence)ProcessPoolExecutor)partial)warn)config)MultiprocessingPoolExecutor	get_asyncreraise)cullfuse)	CPU_COUNT)Key)ensure_dictc                 C  s   t | j| jffS N)getattr__objclass____name__)m r   ]/var/www/html/software/conda/envs/catlas/lib/python3.10/site-packages/dask/multiprocessing.py_reduce_method_descriptor      r   )protocolc                   C  s
   t  jS r   )multiprocessingcurrent_processidentr   r   r   r   _process_get_id$   s   
r   c                   @  s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )RemoteExceptionzXRemote Exception

    Contains the exception and traceback from a remotely run task
    c                 C  s   || _ || _d S r   )	exception	traceback)selfr    r!   r   r   r   __init__;   s   
zRemoteException.__init__c                 C  s   t | jd | j S )Nz

Traceback
---------
)strr    r!   r"   r   r   r   __str__?   s   zRemoteException.__str__c                 C  s(   t ttt| t| j t| j S r   )sortedsetdirtypelist__dict__r    r%   r   r   r   __dir__B   s   (zRemoteException.__dir__c                 C  s.   zt | |W S  ty   t| j| Y S w r   )object__getattribute__AttributeErrorr   r    )r"   keyr   r   r   __getattr__E   s
   zRemoteException.__getattr__N)r   
__module____qualname____doc__r#   r&   r-   r2   r   r   r   r   r   5   s    r   z&dict[type[Exception], type[Exception]]
exceptionsexc	Exceptionreturnc                 C  sr   t | tv rtt |  }|| |S zt | jjtt | fdt | i}|tt | < || |W S  ty8   |  Y S w )z6Metaclass that wraps exception type in RemoteExceptionZexception_type)r*   r6   	__class__r   r   	TypeError)r7   tbtypr   r   r   remote_exceptionO   s   


r>   c                 C  s   | S r   r   r<   r   r   r   _pack_tracebackf   s   r@   c                 C  s   d t| S )N )joinr!   	format_tbr?   r   r   r   r@   k   r   c                 C  s   t | |} | r   )r>   )r7   r<   r   r   r   r   n   s   
r   c              
   C  sr   t  \}}}t|}z	|| |f}W |S  ty8 }  zt  \}}}t|}|| |f}W Y d } ~ |S d } ~ ww r   )sysexc_infor@   BaseException)edumpsexc_type	exc_valueexc_tracebackr<   resultr   r   r   pack_exceptions   s   rM   zThe 'multiprocessing.context' configuration option will be ignored on Python 2
and on Windows, because they each only support a single context.
c                  C  s6   t dd} tjdkr| dkrttt tS t| S )z+Return the current multiprocessing context.zmultiprocessing.contextZspawnwin32)	r   getrD   platformr   _CONTEXT_UNSUPPORTEDUserWarningr   get_context)Zcontext_namer   r   r   rS      s   


rS   Tdskr   keysSequence[Key] | Keyc	              	   K  sZ  |pt dd}|pt dd}|pt dd}|p!t ddp!t}|du rFtjddv r3d	tjd< t }
tt|d
}t||
|d}d}n|durNt	d t
|tjjrYt|}d}t| } t| |\}}|rqt|||\}}n|}|p|t ddp|t}|pt ddpt}zt|j|j||ft||tt|d|	}W |r|  |S |r|  w w )a   Multiprocessed get function appropriate for Bags

    Parameters
    ----------
    dsk : dict
        dask graph
    keys : object or list
        Desired results from graph
    num_workers : int
        Number of worker processes (defaults to number of cores)
    func_dumps : function
        Function to use for function serialization (defaults to cloudpickle.dumps)
    func_loads : function
        Function to use for function deserialization (defaults to cloudpickle.loads)
    optimize_graph : bool
        If True [default], `fuse` is applied to the graph before computation.
    pool : Executor or Pool
        Some sort of `Executor` or `Pool` to use
    initializer: function
        Ignored if ``pool`` has been set.
        Function to initialize a worker process before running any tasks in it.
    chunksize: int, optional
        Size of chunks to use when dispatching work.
        Defaults to 5 as some batching is helpful.
        If -1, will be computed to evenly divide ready work across workers.
    	chunksize   poolNzmultiprocessing.initializernum_workersZPYTHONHASHSEED)N0Z6640Zuser_initializer)Z
mp_contextinitializerTzThe ``initializer`` argument is ignored when ``pool`` is provided. The user should configure ``pool`` with the needed ``initializer`` on creation.F
func_loads
func_dumps)Zget_idrH   loadsrM   Zraise_exceptionrW   )r   rO   r   osenvironrS   r   initialize_worker_processr   r   
isinstancer   rY   ZPoolr	   r   r   r   _loads_dumpsr
   submitZ_max_workersr   rM   r   shutdown)rT   rU   rZ   r^   r_   Zoptimize_graphrY   r]   rW   kwargscontextcleanupZdsk2dependenciesZdsk3r`   rH   rL   r   r   r   rO      sb   &

rO   c                  C  s&   t jd} | d ur| j  d S d S )Nnumpy)rD   modulesrO   randomseed)npr   r   r   default_initializer   s   rr   c                 C  s   t   | dur|   dS dS )zE
    Initialize a worker process before running any tasks in it.
    N)rr   r\   r   r   r   rc     s   
rc   )r7   r8   r9   r8   r   )NNNTNNN)rT   r   rU   rV   )=
__future__r   copyregr   Zmultiprocessing.poolra   picklerD   r!   collections.abcr   r   concurrent.futuresr   	functoolsr   warningsr   ZcloudpickleZdaskr   Z
dask.localr	   r
   r   Zdask.optimizationr   r   Zdask.systemr   Zdask.typingr   Z
dask.utilsr   r   r*   r(   unionrH   HIGHEST_PROTOCOLrf   r`   re   r   r8   r   r6   __annotations__r>   Ztblib.pickling_supportZtblibZpickling_supportinstallr@   ImportErrorrM   rQ   rS   rO   rr   rc   r   r   r   r   <module>   s`    


j