3
]ð]X  ã               @   sì   d Z ddlZddlZddlZddlZdZejjeƒZ	ej
d dk rLdd„ Zndd„ Zdd	„ Zejd
kr|eeddƒZdd„ Znldd„ Zdd„ Zdd„ ZedkrèeedƒrÈyejƒ  W n ek
rÆ   Y nX eeejdd… ƒƒ ejdƒ dS )zßSupport for spawning a daemon process.

PyMongo only attempts to spawn the mongocryptd daemon process when automatic
client-side field level encryption is enabled. See
:ref:`automatic-client-side-encryption` for more info.
é    Né
   é   c             C   sh   ddl m} |ƒ | }d}xH| jƒ }|dk	r0|S ||ƒ  }|dkrFdS t|d |dƒ}tj|ƒ qW dS )z,Implement wait timeout support for Python 2.r   )Útimegü©ñÒMb@?Né   g      à?)Zpymongo.monotonicr   ÚpollÚminÚsleep)ÚpopenÚtimeoutÚ_timeÚdeadlineÚdelayÚ
returncodeÚ	remaining© r   ú1/tmp/pip-build-20mum3z4/pymongo/pymongo/daemon.pyÚ_popen_wait    s    

r   c             C   s(   y| j |d�S  tjk
r"   dS X dS )z,Implement wait timeout support for Python 3.)r
   N)ÚwaitÚ
subprocessÚTimeoutExpired)r	   r
   r   r   r   r   3   s    c             C   s
   d| _ dS )zpSilence Popen's ResourceWarning.

    Note this should only be used if the process was created as a daemon.
    r   N)r   )r	   r   r   r   Ú_silence_resource_warning<   s    	r   Úwin32ZDETACHED_PROCESSé   c             C   s8   t tjdƒ�"}tj| t|||d�}t|ƒ W dQ R X dS )z!Spawn a daemon process (Windows).zr+b)ÚcreationflagsÚstdinÚstderrÚstdoutN)ÚopenÚosÚdevnullr   ÚPopenÚ_DETACHED_PROCESSr   )Úargsr   r	   r   r   r   Ú_spawn_daemonL   s    r#   c             C   s,   t tjdƒ�}tj| d|||d�S Q R X dS )z,Spawn the process and silence stdout/stderr.zr+bT)Ú	close_fdsr   r   r   N)r   r   r   r   r    )r"   r   r   r   r   Ú_spawn`   s
    r%   c             C   s0   t jtg}|j| ƒ tj|dd�}t|tƒ dS )z7Spawn a daemon process using a double subprocess.Popen.T)r$   N)ÚsysÚ
executableÚ
_THIS_FILEÚextendr   r    r   Ú_WAIT_TIMEOUT)r"   Zspawner_argsZ	temp_procr   r   r   Ú_spawn_daemon_double_popeni   s    

r+   c             C   s   t jrt| ƒ nt| ƒ dS )zSpawn a daemon process (Unix).N)r&   r'   r+   r%   )r"   r   r   r   r#   s   s    

Ú__main__Úsetsidé   )Ú__doc__r   r   r&   r   r*   ÚpathÚrealpathÚ__file__r(   Úversion_infor   r   ÚplatformÚgetattrr!   r#   r%   r+   Ú__name__Úhasattrr-   ÚOSErrorÚargvÚ_exitr   r   r   r   Ú<module>   s0   
	

	

