3
YdT                 @   s   d Z ddlZddlZddlmZ ddlmZ ddlZddlj	j
ZddlmZ dddZdd	 Zejjejdkdddd Zejjejdkdddd ZdS )z;Testing module for functions and classes from multiproc.py
    N)mkdtemp)rmtree)Functionc             C   s&  ddl }ddl}ddl}ddl}d}dg| }dg| }dg| }dd }	xPt|D ]D}
d||
< |jddd	 }|||
< |j|	|fd
||
< ||
 j  qTW d}x@|s|jd	 x t|D ]}
||
 j	 ||
< qW t
|sd}qW | }x:|D ]2}t|}|t|j 7 }W dQ R X |j| qW |S )z>
    Run a multiprocessing job and spawn child processes.
    r   N   c          
   S   sH   d}xt ddD ]}||7 }qW t| d}|jt| W dQ R X dS )zJ
        This function writes the value 45 to the given filename.
        r   
   wN)rangeopenwritestr)filenamejif r   n/var/www/html/virt/lib/python3.6/site-packages/nipype/pipeline/plugins/tests/test_legacymultiproc_nondaemon.pydummyFunction'   s
    z%mytestFunction.<locals>.dummyFunctionTz.txttest_engine_   )targetargsF)multiprocessingostempfiletimer   mkstempProcessstartsleepis_aliveanyr	   intreadremove)insumr   r   r   r   ZnumberOfThreadstar   r   nZtmpFileZallDonetotalfffdr   r   r   mytestFunction   s8    





r+   c       
      C   s   t j }tdd}t j| tjdd}tjttdgdgddd	}tjttdgdgdd
d	}|j	||dgfg t j |_
d|j_d|jd d< |jdd| dd}dd |j D }t|j |jd }|jd}	t j| t| |	S )zm
    Start a pipe with two nodes using the resource multiproc plugin and
    passing the nondaemon_flag.
    r   )prefixpipe)namer$   sum_out)functionZinput_namesZoutput_namesf1)Z	interfacer.   f2r   TZ	executionZstop_on_first_crashZLegacyMultiProcr   )Zn_procsZ
non_daemon)ZpluginZplugin_argsc             S   s   g | ]}d j |j|jfqS ).)joinZ
_hierarchyr.   ).0noder   r   r   
<listcomp>   s    z5run_multiproc_nondaemon_with_flag.<locals>.<listcomp>zpipe.f2)r/   r$   )r   getcwdr   chdirpeZWorkflowNoder   r+   connectbase_dirinputsr$   configrunZnodeslistindexZ
get_outputr   )
Znondaemon_flagZcur_dirtemp_dirr-   r1   r2   Z	execgraphnamesr6   resultr   r   r   !run_multiproc_nondaemon_with_flagZ   s2    




rF         z$multiprocessing issues in Python 3.8)reasonc           	   C   s.   d} yt d W n   d} Y nX | s*tdS )a7  
    This is the entry point for the test. Two times a pipe of several
    multiprocessing jobs gets executed. First, without the nondaemon flag.
    Second, with the nondaemon flag.

    Since the processes of the pipe start child processes, the execution only
    succeeds when the non_daemon flag is on.
    FTN)rF   AssertionError)ZshouldHaveFailedr   r   r   "test_run_multiproc_nondaemon_false   s    
rK   c              C   s   t d} | dkstd S )NT   )rF   rJ   )rE   r   r   r   !test_run_multiproc_nondaemon_true   s    rM   )r   )rG   rH   )rG   rH   )__doc__r   sysr   r   shutilr   ZpytestZnipype.pipeline.engineZpipelineZenginer:   Znipype.interfaces.utilityr   r+   rF   markZskipifversion_inforK   rM   r   r   r   r   <module>   s   
I/