a
    ze-                     @   s6  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  ejZdZd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r2e%  dS )    )support_multiprocessingN)make_pkgmake_scriptmake_zip_pkgmake_zip_scriptassert_python_okztest is not helpful for PGOa  # 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
from multiprocessing 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 = time.monotonic()
        while not results:
            time.sleep(0.05)
            # up to 1 min to report the results
            dt = time.monotonic() - 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
from multiprocessing 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 = time.monotonic()
    while not results:
        time.sleep(0.05)
        # up to 1 min to report the results
        dt = time.monotonic() - 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   =/usr/lib/python3.9/test/test_multiprocessing_main_handling.py_make_test_scriptm   s    r      c                 C   s   t | |||||}t  |S N)r   r   r   )Zzip_dirZzip_basenameZpkg_namer   r   depthr   r   r   r   _make_test_zip_pkgw   s
    
r   zPimport sys, os.path, runpy
sys.path.insert(0, %s)
runpy._run_module_as_main(%r)
c                 C   s:   |d u 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v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   ZassertEqualdecoder   strip)r!   script_nameZ	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 d    n1 s20    Y  d S )Nscriptr   temp_dirr   r-   r!   r   r'   r   r   r   test_basic_script   s    

z-MultiProcessingCmdLineMixin.test_basic_scriptc                 C   sD   t  (}t|ddd}| | W d    n1 s60    Y  d S )Nr.   T)r   r/   r1   r   r   r   test_basic_script_no_suffix   s
    
z7MultiProcessingCmdLineMixin.test_basic_script_no_suffixc                 C   sb   t }t B}t|d|d}| | t|d|dd}| | W d    n1 sT0    Y  d S )NZipythonr   T)r   r   )$test_source_main_skipped_in_childrenr   r0   r   r-   )r!   r   r   r'   Zscript_no_suffixr   r   r   test_ipython_workaround   s    

z3MultiProcessingCmdLineMixin.test_ipython_workaroundc                 C   sb   t  F}t|d}tj|dd t| t |}| | W d    n1 sT0    Y  d S )Nr.   Tdoraise)	r   r0   r   
py_compilecompileosremovemake_legacy_pycr-   )r!   r   r'   pyc_filer   r   r   test_script_compiled   s    



z0MultiProcessingCmdLineMixin.test_script_compiledc                 C   sJ   | j }t (}t|d|d}| | W d    n1 s<0    Y  d S )N__main__r4   )main_in_children_sourcer   r0   r   r-   )r!   r   r   r'   r   r   r   test_directory   s    
z*MultiProcessingCmdLineMixin.test_directoryc                 C   sl   | j }t J}t|d|d}tj|dd t| t|}| 	| W d    n1 s^0    Y  d S )Nr@   r4   Tr7   )
rA   r   r0   r   r9   r:   r;   r<   r=   r-   )r!   r   r   r'   r>   r   r   r   test_directory_compiled   s    


z3MultiProcessingCmdLineMixin.test_directory_compiledc                 C   sZ   | j }t 8}t|d|d}t|d|\}}| | W d    n1 sL0    Y  d S )Nr@   r4   test_zip)rA   r   r0   r   r   r-   )r!   r   r   r'   zip_namerun_namer   r   r   test_zipfile   s    
z(MultiProcessingCmdLineMixin.test_zipfilec                 C   sh   | j }t F}t|d|d}tj|dd}t|d|\}}| | W d    n1 sZ0    Y  d S )Nr@   r4   Tr7   rD   )rA   r   r0   r   r9   r:   r   r-   )r!   r   r   r'   compiled_namerE   rF   r   r   r   test_zipfile_compiled   s    
z1MultiProcessingCmdLineMixin.test_zipfile_compiledc                 C   sb   t  F}tj|d}t| t|d}t|dd}| | W d    n1 sT0    Y  d S )Ntest_pkgr	   launchztest_pkg.check_sibling)	r   r0   r;   r   joinr   r   r   r-   )r!   r   pkg_dirr'   launch_namer   r   r   test_module_in_package   s    

z2MultiProcessingCmdLineMixin.test_module_in_packagec                 C   sV   t  :}t|ddd\}}t|dd|}| | W d    n1 sH0    Y  d S )NrD   rJ   r.   rK   ztest_pkg.scriptr   r0   r   r   r-   r!   r   rE   rF   rN   r   r   r   !test_module_in_package_in_zipfile   s    
z=MultiProcessingCmdLineMixin.test_module_in_package_in_zipfilec                 C   sZ   t  >}t|ddddd\}}t|dd|}| | W d    n1 sL0    Y  d S )NrD   rJ   r.      )r   rK   ztest_pkg.test_pkg.scriptrP   rQ   r   r   r   $test_module_in_subpackage_in_zipfile   s    
z@MultiProcessingCmdLineMixin.test_module_in_subpackage_in_zipfilec                 C   sl   | j }t J}tj|d}t| t|d|d}t|dd}| 	| W d    n1 s^0    Y  d S )NrJ   r@   r4   rK   )
rA   r   r0   r;   r   rL   r   r   r   r-   )r!   r   r   rM   r'   rN   r   r   r   test_package  s    
z(MultiProcessingCmdLineMixin.test_packagec                 C   s   | j }t l}tj|d}t| t|d|d}tj	|dd}t
| t|}t|dd}| | W d    n1 s0    Y  d S )NrJ   r@   r4   Tr7   rK   )rA   r   r0   r;   r   rL   r   r   r9   r:   r<   r=   r   r-   )r!   r   r   rM   r'   rH   r>   rN   r   r   r   test_package_compiled  s    


z1MultiProcessingCmdLineMixin.test_package_compiled)__name__
__module____qualname__ZmaxDiffr"   r*   r-   r2   r3   r6   r?   rB   rC   rG   rI   rO   rR   rT   rU   rV   r   r   r   r   r      s"   	
		
r   c                   @   s   e Zd ZdZeZdS )SpawnCmdLineTestspawnNrW   rX   rY   r   r5   rA   r   r   r   r   rZ     s   rZ   c                   @   s   e Zd ZdZeZdS )ForkCmdLineTestforkN)rW   rX   rY   r   test_sourcerA   r   r   r   r   r]     s   r]   c                   @   s   e Zd ZdZeZdS )ForkServerCmdLineTest
forkserverNr\   r   r   r   r   r`   #  s   r`   c                   C   s   t   d S r   )r   reap_childrenr   r   r   r   tearDownModule'  s    rc   r@   )N)&testr   import_moduler   Zimportlib.machineryZunittestr+   r;   Zos.pathr9   Ztest.support.script_helperr   r   r   r   r   ZPGOZSkipTestmultiprocessingsetget_all_start_methodsr    Z*skip_if_broken_multiprocessing_synchronizer#   r_   r5   r   r   r   r   r   ZTestCaserZ   r]   r`   rc   rW   mainr   r   r   r   <module>   s>   

,$



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