o
    ҷh                     @   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je	jje	jdgZdd	 Zd
d Zdd ZdS )zd
Tests multithreading behaviour for reading and
parsing files for each parser defined in parsers.py
    )	ExitStack)BytesIO)
ThreadPoolN)	DataFramepyarrow_skipc           	         s   | }d d} fddt |D }t /fdd|D }td}||j|}|d }|D ]}t|| q2W d    d S 1 sFw   Y  d S )	Nd   
   c                 3   s,    | ]}d  dd t D  V  qdS )
c                 S   s&   g | ]}|d d|d d|d qS )d, .0ir   r   [/var/www/html/venv/lib/python3.10/site-packages/pandas/tests/io/parser/test_multi_thread.py
<listcomp>    s   & zBtest_multi_thread_string_io_read_csv.<locals>.<genexpr>.<listcomp>N)joinrangeencode)r   _)max_row_ranger   r   	<genexpr>   s
    
z7test_multi_thread_string_io_read_csv.<locals>.<genexpr>c                    s   g | ]	}  t|qS r   )enter_contextr   )r   b)stackr   r   r   &   s    z8test_multi_thread_string_io_read_csv.<locals>.<listcomp>   r   )r   r   r   r   mapread_csvtmassert_frame_equal)	all_parsersparser	num_filesbytes_to_dffilespoolresults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 s)w   Y  |d j}|dd D ]}	||	_q9t|}
|
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   date)	index_colheadernrowsparse_datesN   	   )r+   r,   skiprowsr-   r.   )r   int)argstartr-   )r!   pathr   r   readerE   s   
z0_generate_multi_thread_dataframe.<locals>.readerc                    s    g | ]} |    fqS r   r   r   )num_rows	num_tasksr   r   r   g   s    z4_generate_multi_thread_dataframe.<locals>.<listcomp>)	processesNr   r/   )r   r   r   columnspdconcat)r!   r5   r7   r8   r6   tasksr%   r&   r,   rfinal_dataframer   )r7   r8   r!   r5   r    _generate_multi_thread_dataframe1   s   "

r@   c                 C   s   d}d}| }d}t tjd|tjd|tjd|tjd|tjd|dg| dg| dg| tjd|d	d
tj|ddd
}t|}|	| t
||||}t|| W d    d S 1 sqw   Y  d S )N   0   z__thread_pool_reader__.csv   foobarbazz20000101 09:00:00s)periodsfreqint64)dtype)
ar   cr
   erD   rE   rF   r*   r2   )r   nprandomdefault_rngr;   
date_rangearanger   ensure_cleanto_csvr@   r   )r    r8   r7   r!   	file_namedfr5   r?   r   r   r   )test_multi_thread_path_multipart_read_csvw   s0   
"rX   )__doc__
contextlibr   ior   multiprocessing.poolr   numpyrO   pytestpandasr;   r   pandas._testing_testingr   mark
single_cpuslowusefixtures
pytestmarkr)   r@   rX   r   r   r   r   <module>   s     
F