B
    \J	                 @   sR   d Z yddlmZmZ W n$ ek
r<   ddlmZmZ Y nX G dd deZdS )zLBlocking channels

Useful for test suites and blocking terminal interfaces.
    )QueueEmptyc                   s|   e Zd ZdZdZdZdZdZg Zd fdd	Z	dd Z
dd	d
Zdd Zdd Zdd ZeZdd Zdd Zdd Z  ZS )ZMQSocketChannelz%A ZMQ socket in a simple blocking APINFc                s   t t|   || _|| _dS )a  Create a channel.

        Parameters
        ----------
        socket : :class:`zmq.Socket`
            The ZMQ socket to use.
        session : :class:`session.Session`
            The session to use.
        loop
            Unused here, for other implementations
        N)superr   __init__socketsession)selfr   r   Zloop)	__class__ ?lib/python3.7/site-packages/jupyter_client/blocking/channels.pyr      s    zZMQSocketChannel.__init__c             K   s*   | j jf |}| j|\}}| j|S )N)r   Zrecv_multipartr   Zfeed_identitiesZdeserialize)r	   kwargsmsgZidentZsmsgr   r   r   _recv(   s    zZMQSocketChannel._recvTc             C   sD   |r"|dk	r|d9 }| j |}n| j jdd}|r<|  S tdS )z/ Gets a message if there is one that is ready. Ni  r   )timeout)r   pollr   r   )r	   blockr   Zreadyr   r   r   get_msg-   s    zZMQSocketChannel.get_msgc             C   s<   g }x2y| | jdd W q tk
r2   P Y qX qW |S )z, Get all messages that are currently ready. F)r   )appendr   r   )r	   Zmsgsr   r   r   get_msgs;   s    zZMQSocketChannel.get_msgsc             C   s   t | jjddS )z, Is there a message that has been received? r   )r   )boolr   r   )r	   r   r   r   	msg_readyE   s    zZMQSocketChannel.msg_readyc             C   s<   | j d k	r8y| j jdd W n tk
r0   Y nX d | _ d S )Nr   )Zlinger)r   close	Exception)r	   r   r   r   r   I   s    
zZMQSocketChannel.closec             C   s
   | j d k	S )N)r   )r	   r   r   r   is_aliveR   s    zZMQSocketChannel.is_alivec             C   s   | j | j| dS )z1Pass a message to the ZMQ socket to send
        N)r   sendr   )r	   r   r   r   r   r   U   s    zZMQSocketChannel.sendc             C   s   d S )Nr   )r	   r   r   r   startZ   s    zZMQSocketChannel.start)N)TN)__name__
__module____qualname____doc__r   r   streamZ_exitingZproxy_methodsr   r   r   r   r   r   stopr   r   r   __classcell__r   r   )r
   r   r      s    

r   N)r    Zqueuer   r   ImportErrorobjectr   r   r   r   r   <module>   s
   