U
    +if                     @   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foobarZbazz20000101 09:00:00s)ZperiodsfreqdateZint64)Zdtypeint)r   nprandomZrandlistpdZ
date_rangeZarange)num_rowsdf r   [/home/mars/bis/venv/lib/python3.8/site-packages/pandas/tests/io/parser/test_multi_thread.py_construct_dataframe   s    r   c           	   	      s   | }d d} fddt |D }t Rfdd|D }td}||j|}|d }|D ]}t|| qdW 5 Q R X 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   s     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   s        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    
r1   c              	      sx   fdd} fddt D }td}|||}W 5 Q R X |d j}|dd D ]
}	||	_q^t|}
|
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   	   )r2   r3   Zskiprowsr4   r5   )r(   r   )argstartr4   )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	processesr   r6   N)r   r   r'   r   r   concat)r,   r:   r   r<   r;   tasksr.   r/   r3   rfinal_dataframer   )r   r<   r,   r:   r    _generate_multi_thread_dataframeD   s    "

rA   c              	   C   sV   d}d}| }d}t |}t|*}|| t||||}t|| W 5 Q R X d S )N   i z__thread_pool_reader__.csv)r   r)   Zensure_cleanZto_csvrA   r*   )r+   r<   r   r,   	file_namer   r:   r@   r   r   r   )test_multi_thread_path_multipart_read_csv   s    
   rD   )__doc__
contextlibr   ior   Zmultiprocessing.poolr   numpyr   ZpytestZpandasr   r   Zpandas._testingZ_testingr)   markZusefixturesZ
pytestmarkr   Zslowr1   rA   rD   r   r   r   r   <module>   s   
F