a
    Gb                     @   s   d Z ddlmZ ddlmZ ddlmZ ddlZddl	Z	ddl
Zddl
mZ ddlmZ e	jdZdd	 Ze	jjd
d Zdd Ze	jjdd ZdS )zd
Tests multithreading behaviour for reading and
parsing files for each parser defined in parsers.py
    )	ExitStack)BytesIO)
ThreadPoolN)	DataFrameZpyarrow_skipc                 C   s\   t tj| dtdd}d|d< d|d< d|d< tjd| dd	|d
< tj| dd|d< |S )z
    Construct a DataFrame for testing.

    Parameters
    ----------
    num_rows : int
        The number of rows for our DataFrame.

    Returns
    -------
    df : DataFrame
       Zabcde)columnsZfooZbarZbazz20000101 09:00:00s)ZperiodsZfreqdateZint64)Zdtypeint)r   npZrandomZrandlistpdZ
date_rangeZarange)num_rowsdf r   Glib/python3.9/site-packages/pandas/tests/io/parser/test_multi_thread.py_construct_dataframe   s    r   c           	         s   | }d d} fddt |D }t \fdd|D }td}||j|}|d }|D ]}t|| qdW d    n1 s0    Y  d S )Ni'  d   c                    s(   g | ] }d  dd t D  qS )
c                 S   s&   g | ]}|d d|d d|d qS )d,r   .0ir   r   r   
<listcomp>3       zCtest_multi_thread_string_io_read_csv.<locals>.<listcomp>.<listcomp>)joinrangeencode)r   _)max_row_ranger   r   r   2   s   z8test_multi_thread_string_io_read_csv.<locals>.<listcomp>c                    s   g | ]}  t|qS r   )enter_contextr   )r   b)stackr   r   r   9   r      r   )r   r   r!   r   mapread_csvtmassert_frame_equal)	all_parsersparserZ	num_filesZbytes_to_dffilespoolresultsZfirst_resultresultr   )r    r#   r   $test_multi_thread_string_io_read_csv+   s    
r/   c                    s   fdd} fddt D }td}|||}W d   n1 sR0    Y  |d j}|dd D ]
}	||	_qrt|}
|
S )	a  
    Generate a DataFrame via multi-thread.

    Parameters
    ----------
    parser : BaseParser
        The parser object to use for reading the data.
    path : str
        The location of the CSV file to read.
    num_rows : int
        The number of rows to read per task.
    num_tasks : int
        The number of tasks to use for reading this DataFrame.

    Returns
    -------
    df : DataFrame
    c                    sB   | \}}|s" j dd|dgdS  j ddt|d |dgdS )aj  
        Create a reader for part of the CSV.

        Parameters
        ----------
        arg : tuple
            A tuple of the following:

            * start : int
                The starting row to start for parsing CSV
            * nrows : int
                The number of rows to read.

        Returns
        -------
        df : DataFrame
        r   r	   )	index_colheadernrowsparse_datesN   	   )r0   r1   Zskiprowsr2   r3   )r&   r
   )argstartr2   )r*   pathr   r   readerX   s    
z0_generate_multi_thread_dataframe.<locals>.readerc                    s    g | ]} |    fqS r   r   r   )r   	num_tasksr   r   r   z   s   z4_generate_multi_thread_dataframe.<locals>.<listcomp>)Z	processesNr   r4   )r   r   r%   r   r   concat)r*   r8   r   r:   r9   Ztasksr,   r-   r1   rfinal_dataframer   )r   r:   r*   r8   r    _generate_multi_thread_dataframeD   s    "*

r>   c                 C   sj   d}d}| }d}t |}t|4}|| t||||}t|| W d    n1 s\0    Y  d S )N   i z__thread_pool_reader__.csv)r   r'   Zensure_cleanZto_csvr>   r(   )r)   r:   r   r*   	file_namer   r8   r=   r   r   r   )test_multi_thread_path_multipart_read_csv   s    
rA   )__doc__
contextlibr   ior   Zmultiprocessing.poolr   Znumpyr   ZpytestZpandasr   r   Zpandas._testingZ_testingr'   ZmarkZusefixturesZ
pytestmarkr   Zslowr/   r>   rA   r   r   r   r   <module>   s   
F