B
    }d                 @   sT   d 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
 Zdd ZdS )zMulti-processing utilities for input/output

This module provides the `read_multi` method, which enables spreading
reading multiple files across multiple cores, returning a flattened result.
    )SAXException)read   )get_read_format)	file_list   )mpc                s$   dd}yt|}W n tk
r6   |g}d}Y nX |rD|d nd}|s^tdj ddddkrt||f  d< t dd	t||d
krdt	 d} fdd|D }t
jt||dd}	x2|	D ]*\}
}t|trd|
 d| f|_|qW t|	 \}}| |S )a  Read sources into a `cls` with multiprocessing

    This method should be called by `cls.read` and uses the `nproc`
    keyword to enable and handle pool-based multiprocessing of
    multiple source files, using `flatten` to combine the
    chunked data into a single object of the correct type.

    Parameters
    ----------
    flatten : `callable`
        a method to take a list of ``cls`` instances, and combine them
        into a single ``cls`` instance

    cls : `type`
        the object type to read

    source : `str`, `list` of `str`, ...
        the input data source, can be of in many different forms

    *args
        positional arguments to pass to the reader

    **kwargs
        keyword arguments to pass to the reader
    verboseFNr   zcannot read z from empty source listformatnprocr   Tz	Reading ()c                s   g | ]}| fqS  r   ).0f)argsclskwargsr   r   W/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/gwpy/io/mp.py
<listcomp>W   s    zread_multi.<locals>.<listcomp>files)r	   unitzFailed to read z: )popr   
ValueError
IndexError__name__getr   minlenr
   mp_utilsZmultiprocess_with_queues_read_single_file
isinstance	Exceptionr   zip)flattenr   sourcer   r   r	   r   pathinputsoutputfobjexc_outr   )r   r   r   r   r   
read_multi"   s.    

r,   c          
   C   sp   | \}}}}}y|t ||f||fS  tk
rj } z(|dkr@ t|trV|| fS ||fS d}~X Y nX dS )aO  Reads a single file and returns the output

    This is designed only to be passed to
    :func:`gwpy.utils.mp.multiprocess_with_queues`

    Returns
    -------
    f, output :
        the input file (object) that was (attempted to be) read, and
        the result of the read operation, which may be an exception
        object.
    r   N)io_readr!   r    r   ZgetException)Zbundler(   r   r   r   r   r)   r   r   r   r   h   s    
r   N)__doc__Zxml.saxr   Zastropy.io.registryr   r-   registryr   utilsr   r   r   r,   r   r   r   r   r   <module>   s   F