Your IP : 216.73.216.213


Current Path : /opt/alt/python35/lib/python3.5/site-packages/aioredis/__pycache__/
Upload File :
Current File : //opt/alt/python35/lib/python3.5/site-packages/aioredis/__pycache__/pubsub.cpython-35.opt-1.pyc



�D�Z�5�@s�ddlZddlZddlZddlZddlmZddlmZddlm	Z	ddl
mZddd	gZe
�ZGd
d�de�ZGdd�d�ZGd
d	�d	�ZGdd�de�ZGdd�d�ZdS)�N�)�
AbcChannel)�_converters)�ChannelClosedError)�logger�Channel�EndOfStream�Receiverc@s�eZdZdZddd�Zdd�Zedd��Zed	d
��Zedd��Z	d
ddddd�Z
ddd�Zd
ddddd�Zdd�Z
dd�Zddd�ZdS)rzWrapper around asyncio.Queue.NcCs8td|�|_tt|�|�|_||_dS)N�loop)�
ClosableQueue�_queuer�type�_name�_is_pattern)�self�name�
is_patternr
�r�@/opt/alt/python35/lib/python3.5/site-packages/aioredis/pubsub.py�__init__szChannel.__init__cCs+dj|jj|j|j|jj��S)Nz'<{} name:{!r}, is_pattern:{}, qsize:{}>)�format�	__class__�__name__rrr�qsize)rrrr�__repr__s	zChannel.__repr__cCs|jS)zEncoded channel name/pattern.)r)rrrrr#szChannel.namecCs|jS)z0Set to True if channel is subscribed to pattern.)r)rrrrr(szChannel.is_patterncCs|jjS)aReturns True until there are messages in channel or
        connection is subscribed to it.

        Can be used with ``while``:

        >>> ch = conn.pubsub_channels['chan:1']
        >>> while ch.is_active:
        ...     msg = await ch.get()   # may stuck for a long time

        )r�	exhausted)rrrr�	is_active-szChannel.is_active�encoding�decoderc�s�|jjrt��|jj�IdH}|tkr9dS|jrN|\}}|dk	ri|j|�}|dk	r�||�}|jr�||fS|S)z�Coroutine that waits for and returns a message.

        :raises aioredis.ChannelClosedError: If channel is unsubscribed
            and has no messages.
        N)rrr�getrr�decode)rrr�msgZdest_channelrrrr;s			
zChannel.getzutf-8c�s|jd|dtj�IdHS)zShortcut to get JSON messages.rrN)r�json�loads)rrrrr�get_jsonVszChannel.get_jsoncCs"t|ddd�d|d|�S)z�Same as get method but its native coroutine.

        Usage example:

        >>> async for msg in ch.iter():
        ...     print(msg)
        rcSs|jS)N)r)�chrrr�<lambda>cszChannel.iter.<locals>.<lambda>rr)�_IterHelper)rrrrrr�iterZs	zChannel.iterc�s9|js
dS|jj�s dS|jj�IdH|jS)z�Waits for message to become available in channel
        or channel is closed (unsubscribed).

        Possible usage:

        >>> while (await ch.wait_message()):
        ...     msg = await ch.get()
        FTN)rr�empty�wait)rrrr�wait_messagegs		zChannel.wait_messagecCs|jj|�dS)N)r�put)r�datarrr�
put_nowaityszChannel.put_nowaitcCs|jjs|jj�dS)z~Marks channel as inactive.

        Internal method, will be called from connection
        on `unsubscribe` command.
        N)r�closed�close)r�excrrrr0|sz
Channel.close)r�
__module__�__qualname__�__doc__rr�propertyrrrrr$r(r+r.r0rrrrrs
c@s:eZdZdZdd�Zdd�Zd	d
�ZdS)
r'�_ch�
_is_active�_args�_kwcOs(||_||_||_||_dS)N)r6r7r8r9)rr%r�args�kwrrrr�s			z_IterHelper.__init__cCs|S)Nr)rrrr�	__aiter__�sz_IterHelper.__aiter__c�sN|j|j�st�|jj|j|j�IdH}|dkrJt�|S)N)r7r6�StopAsyncIterationrr8r9)rr!rrr�	__anext__�s z_IterHelper.__anext__N)r6r7r8r9)rr2r3�	__slots__rr<r>rrrrr'�sr'c@s�eZdZdZdddd�Zdd�Zdd�Zd	d
�Zedd��Z	ed
d��Z
dddddd�Zdd�Zedd��Z
dd�Zdddddd�Zddd�Zdd�Zddd �ZdS)!r	a�Multi-producers, single-consumer Pub/Sub queue.

    Can be used in cases where a single consumer task
    must read messages from several different channels
    (where pattern subscriptions may not work well
    or channels can be added/removed dynamically).

    Example use case:

    >>> from aioredis.pubsub import Receiver
    >>> from aioredis.abc import AbcChannel
    >>> mpsc = Receiver(loop=loop)
    >>> async def reader(mpsc):
    ...     async for channel, msg in mpsc.iter():
    ...         assert isinstance(channel, AbcChannel)
    ...         print("Got {!r} in channel {!r}".format(msg, channel))
    >>> asyncio.ensure_future(reader(mpsc))
    >>> await redis.subscribe(mpsc.channel('channel:1'),
    ...                       mpsc.channel('channel:3'))
    ...                       mpsc.channel('channel:5'))
    >>> await redis.psubscribe(mpsc.pattern('hello'))
    >>> # publishing 'Hello world' into 'hello-channel'
    >>> # will print this message:
    Got b'Hello world' in channel b'hello-channel'
    >>> # when all is done:
    >>> await redis.unsubscribe('channel:1', 'channel:3', 'channel:5')
    >>> await redis.punsubscribe('hello')
    >>> mpsc.stop()
    >>> # any message received after stop() will be ignored.
    NcCsU|dkrtj�}|dkr-|j}td|�|_i|_||_dS)Nr
)�asyncioZget_event_loop�
check_stoprr�_refs�	_on_close)rr
Zon_closerrrr�s		zReceiver.__init__cCs(dj|jt|j�|jj��S)Nz-<Receiver is_active:{}, senders:{}, qsize:{}>)rr�lenrBrr)rrrrr�szReceiver.__repr__cCshtt|�|�}|df|jkrWt||dd�}||j|df<|S|j|dfS)zvCreate a channel.

        Returns ``_Sender`` object implementing
        :class:`~aioredis.abc.AbcChannel`.
        Fr)rr
rB�_Sender)rrZenc_namer%rrr�channel�s	zReceiver.channelcCsdtt|�|�}|df|jkrSt||dd�}||j|df<|j|dfS)z~Create a pattern channel.

        Returns ``_Sender`` object implementing
        :class:`~aioredis.abc.AbcChannel`.
        Tr)rr
rBrE)r�patternZenc_patternr%rrrrG�s	zReceiver.patterncCs#tjdd�|jj�D��S)zRead-only channels dict.cSs%i|]}|js||j�qSr)rr)�.0r%rrr�
<dictcomp>�s	z%Receiver.channels.<locals>.<dictcomp>)�types�MappingProxyTyperB�values)rrrr�channels�szReceiver.channelscCs#tjdd�|jj�D��S)zRead-only patterns dict.cSs%i|]}|jr||j�qSr)rr)rHr%rrrrI�s	z%Receiver.patterns.<locals>.<dictcomp>)rJrKrBrL)rrrr�patterns�szReceiver.patternsrrc�s�|jjrt��|jj�IdH}|tkr9dS|\}}|jrZ|\}}|dk	ru|j|�}|dk	r�||�}|jr�|||ffS||fS)a�Wait for and return pub/sub message from one of channels.

        Return value is either:

        * tuple of two elements: channel & message;

        * tuple of three elements: pattern channel, (target channel & message);

        * or None in case Receiver is not active or has just been stopped.

        :raises aioredis.ChannelClosedError: If listener is stopped
            and all messages have been received.
        N)rrrrrrr )rrr�objr%r!Zdest_chrrrr�s			zReceiver.getc�s<|jj�sdS|jjr#dS|jj�IdH|jS)z Blocks until new message appear.TFN)rr)r/r*r)rrrrr+szReceiver.wait_messagecCs0|jjrdStdd�|jj�D��S)z5Returns True if listener has any active subscription.Fcss|]}|jVqdS)N)r)rHr%rrr�	<genexpr>+sz%Receiver.is_active.<locals>.<genexpr>)rr�anyrBrL)rrrrr&szReceiver.is_activecCs|jj�dS)z�Stop receiving messages.

        All new messages after this call will be ignored,
        so you must call unsubscribe before stopping this listener.
        N)rr0)rrrr�stop-sz
Receiver.stopcCs"t|ddd�d|d|�S)z�Returns async iterator.

        Usage example:

        >>> async for ch, msg in mpsc.iter():
        ...     print(ch, msg)
        rcSs|jjS)N)rr)�rrrrr&BszReceiver.iter.<locals>.<lambda>rr)r')rrrrrrr(9s	z
Receiver.itercCs|js|j�dS)ZTBDN)rBrR)rrFr1rrrrAFs	zReceiver.check_stopcCs=|jjr#tjd||�dS|jj||f�dS)Nz9Pub/Sub listener message after stop: sender: %r, data: %r)rr/rZwarningr,)rr-�senderrrr�_put_nowaitQs
	
zReceiver._put_nowaitcCs3|jj|j|jf�|j|d|�dS)Nr1)rB�poprrrC)rrTr1rrr�_closeYszReceiver._close)rr2r3r4rrrFrGr5rMrNrr+rrRr(rArUrWrrrrr	�s
)	
c@s�eZdZdZdd�Zdd�Zedd��Zedd	��Zed
d��Z	dd
dd
dd�Z
dd�Zd
dd�Zd
S)rEzEWrite-Only Channel.

    Does not allow direct ``.get()`` calls.
    cCs8||_tt|�|�|_||_d|_dS)NF)�	_receiverrr
rr�_closed)rZreceiverrrrrrrds		z_Sender.__init__cCs%dj|jj|j|j|j�S)Nz,<{} name:{!r}, is_pattern:{}, receiver:{!r}>)rrrrrrX)rrrrrjs	z_Sender.__repr__cCs|jS)z Encoded channel name or pattern.)r)rrrrrosz_Sender.namecCs|jS)z0Set to True if channel is subscribed to pattern.)r)rrrrrtsz_Sender.is_patterncCs|jS)N)rY)rrrrrysz_Sender.is_activerNrc�std��dS)Nz.MPSC channel does not allow direct get() calls)�RuntimeError)rrrrrrr}sz_Sender.getcCs|jj|d|�dS)NrT)rXrU)rr-rrrr.�sz_Sender.put_nowaitcCs0|jr
dSd|_|jj|d|�dS)NTr1)rYrXrW)rr1rrrr0�s		z
_Sender.close)
rr2r3r4rrr5rrrrr.r0rrrrrE^srEc@s�eZdZdddd�Zdd�Zdd�Zd	d
�Zdd�Zed
d��Z	edd��Z
dd�Zdd�Zdd�Z
dS)rr
NcCs1tj�|_tjd|�|_d|_dS)Nr
F)�collections�dequerr@ZEvent�_eventrY)rr
rrrr�szClosableQueue.__init__c�s/x(|jp|js*|jj�IdHqWdS)NT)rrYr]r*)rrrrr*�szClosableQueue.waitc�sO|j�IdH|jr&|jr&tS|jj�}|jsK|jj�|S)N)r*rrYr�popleftr]�clear)r�itemrrrr�s	
zClosableQueue.getcCs.|jr
dS|jj|�|jj�dS)N)rYr�appendr]�set)rr`rrrr,�s	zClosableQueue.putcCsd|_|jj�dS)z,Mark queue as closed and notify all waiters.TN)rYr]rb)rrrrr0�s	zClosableQueue.closecCs|jS)N)rY)rrrrr/�szClosableQueue.closedcCs|jo|jS)N)rYr)rrrrr�szClosableQueue.exhaustedcCs|jS)N)r)rrrrr)�szClosableQueue.emptycCs
t|j�S)N)rDr)rrrrr�szClosableQueue.qsizecCs.|jrdnd}dj|t|j��S)Nr/�openz<Queue {} size:{}>)rYrrDr)rr/rrrr�szClosableQueue.__repr__)rr2r3rr*rr,r0r5r/rr)rrrrrrr�sr)r@r"rJr[�abcr�utilr�errorsr�logr�__all__�objectrrr'r	rErrrrr�<module>s 		p�/