U
    -ep.                     @   sH  d dl mZ ed 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mZmZmZ ejrzedd dlZee Zed ejZdejZdejZed	fd
dZedfddZdZdddZG dd dZ G dd de ej!Z"G dd de ej!Z#G dd de ej!Z$dd Z%e&dkrDe'  dS )    )support_multiprocessingN)make_pkgmake_scriptmake_zip_pkgmake_zip_scriptassert_python_okztest is not helpful for PGOzmultiprocess.synchronizea  # multiprocessing includes all sorts of shenanigans to make __main__
# attributes accessible in the subprocess in a pickle compatible way.

# We run the "doesn't work in the interactive interpreter" example from
# the docs to make sure it *does* work from an executed __main__,
# regardless of the invocation mechanism

import sys
import time
sys.path.extend({0})
from multiprocess import Pool, set_start_method

# We use this __main__ defined function in the map call below in order to
# check that multiprocessing in correctly running the unguarded
# code in child processes and then making it available as __main__
def f(x):
    return x*x

# Check explicit relative imports
if "check_sibling" in __file__:
    # We're inside a package and not in a __main__.py file
    # so make sure explicit relative imports work correctly
    from . import sibling

if __name__ == '__main__':
    start_method = sys.argv[1]
    set_start_method(start_method)
    results = []
    with Pool(5) as pool:
        pool.map_async(f, [1, 2, 3], callback=results.extend)
        start_time = getattr(time,'monotonic',time.time)()
        while not results:
            time.sleep(0.05)
            # up to 1 min to report the results
            dt = getattr(time,'monotonic',time.time)() - start_time
            if dt > 60.0:
                raise RuntimeError("Timed out waiting for results (%.1f sec)" % dt)

    results.sort()
    print(start_method, "->", results)

    pool.join()
a  # __main__.py files have an implied "if __name__ == '__main__'" so
# multiprocessing should always skip running them in child processes

# This means we can't use __main__ defined functions in child processes,
# so we just use "int" as a passthrough operation below

if __name__ != "__main__":
    raise RuntimeError("Should only be called as __main__!")

import sys
import time
sys.path.extend({0})
from multiprocess import Pool, set_start_method

start_method = sys.argv[1]
set_start_method(start_method)
results = []
with Pool(5) as pool:
    pool.map_async(int, [1, 4, 9], callback=results.extend)
    start_time = getattr(time,'monotonic',time.time)()
    while not results:
        time.sleep(0.05)
        # up to 1 min to report the results
        dt = getattr(time,'monotonic',time.time)() - start_time
        if dt > 60.0:
            raise RuntimeError("Timed out waiting for results (%.1f sec)" % dt)

results.sort()
print(start_method, "->", results)

pool.join()
Fc                 C   s.   t | |||}|dkr"t | dd t  |S )Ncheck_siblingZsibling )r   	importlibinvalidate_caches)
script_dirscript_basenamesourceomit_suffix	to_return r   v/var/www/html/Darija-Ai-Train/env/lib/python3.8/site-packages/multiprocess/tests/test_multiprocessing_main_handling.py_make_test_scripto   s     r      c                 C   s   t | |||||}t  |S N)r   r   r   )zip_dirZzip_basenamepkg_namer   r   depthr   r   r   r   _make_test_zip_pkgy   s    
 r   zPimport sys, os.path, runpy
sys.path.insert(0, %s)
runpy._run_module_as_main(%r)
c                 C   s:   |d krd}nt |}t||f }t| ||}t  |S )Nzos.path.dirname(__file__))reprlaunch_sourcer   r   r   )r   r   module_namepathr   r   r   r   r   _make_launch_script   s    r   c                   @   s   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! ZdS )"MultiProcessingCmdLineMixinNc                 C   s   | j tkr| d| j   d S )Nz%r start method not available)start_methodAVAILABLE_START_METHODSZskipTest)selfr   r   r   setUp   s    
z!MultiProcessingCmdLineMixin.setUpc                 C   sb   t dkr td|  tt| | |d | |dd d| j }| |d | d S )Nr   zOutput from test script %r:r   zutf-8r
   z%s -> [1, 4, 9])verboseprintr   assertEqualdecoder!   strip)r#   script_name	exit_codeouterrZexpected_resultsr   r   r   _check_output   s    
z)MultiProcessingCmdLineMixin._check_outputc                 G   s6   ||| jf }t|ddi\}}}| |||| d S )NZ
__isolatedF)sysr!   r   r.   )r#   r*   Zcmd_line_switchesZrun_argsrcr,   r-   r   r   r   _check_script   s    z)MultiProcessingCmdLineMixin._check_scriptc              	   C   s,   t  }t|d}| | W 5 Q R X d S )Nscriptr   temp_dirr   r1   r#   r   r*   r   r   r   test_basic_script   s    

z-MultiProcessingCmdLineMixin.test_basic_scriptc              	   C   s0   t  }t|ddd}| | W 5 Q R X d S )Nr2   T)r   r3   r5   r   r   r   test_basic_script_no_suffix   s
    
z7MultiProcessingCmdLineMixin.test_basic_script_no_suffixc              	   C   sN   t }t 8}t|d|d}| | t|d|dd}| | W 5 Q R X d S )Nipythonr   T)r   r   )$test_source_main_skipped_in_childrenr   r4   r   r1   )r#   r   r   r*   Zscript_no_suffixr   r   r   test_ipython_workaround   s    

z3MultiProcessingCmdLineMixin.test_ipython_workaroundc              	   C   sN   t  <}t|d}tj|dd t| t |}| | W 5 Q R X d S )Nr2   Tdoraise)	r   r4   r   
py_compilecompileosremovemake_legacy_pycr1   )r#   r   r*   pyc_filer   r   r   test_script_compiled   s    



z0MultiProcessingCmdLineMixin.test_script_compiledc              	   C   s6   | j }t }t|d|d}| | W 5 Q R X d S )N__main__r9   )main_in_children_sourcer   r4   r   r1   )r#   r   r   r*   r   r   r   test_directory   s    
z*MultiProcessingCmdLineMixin.test_directoryc              	   C   sX   | j }t @}t|d|d}tj|dd t| t|}| 	| W 5 Q R X d S )NrE   r9   Tr<   )
rF   r   r4   r   r>   r?   r@   rA   rB   r1   )r#   r   r   r*   rC   r   r   r   test_directory_compiled   s    


z3MultiProcessingCmdLineMixin.test_directory_compiledc              	   C   sF   | j }t .}t|d|d}t|d|\}}| | W 5 Q R X d S )NrE   r9   test_zip)rF   r   r4   r   r   r1   )r#   r   r   r*   zip_namerun_namer   r   r   test_zipfile   s    
z(MultiProcessingCmdLineMixin.test_zipfilec              	   C   sT   | j }t <}t|d|d}tj|dd}t|d|\}}| | W 5 Q R X d S )NrE   r9   Tr<   rI   )rF   r   r4   r   r>   r?   r   r1   )r#   r   r   r*   compiled_namerJ   rK   r   r   r   test_zipfile_compiled   s    
z1MultiProcessingCmdLineMixin.test_zipfile_compiledc              	   C   sN   t  <}tj|d}t| t|d}t|dd}| | W 5 Q R X d S )Ntest_pkgr	   launchztest_pkg.check_sibling)	r   r4   r@   r   joinr   r   r   r1   )r#   r   pkg_dirr*   launch_namer   r   r   test_module_in_package   s    

z2MultiProcessingCmdLineMixin.test_module_in_packagec              	   C   sB   t  0}t|ddd\}}t|dd|}| | W 5 Q R X d S )NrI   rO   r2   rP   ztest_pkg.scriptr   r4   r   r   r1   r#   r   rJ   rK   rS   r   r   r   !test_module_in_package_in_zipfile   s    
z=MultiProcessingCmdLineMixin.test_module_in_package_in_zipfilec              	   C   sF   t  4}t|ddddd\}}t|dd|}| | W 5 Q R X d S )NrI   rO   r2      )r   rP   ztest_pkg.test_pkg.scriptrU   rV   r   r   r   $test_module_in_subpackage_in_zipfile   s    
z@MultiProcessingCmdLineMixin.test_module_in_subpackage_in_zipfilec              	   C   sX   | j }t @}tj|d}t| t|d|d}t|dd}| 	| W 5 Q R X d S )NrO   rE   r9   rP   )
rF   r   r4   r@   r   rQ   r   r   r   r1   )r#   r   r   rR   r*   rS   r   r   r   test_package  s    
z(MultiProcessingCmdLineMixin.test_packagec              	   C   sz   | j }t b}tj|d}t| t|d|d}tj	|dd}t
| t|}t|dd}| | W 5 Q R X d S )NrO   rE   r9   Tr<   rP   )rF   r   r4   r@   r   rQ   r   r   r>   r?   rA   rB   r   r1   )r#   r   r   rR   r*   rM   rC   rS   r   r   r   test_package_compiled  s    


z1MultiProcessingCmdLineMixin.test_package_compiled)__name__
__module____qualname__ZmaxDiffr$   r.   r1   r6   r7   r;   rD   rG   rH   rL   rN   rT   rW   rY   rZ   r[   r   r   r   r   r       s"   	
		
r    c                   @   s   e Zd ZdZeZdS )SpawnCmdLineTestspawnNr\   r]   r^   r!   r:   rF   r   r   r   r   r_     s   r_   c                   @   s   e Zd ZdZeZdS )ForkCmdLineTestforkN)r\   r]   r^   r!   test_sourcerF   r   r   r   r   rb   !  s   rb   c                   @   s   e Zd ZdZeZdS )ForkServerCmdLineTestZ
forkserverNra   r   r   r   r   re   %  s   re   c                   C   s   t   d S r   )r   Zreap_childrenr   r   r   r   tearDownModule)  s    rf   rE   )N)(testr   import_moduler   Zimportlib.machineryZunittestr/   r@   os.pathr>   Ztest.support.script_helperr   r   r   r   r   ZPGOZSkipTestZmultiprocessmultiprocessingsetZget_all_start_methodsr"   r%   formatr   rd   r:   r   r   r   r   r    ZTestCaser_   rb   re   rf   r\   mainr   r   r   r   <module>   sJ   


+- % 
 


 

