o
    OZh.                     @   sb  d dl mZ 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 d dlmZmZmZmZmZ ejrIe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re*  dS dS )     )support)import_helper_multiprocessingN)	os_helper)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   l/var/www/html/lang_env/lib/python3.10/site-packages/multiprocess/tests/test_multiprocessing_main_handling.py_make_test_scriptq   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_pkg{   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 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   s8   	 ||| jf }t|ddi\}}}| |||| d S )NF-OZ
__isolated)sysflagsoptimizer#   r
   r0   )r%   r,   Zcmd_line_switchesZrun_argsrcr.   r/   r   r   r   _check_script   s   z)MultiProcessingCmdLineMixin._check_scriptc                 C   sB   t  }t|d}| | W d    d S 1 sw   Y  d S )Nscriptr   temp_dirr   r7   r%   r   r,   r   r   r   test_basic_script   s   

"z-MultiProcessingCmdLineMixin.test_basic_scriptc                 C   sF   t  }t|ddd}| | W d    d S 1 sw   Y  d S )Nr8   T)r   r9   r;   r   r   r   test_basic_script_no_suffix   s   
"z7MultiProcessingCmdLineMixin.test_basic_script_no_suffixc                 C   sd   t }t "}t|d|d}| | t|d|dd}| | W d    d S 1 s+w   Y  d S )Nipythonr   T)r   r   )$test_source_main_skipped_in_childrenr   r:   r   r7   )r%   r   r   r,   Zscript_no_suffixr   r   r   test_ipython_workaround   s   

"z3MultiProcessingCmdLineMixin.test_ipython_workaroundc                 C   sd   t  $}t|d}tj|dd t| t|}| 	| W d    d S 1 s+w   Y  d S )Nr8   Tdoraise)
r   r:   r   
py_compilecompileosremover   make_legacy_pycr7   )r%   r   r,   pyc_filer   r   r   test_script_compiled   s   



"z0MultiProcessingCmdLineMixin.test_script_compiledc                 C   sL   | j }t }t|d|d}| | W d    d S 1 sw   Y  d S )N__main__r?   )main_in_children_sourcer   r:   r   r7   )r%   r   r   r,   r   r   r   test_directory   s   
"z*MultiProcessingCmdLineMixin.test_directoryc                 C   sn   | j }t &}t|d|d}tj|dd t| t	|}| 
| W d    d S 1 s0w   Y  d S )NrK   r?   TrB   )rL   r   r:   r   rD   rE   rF   rG   r   rH   r7   )r%   r   r   r,   rI   r   r   r   test_directory_compiled   s   


"z3MultiProcessingCmdLineMixin.test_directory_compiledc                 C   s\   | j }t }t|d|d}t|d|\}}| | W d    d S 1 s'w   Y  d S )NrK   r?   test_zip)rL   r   r:   r   r	   r7   )r%   r   r   r,   zip_namerun_namer   r   r   test_zipfile   s   
"z(MultiProcessingCmdLineMixin.test_zipfilec                 C   sj   | j }t $}t|d|d}tj|dd}t|d|\}}| | W d    d S 1 s.w   Y  d S )NrK   r?   TrB   rO   )rL   r   r:   r   rD   rE   r	   r7   )r%   r   r   r,   compiled_namerP   rQ   r   r   r   test_zipfile_compiled   s   
"z1MultiProcessingCmdLineMixin.test_zipfile_compiledc                 C   sd   t  $}tj|d}t| t|d}t|dd}| | W d    d S 1 s+w   Y  d S )Ntest_pkgr   launchztest_pkg.check_sibling)	r   r:   rF   r    joinr   r   r!   r7   )r%   r   pkg_dirr,   launch_namer   r   r   test_module_in_package   s   

"z2MultiProcessingCmdLineMixin.test_module_in_packagec                 C   sX   t  }t|ddd\}}t|dd|}| | W d    d S 1 s%w   Y  d S )NrO   rU   r8   rV   ztest_pkg.scriptr   r:   r   r!   r7   r%   r   rP   rQ   rY   r   r   r   !test_module_in_package_in_zipfile   s
   
"z=MultiProcessingCmdLineMixin.test_module_in_package_in_zipfilec                 C   s\   t   }t|ddddd\}}t|dd|}| | W d    d S 1 s'w   Y  d S )NrO   rU   r8      )r   rV   ztest_pkg.test_pkg.scriptr[   r\   r   r   r   $test_module_in_subpackage_in_zipfile   s
   
"z@MultiProcessingCmdLineMixin.test_module_in_subpackage_in_zipfilec                 C   sn   | j }t &}tj|d}t| t|d|d}t|dd}| 	| W d    d S 1 s0w   Y  d S )NrU   rK   r?   rV   )
rL   r   r:   rF   r    rW   r   r   r!   r7   )r%   r   r   rX   r,   rY   r   r   r   test_package  s   
"z(MultiProcessingCmdLineMixin.test_packagec                 C   s   | j }t 7}tj|d}t| t|d|d}tj	|dd}t
| t|}t|dd}| | W d    d S 1 sAw   Y  d S )NrU   rK   r?   TrB   rV   )rL   r   r:   rF   r    rW   r   r   rD   rE   rG   r   rH   r!   r7   )r%   r   r   rX   r,   rS   rI   rY   r   r   r   test_package_compiled  s   


"z1MultiProcessingCmdLineMixin.test_package_compiled)__name__
__module____qualname__ZmaxDiffr&   r0   r7   r<   r=   rA   rJ   rM   rN   rR   rT   rZ   r]   r_   r`   ra   r   r   r   r   r"      s$    	
		
r"   c                   @      e Zd ZdZeZdS )SpawnCmdLineTestZspawnNrb   rc   rd   r#   r@   rL   r   r   r   r   rf         rf   c                   @   re   )ForkCmdLineTestforkN)rb   rc   rd   r#   test_sourcerL   r   r   r   r   ri   #  rh   ri   c                   @   re   )ForkServerCmdLineTestZ
forkserverNrg   r   r   r   r   rl   '  rh   rl   c                   C   s   t   d S r   )r   Zreap_childrenr   r   r   r   tearDownModule+  s   rm   rK   r   )+testr   Ztest.supportr   import_moduler   Zimportlib.machineryZunittestr3   rF   Zos.pathrD   r   Ztest.support.script_helperr   r   r   r	   r
   ZPGOZSkipTestZmultiprocessmultiprocessingsetZget_all_start_methodsr$   r'   formatr    rk   r@   r   r   r   r!   r"   ZTestCaserf   ri   rl   rm   rb   mainr   r   r   r   <module>   sN   


+- %



 
