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.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�|dks$t|�s$t|��|jjr9t��|jj�IdH}|tkr]dS|jrr|\}}|dk	r�|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)	�callable�AssertionErrorr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�excrrrr2|sz
Channel.close)r�
__module__�__qualname__�__doc__rr�propertyrrrr!r&r*r-r0r2rrrrrs
c@s:eZdZdZdd�Zdd�Zd	d
�ZdS)
r)�_ch�
_is_active�_args�_kwcOs(||_||_||_||_dS)N)r8r9r:r;)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)r9r8�StopAsyncIterationr!r:r;)rr#rrr�	__anext__�s z_IterHelper.__anext__N)r8r9r:r;)rr4r5�	__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.
    NcCs|dks*t|�s*td|f��|dkrBtj�}|dkrW|j}td|�|_i|_||_dS)Nz!on_close must be None or callabler
)	rr �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�lenrDr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
rD�_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
rDrG)r�patternZenc_patternr'rrrrI�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�MappingProxyTyperD�values)rrrr�channels�szReceiver.channelscCs#tjdd�|jj�D��S)zRead-only patterns dict.cSs%i|]}|jr||j�qSr)rr)rJr'rrrrK�s	z%Receiver.patterns.<locals>.<dictcomp>)rLrMrDrN)rrrr�patterns�szReceiver.patternsrrc�s�|dks$t|�s$t|��|jjr9t��|jj�IdH}|tkr]dS|\}}|jr~|\}}|dk	r�|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)	rr rrrr!rrr")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+r1r,r)rrrrr-szReceiver.wait_messagecCs0|jjrdStdd�|jj�D��S)z5Returns True if listener has any active subscription.Fcss|]}|jVqdS)N)r)rJr'rrr�	<genexpr>+sz%Receiver.is_active.<locals>.<genexpr>)rr�anyrDrN)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r2)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)rDrT)rrHr3rrrrCFs	zReceiver.check_stopcCs=|jjr#tjd||�dS|jj||f�dS)Nz9Pub/Sub listener message after stop: sender: %r, data: %r)rr1rZwarningr.)rr/�senderrrr�_put_nowaitQs
	
zReceiver._put_nowaitcCs3|jj|j|jf�|j|d|�dS)Nr3)rD�poprrrE)rrVr3rrr�_closeYszReceiver._close)rr4r5r6rrrHrIr7rOrPr!r-rrTr*rCrWrYrrrrr	�s
)	
c@s�eZdZdZdd�Zdd�Zedd��Zedd	��Zed
d��Z	dd
dd
dd�Z
dd�Zd
dd�Zd
S)rGzEWrite-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}>)rrrrrrZ)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)r[)rrrrrysz_Sender.is_activerNrc�std��dS)Nz.MPSC channel does not allow direct get() calls)�RuntimeError)rrrrrrr!}sz_Sender.getcCs|jj|d|�dS)NrV)rZrW)rr/rrrr0�sz_Sender.put_nowaitcCs0|jr
dSd|_|jj|d|�dS)NTr3)r[rZrY)rr3rrrr2�s		z
_Sender.close)
rr4r5r6rrr7rrrr!r0r2rrrrrG^srGc@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rB�Event�_eventr[)rr
rrrr�szClosableQueue.__init__c�s/x(|jp|js*|jj�IdHqWdS)NT)rr[r`r,)rrrrr,�szClosableQueue.waitc�s||j�IdH|js<|js<td|j|jf��|jrS|jrStS|jj�}|jsx|jj�|S)NzUnexpected queue state)r,rr[r r�popleftr`�clear)r�itemrrrr!�s	
zClosableQueue.getcCs.|jr
dS|jj|�|jj�dS)N)r[r�appendr`�set)rrcrrrr.�s	zClosableQueue.putcCsd|_|jj�dS)z,Mark queue as closed and notify all waiters.TN)r[r`re)rrrrr2�s	zClosableQueue.closecCs|jS)N)r[)rrrrr1�szClosableQueue.closedcCs|jo|jS)N)r[r)rrrrr�szClosableQueue.exhaustedcCs|jS)N)r)rrrrr+�szClosableQueue.emptycCs
t|j�S)N)rFr)rrrrr�szClosableQueue.qsizecCs.|jrdnd}dj|t|j��S)Nr1�openz<Queue {} size:{}>)r[rrFr)rr1rrrr�szClosableQueue.__repr__)rr4r5rr,r!r.r2r7r1rr+rrrrrrr�sr)rBr$rLr]�abcr�utilr�errorsr�logr�__all__�objectrrr)r	rGrrrrr�<module>s 		p�/