a
    ƹDfk                     @   sp   d Z ddg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
 G dd deZG dd deZdS )	a  
This module contains the base class for popen pipes, and describes
the popen pipe interface. The 'config' method can be overwritten
for pipe customization. The pipe's 'launch' method can be overwritten
with a derived pipe's new execution algorithm. See the following for
an example of standard use.


Usage
=====

A typical call to a popen 'pipe' will roughly follow this example:

    >>> # instantiate the pipe
    >>> pipe = Pipe()
    >>>
    >>> # configure the pipe to stage the command
    >>> pipe(command='hostname')
    >>>
    >>> # execute the launch and retrieve the response
    >>> pipe.launch()
    >>> print(pipe.response())
 
PipePipeException    N)Selector)_strc                   @   s   e Zd ZdZdS )r   z)Exception for failure to launch a commandN)__name__
__module____qualname____doc__ r
   r
   ^/nfs/NAS7/SABIOD/METHODE/ermites/ermites_venv/lib/python3.9/site-packages/pathos/connection.pyr   ,   s   c                   @   sr   e Zd ZdZdZddlmZ eddZ[ddd	Zd
d Z	dd Z
dd Zdd Zdd Zdd Zdd Ze
ZdS )r   z9a popen-based pipe for parallel and distributed computingTr   )logger   )levelNc                    s   t j |du r*d fddtdD n|| _|dd| _|dtj| _|d	d
| _	|dd| j | _
d| _d| _| jf i | dS )aO  create a popen-pipe

Inputs:
    name: a unique identifier (string) for the pipe
    command: a command to send  [default = 'echo <name>']
    background: run in background  [default = False]
    decode: ensure response is 'ascii'  [default = True]
    stdin: file-like object to serve as standard input for the remote process
        N c                 3   s   | ]}t  V  qd S N)randomchoice.0iZxyzr
   r   	<genexpr>C       z Pipe.__init__.<locals>.<genexpr>   
backgroundFstdindecodeasciicommandecho %sr   )stringascii_lettersjoinrangenamepopr   sysr   codecmessage	_response_pidconfig)selfr$   kwdsr
   r   r   __init__8   s    
zPipe.__init__c                 C   s
   d| j  S )Nz
Pipe('%s'))r(   r,   r
   r
   r   __repr__O   s    zPipe.__repr__c                    s    j du rd j  _  jdu r(tj _ jdu r8d _| D ]F\}}|dkrX| _ q@|dkrh| _q@|dkrx| _q@|dkr@| _q@d _g d}t fd	d
|D S )aX  configure the pipe using given keywords

(Re)configure the pipe for the following inputs:
    command: a command to send  [default = 'echo <name>']
    background: run in background  [default = False]
    decode: ensure response is 'ascii'  [default = True]
    stdin: file-like object to serve as standard input for the remote process
        Nr   r   r   r   r   r   )r(   r   r   r'   c                 3   s   | ]}|t  |fV  qd S r   )getattrr   r/   r
   r   r   m   r   zPipe.config.<locals>.<genexpr>)	r(   r$   r   r&   r'   itemsr   _stdoutdict)r,   r-   keyvaluenamesr
   r/   r   r+   R   s$    	


zPipe.configc                 C   s   d| _ |   dS )zlaunch a configured commandN)r)   _executer/   r
   r
   r   launcho   s    zPipe.launchc                 C   s   ddl m}m}m} | jrdz|| jd| j||dd}W n   td| j Y n0 |j| _	|j
| _nBz|| jd| j|d}W n   td| j Y n0 |j
| _d| _	d S )Nr   )PopenPIPESTDOUTT)shellr   stdoutstderr	close_fdszfailure to pipe: %s)r=   r   r>   )
subprocessr:   r;   r<   r   r(   r   r   pidr*   r>   r3   )r,   r:   r;   r<   pr
   r
   r   r8   u   s(    


zPipe._executec                    s    j du rtd jdu r"d}n& jdu r2d}n jdu rBd}n j} jdur^t j|S  jdkr j   _t j|S  fdd}d	d
 }t }| j | |	| |
d t j|S )zReturn the response from the launched process.
        Return None if no response was received yet from a background process.
        Nz.'launch' is required after any reconfigurationTr   Fr   c                    s.    j rtd  jd |  _d| _d S )Nzhandling pipe responseZ	on_remoteF)verboseprint_debuginforeadr)   state)selectorZfobjr/   r
   r   onData   s
    
zPipe.response.<locals>.onDatac                 S   s
   d| _ d S )NF)rI   )rJ   r
   r
   r   	onTimeout   s    z Pipe.response.<locals>.onTimeoutg       @)r3   r   r'   r)   r   r*   rH   r   ZnotifyOnReadReadyZnotifyWhenIdleZwatch)r,   r'   rK   rL   selr
   r/   r   response   s"    



zPipe.responsec                 C   s   | j S )zget pipe pid)r*   r/   r
   r
   r   rB      s    zPipe.pidc                 C   sF   | j dkrB| jrtd| j   t| j tj t| j d d| _ dS )zterminate the piper   zKill pid=%dN)r*   rD   rE   oskillsignalSIGTERMwaitpidr/   r
   r
   r   rP      s    
z	Pipe.kill)N)r   r   r   r	   rD   Zpathosr   rF   r.   r0   r+   r9   r8   rN   rB   rP   __call__r
   r
   r
   r   r   1   s   

&
)r	   __all__rO   r&   rQ   r   r    Zpathos.selectorr   Zpathos.utilr   	Exceptionr   objectr   r
   r
   r
   r   <module>
   s   