File: //usr/local/lib/python3.7/test/__pycache__/test_multiprocessing_main_handling.cpython-37.opt-2.pyc
B
��g�- � @ s8 d dl mZ e�d� d dlZd dlZd dlZd dlZd dlZd dlZd dl Z d dl
mZmZm
Z
mZmZ ejrze�d��d dlZee�� �Ze�d� ejZdZdZed fd
d�Zedfd
d�ZdZddd�ZG dd� d�ZG dd� deej�ZG dd� deej�Z G dd� deej�Z!dd� Z"e#dk�r4e�$� dS )� )�supportZ_multiprocessingN)�make_pkg�make_script�make_zip_pkg�make_zip_script�assert_python_okztest is not helpful for PGOzmultiprocessing.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
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 | |||�}|dkr"t | dd� t�� |S )N�
check_siblingZsibling� )r � importlib�invalidate_caches)�
script_dir�script_basename�source�omit_suffix� to_return� r �C/usr/local/lib/python3.7/test/test_multiprocessing_main_handling.py�_make_test_scriptm s r � c C s t | |||||�}t�� |S )N)r r
r )Zzip_dirZzip_basenameZpkg_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 krd}nt |�}t||f }t| ||�}t�� |S )Nzos.path.dirname(__file__))�repr�
launch_sourcer r
r )r r
Zmodule_name�pathr r r r r �_make_launch_script� s r c @ s� e Zd ZdZdd� Zdd� Zdd� Zdd � Zd
d� Zdd
� Z dd� Z
dd� Zdd� Zdd� Z
dd� Zdd� Zdd� Zdd� Zdd� Zd d!� ZdS )"�MultiProcessingCmdLineMixinNc C s | j tkr| �d| j � d S )Nz%r start method not available)�start_method�AVAILABLE_START_METHODSZskipTest)�selfr r r �setUp� s
z!MultiProcessingCmdLineMixin.setUpc C sb t dkr td| � tt|�� | �|d� | �|�d�d� d| j }| �|�d��� |� d S )Nr zOutput from test script %r:r zutf-8r z%s -> [1, 4, 9])�verbose�printr ZassertEqual�decoder �strip)r �script_nameZ exit_code�out�errZexpected_resultsr r r �
_check_output� s
z)MultiProcessingCmdLineMixin._check_outputc G sL |ddt jj f7 }||| jf }t|ddi�\}}}| �||||� d S )N�-�OZ
__isolatedF)�sys�flags�optimizer r r'