
    daf-                     D   d dl mZ d dlmZ  ej                  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 d dlmZmZmZmZmZ ej&                  r ej(                  d      d dlZ e ej.                               Z ej2                          ej4                  ZdZd	Zed
fdZedfdZdZddZ  G d d      Z! G d de!ejD                        Z# G d de!ejD                        Z$ G d de!ejD                        Z%d Z&e'dk(  r ejP                          yy)    )support)import_helper_multiprocessingN)	os_helper)make_pkgmake_scriptmake_zip_pkgmake_zip_scriptassert_python_okztest is not helpful for PGOaT  # 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
from test import support

# 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)

        # up to 1 min to report the results
        for _ in support.sleeping_retry(support.LONG_TIMEOUT,
                                        "Timed out waiting for results"):
            if results:
                break

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

    pool.join()
ao  # __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
from test import support

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)
    # up to 1 min to report the results
    for _ in support.sleeping_retry(support.LONG_TIMEOUT,
                                    "Timed out waiting for results"):
        if results:
            break

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

pool.join()
Fc                 n    t        | |||      }|dk(  rt        | dd       t        j                          |S )Ncheck_siblingsibling )r   	importlibinvalidate_caches)
script_dirscript_basenamesourceomit_suffix	to_returns        B/root/Python-3.12.4/Lib/test/test_multiprocessing_main_handling.py_make_test_scriptr   n   s=    J"K1I /)J	2.!       c                 N    t        | |||||      }t        j                          |S N)r	   r   r   )zip_dirzip_basenamepkg_namer   r   depthr   s          r   _make_test_zip_pkgr!   x   s+    WlHo#U,I!r   zPimport sys, os.path, runpy
sys.path.insert(0, %s)
runpy._run_module_as_main(%r)
c                 ~    |d}nt        |      }t        ||fz  }t        | ||      }t        j                          |S )Nzos.path.dirname(__file__))reprlaunch_sourcer   r   r   )r   r   module_namepathr   r   s         r   _make_launch_scriptr'      sD    |*DzdK00FJ@I!r   c                   p    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zy)MultiProcessingCmdLineMixinNc                 f    | j                   t        vr| j                  d| j                   z         y y )Nz%r start method not available)start_methodAVAILABLE_START_METHODSskipTest)selfs    r   setUpz!MultiProcessingCmdLineMixin.setUp   s.    $;;MM9D<M<MMN <r   c                 <   t         dkD  r"t        d|z         t        t        |             | j                  |d       | j                  |j	                  d      d       d| j
                  z  }| j                  |j	                  d      j                         |       y )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errexpected_resultss         r   _check_outputz)MultiProcessingCmdLineMixin._check_output   s    Q;/+=>$s)A&G,b1,t/@/@@G,2246FGr   c                 n    	 ||| j                  fz   }t	        |ddi\  }}}| j                  ||||       y )NF
__isolated)sysflagsoptimizer+   r   r;   )r.   r6   cmd_line_switchesrun_argsrcr8   r9   s          r   _check_scriptz)MultiProcessingCmdLineMixin._check_script   sC    $T5F5F'GG'DeDC;C5r   c                     t        j                         5 }t        |d      }| j                  |       d d d        y # 1 sw Y   y xY w)Nscriptr   temp_dirr   rD   r.   r   r6   s      r   test_basic_scriptz-MultiProcessingCmdLineMixin.test_basic_script   s?    ! 	,Z+JAK{+	, 	, 	,s	   <Ac                     t        j                         5 }t        |dd      }| j                  |       d d d        y # 1 sw Y   y xY w)NrF   T)r   rG   rI   s      r   test_basic_script_no_suffixz7MultiProcessingCmdLineMixin.test_basic_script_no_suffix   sD    ! 	,Z+J8<>K{+	, 	, 	,s	    >Ac                     t         }t        j                         5 }t        |d|      }| j	                  |       t        |d|d      }| j	                  |       d d d        y # 1 sw Y   y xY w)Nipythonr   T)r   r   )$test_source_main_skipped_in_childrenr   rH   r   rD   )r.   r   r   r6   script_no_suffixs        r   test_ipython_workaroundz3MultiProcessingCmdLineMixin.test_ipython_workaround   sr    
 6! 	1Z+J	39;K{+0Y8>=A C /0	1 	1 	1s   A A$$A-c                    t        j                         5 }t        |d      }t        j                  |d       t        j                  |       t        j                  |      }| j                  |       d d d        y # 1 sw Y   y xY w)NrF   Tdoraise)
r   rH   r   
py_compilecompileosremover   make_legacy_pycrD   )r.   r   r6   pyc_files       r   test_script_compiledz0MultiProcessingCmdLineMixin.test_script_compiled   sk    ! 	)Z+JAK{D9IIk"$44[AHx(	) 	) 	)s   AA==Bc                     | j                   }t        j                         5 }t        |d|      }| j	                  |       d d d        y # 1 sw Y   y xY w)N__main__rO   )main_in_children_sourcer   rH   r   rD   )r.   r   r   r6   s       r   test_directoryz*MultiProcessingCmdLineMixin.test_directory   sO    --! 	+Z+J
39;Kz*	+ 	+ 	+s    A

Ac                 .   | j                   }t        j                         5 }t        |d|      }t	        j
                  |d       t        j                  |       t        j                  |      }| j                  |       d d d        y # 1 sw Y   y xY w)Nr^   rO   TrT   )r_   r   rH   r   rV   rW   rX   rY   r   rZ   rD   )r.   r   r   r6   r[   s        r   test_directory_compiledz3MultiProcessingCmdLineMixin.test_directory_compiled   s{    --! 	+Z+J
39;K{D9IIk"$44[AHz*	+ 	+ 	+s   A!BBc                     | j                   }t        j                         5 }t        |d|      }t	        |d|      \  }}| j                  |       d d d        y # 1 sw Y   y xY w)Nr^   rO   test_zip)r_   r   rH   r   r
   rD   )r.   r   r   r6   zip_namerun_names         r   test_zipfilez(MultiProcessingCmdLineMixin.test_zipfile   sb    --! 	)Z+J
39;K!0Z!UHhx(		) 	) 	)s   0AA#c                     | j                   }t        j                         5 }t        |d|      }t	        j
                  |d      }t        |d|      \  }}| j                  |       d d d        y # 1 sw Y   y xY w)Nr^   rO   TrT   rd   )r_   r   rH   r   rV   rW   r
   rD   )r.   r   r   r6   compiled_namere   rf   s          r   test_zipfile_compiledz1MultiProcessingCmdLineMixin.test_zipfile_compiled   st    --! 	)Z+J
39;K&..{DIM!0Z!WHhx(	) 	) 	)s   AA11A:c                     t        j                         5 }t        j                  j	                  |d      }t        |       t        |d      }t        |dd      }| j                  |       d d d        y # 1 sw Y   y xY w)Ntest_pkgr   launchztest_pkg.check_sibling)	r   rH   rX   r&   joinr   r   r'   rD   )r.   r   pkg_dirr6   launch_names        r   test_module_in_packagez2MultiProcessingCmdLineMixin.test_module_in_package   sm    ! 	,Zggll:z:GW+G_EK-j(.FHK{+	, 	, 	,s   AA44A=c                     t        j                         5 }t        |ddd      \  }}t        |dd|      }| j	                  |       d d d        y # 1 sw Y   y xY w)Nrd   rl   rF   rm   ztest_pkg.scriptr   rH   r!   r'   rD   r.   r   re   rf   rp   s        r   !test_module_in_package_in_zipfilez=MultiProcessingCmdLineMixin.test_module_in_package_in_zipfile   s[    ! 	,Z!3J
JX`!aHh-j(DUW_`K{+	, 	, 	,s   1AAc                     t        j                         5 }t        |dddd      \  }}t        |dd|      }| j	                  |       d d d        y # 1 sw Y   y xY w)Nrd   rl   rF      )r    rm   ztest_pkg.test_pkg.scriptrs   rt   s        r   $test_module_in_subpackage_in_zipfilez@MultiProcessingCmdLineMixin.test_module_in_subpackage_in_zipfile   s^    ! 	,Z!3J
JX`hi!jHh-j(D^`hiK{+	, 	, 	,s   3AAc                    | j                   }t        j                         5 }t        j                  j                  |d      }t        |       t        |d|      }t        |dd      }| j                  |       d d d        y # 1 sw Y   y xY w)Nrl   r^   rO   rm   )
r_   r   rH   rX   r&   rn   r   r   r'   rD   )r.   r   r   ro   r6   rp   s         r   test_packagez(MultiProcessingCmdLineMixin.test_package  sy    --! 	,Zggll:z:GW+GZ39;K-j(JOK{+	, 	, 	,s   ABBc                    | j                   }t        j                         5 }t        j                  j                  |d      }t        |       t        |d|      }t        j                  |d      }t        j                  |       t        j                  |      }t        |dd      }| j                  |       d d d        y # 1 sw Y   y xY w)Nrl   r^   rO   TrT   rm   )r_   r   rH   rX   r&   rn   r   r   rV   rW   rY   r   rZ   r'   rD   )r.   r   r   ro   r6   ri   r[   rp   s           r   test_package_compiledz1MultiProcessingCmdLineMixin.test_package_compiled  s    --! 		,Zggll:z:GW+GZ39;K&..{DIMIIk"$44[AH-j(JOK{+		, 		, 		,s   BCC)__name__
__module____qualname__maxDiffr/   r;   rD   rJ   rL   rR   r\   r`   rb   rg   rj   rq   ru   rx   rz   r|    r   r   r)   r)      sY    GOH6,
,1)++)),,,,,r   r)   c                       e Zd ZdZeZy)SpawnCmdLineTestspawnNr}   r~   r   r+   rP   r_   r   r   r   r   r     s    LBr   r   c                       e Zd ZdZeZy)ForkCmdLineTestforkN)r}   r~   r   r+   test_sourcer_   r   r   r   r   r      s    L)r   r   c                       e Zd ZdZeZy)ForkServerCmdLineTest
forkserverNr   r   r   r   r   r   $  s    LBr   r   c                  ,    t        j                          y r   )r   reap_childrenr   r   r   tearDownModuler   (  s    r   r^   r   ))testr   test.supportr   import_moduler   importlib.machineryunittestr>   rX   os.pathrV   r   test.support.script_helperr   r   r	   r
   r   PGOSkipTestmultiprocessingsetget_all_start_methodsr,   *skip_if_broken_multiprocessing_synchronizer1   r   rP   r   r!   r$   r'   r)   TestCaser   r   r   r   r}   mainr   r   r   <module>r      s4    &   . /    
 	   "  ;;
(

9
:: CoCCEF  3 2 2 4
//*X( $F )e *E, E,RC2H4E4E C*183D3D *C79J9J C zHMMO r   