a ze-@s6ddlmZedddlZddlZddlZddlZddlZddlZddl Z ddl m Z m Z m Z mZmZejrzedddlZeeZeejZdZdZedfd d Zed fd d ZdZdddZGdddZGdddeejZ GdddeejZ!GdddeejZ"ddZ#e$dkr2e%dS))support_multiprocessingN)make_pkg make_script make_zip_pkgmake_zip_scriptassert_python_okztest 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() FcCs.t||||}|dkr"t|ddt|S)N check_siblingZsibling)r importlibinvalidate_caches) script_dirscript_basenamesource omit_suffix to_returnr[/opt/bitninja-python-dojo/embedded/lib/python3.9/test/test_multiprocessing_main_handling.py_make_test_scriptms rcCst||||||}t|SN)rr r )Zzip_dirZ zip_basenameZpkg_namerrdepthrrrr_make_test_zip_pkgws  rzPimport sys, os.path, runpy sys.path.insert(0, %s) runpy._run_module_as_main(%r) cCs:|durd}nt|}t||f}t|||}t|S)Nzos.path.dirname(__file__))repr launch_sourcerr r )r r module_namepathrrrrr_make_launch_scripts  rc@seZdZdZddZddZddZdd Zd d Zd d Z ddZ ddZ ddZ ddZ ddZddZddZddZddZd d!ZdS)"MultiProcessingCmdLineMixinNcCs|jtvr|d|jdS)Nz%r start method not available) start_methodAVAILABLE_START_METHODSZskipTest)selfrrrsetUps z!MultiProcessingCmdLineMixin.setUpcCsbtdkr td|tt|||d||ddd|j}||d|dS)NrzOutput from test script %r:rzutf-8r z%s -> [1, 4, 9])verboseprintrZ assertEqualdecoderstrip)r! script_nameZ exit_codeouterrZexpected_resultsrrr _check_outputs    z)MultiProcessingCmdLineMixin._check_outputcGsL|ddtjjf7}|||jf}t|ddi\}}}|||||dS)N-OZ __isolatedF)sysflagsoptimizerrr*)r!r'Zcmd_line_switchesZrun_argsrcr(r)rrr _check_scriptsz)MultiProcessingCmdLineMixin._check_scriptcCs@t$}t|d}||Wdn1s20YdS)Nscriptrtemp_dirrr1r!r r'rrrtest_basic_scripts  z-MultiProcessingCmdLineMixin.test_basic_scriptcCsDt(}t|ddd}||Wdn1s60YdS)Nr2T)rr3r5rrrtest_basic_script_no_suffixs  z7MultiProcessingCmdLineMixin.test_basic_script_no_suffixcCsbt}tB}t|d|d}||t|d|dd}||Wdn1sT0YdS)NZipythonrT)rr)$test_source_main_skipped_in_childrenrr4rr1)r!rr r'Zscript_no_suffixrrrtest_ipython_workarounds  z3MultiProcessingCmdLineMixin.test_ipython_workaroundcCsbtF}t|d}tj|ddt|t|}||Wdn1sT0YdS)Nr2Tdoraise) rr4r py_compilecompileosremovemake_legacy_pycr1)r!r r'pyc_filerrrtest_script_compileds     z0MultiProcessingCmdLineMixin.test_script_compiledcCsJ|j}t(}t|d|d}||Wdn1s<0YdS)N__main__r8)main_in_children_sourcerr4rr1)r!rr r'rrrtest_directorys  z*MultiProcessingCmdLineMixin.test_directorycCsl|j}tJ}t|d|d}tj|ddt|t|}| |Wdn1s^0YdS)NrDr8Tr;) rErr4rr=r>r?r@rAr1)r!rr r'rBrrrtest_directory_compileds   z3MultiProcessingCmdLineMixin.test_directory_compiledcCsZ|j}t8}t|d|d}t|d|\}}||Wdn1sL0YdS)NrDr8test_zip)rErr4rrr1)r!rr r'zip_namerun_namerrr test_zipfiles z(MultiProcessingCmdLineMixin.test_zipfilecCsh|j}tF}t|d|d}tj|dd}t|d|\}}||Wdn1sZ0YdS)NrDr8Tr;rH)rErr4rr=r>rr1)r!rr r' compiled_namerIrJrrrtest_zipfile_compileds z1MultiProcessingCmdLineMixin.test_zipfile_compiledcCsbtF}tj|d}t|t|d}t|dd}||Wdn1sT0YdS)Ntest_pkgr launchztest_pkg.check_sibling) rr4r?rjoinrrrr1)r!r pkg_dirr' launch_namerrrtest_module_in_packages  z2MultiProcessingCmdLineMixin.test_module_in_packagecCsVt:}t|ddd\}}t|dd|}||Wdn1sH0YdS)NrHrNr2rOztest_pkg.scriptrr4rrr1r!r rIrJrRrrr!test_module_in_package_in_zipfiles z=MultiProcessingCmdLineMixin.test_module_in_package_in_zipfilecCsZt>}t|ddddd\}}t|dd|}||Wdn1sL0YdS)NrHrNr2)rrOztest_pkg.test_pkg.scriptrTrUrrr$test_module_in_subpackage_in_zipfiles z@MultiProcessingCmdLineMixin.test_module_in_subpackage_in_zipfilecCsl|j}tJ}tj|d}t|t|d|d}t|dd}| |Wdn1s^0YdS)NrNrDr8rO) rErr4r?rrPrrrr1)r!rr rQr'rRrrr test_packages  z(MultiProcessingCmdLineMixin.test_packagecCs|j}tl}tj|d}t|t|d|d}tj |dd}t |t |}t |dd}| |Wdn1s0YdS)NrNrDr8Tr;rO)rErr4r?rrPrrr=r>r@rArr1)r!rr rQr'rLrBrRrrrtest_package_compiled s    z1MultiProcessingCmdLineMixin.test_package_compiled)__name__ __module__ __qualname__ZmaxDiffr"r*r1r6r7r:rCrFrGrKrMrSrVrXrYrZrrrrrs"     rc@seZdZdZeZdS)SpawnCmdLineTestspawnNr[r\r]rr9rErrrrr^sr^c@seZdZdZeZdS)ForkCmdLineTestforkN)r[r\r]r test_sourcerErrrrrasrac@seZdZdZeZdS)ForkServerCmdLineTest forkserverNr`rrrrrd#srdcCs tdSr)r reap_childrenrrrrtearDownModule'srgrD)N)&testr import_moduler Zimportlib.machineryZunittestr-r?Zos.pathr=Ztest.support.script_helperrrrrrZPGOZSkipTestmultiprocessingsetget_all_start_methodsr Z*skip_if_broken_multiprocessing_synchronizer#rcr9rrrrrZTestCaser^rardrgr[mainrrrrs>    ,$