ó
ßüÚ\c           @   s  d  d l  m Z m 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
 m Z d d l m Z d d l m Z d d l m Z e j e ƒ Z d	 e f d
 „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   iÿÿÿÿ(   t   defaultdictt   dequeN(   t   geni   (   t   finalizet   get_thread_identity(   t   CommClosedError(   t   sync(   t   to_serializet   PubSubSchedulerExtensionc           B   sz   e  Z d  Z d „  Z d d d d „ Z d d d d d „ Z d d d d „ Z d d d d d „ Z d d d d d „ Z	 RS(   s@    Extend Dask's scheduler with routes to handle PubSub machinery c         C   s¢   | |  _  t t ƒ |  _ t t ƒ |  _ t t ƒ |  _ |  j  j j i |  j d 6ƒ |  j  j	 j i |  j
 d 6|  j d 6|  j d 6|  j d 6ƒ |  |  j  j d <d  S(   Nt   pubsub_add_publishers   pubsub-add-subscribers   pubsub-remove-publishers   pubsub-remove-subscribers
   pubsub-msgt   pubsub(   t	   schedulerR    t   sett
   publisherst   subscriberst   client_subscriberst   handlerst   updatet   add_publishert   stream_handlerst   add_subscribert   remove_publishert   remove_subscribert   handle_messaget
   extensions(   t   selfR   (    (    s1   lib/python2.7/site-packages/distributed/pubsub.pyt   __init__   s    	


c         C   sl   t  j d | | ƒ |  j | j | ƒ i d „  |  j | Dƒ d 6| |  j k og t |  j | ƒ d k d 6S(   Ns   Add publisher: %s %sc         S   s   i  |  ] } i  | “ q S(    (    (   t   .0t   addr(    (    s1   lib/python2.7/site-packages/distributed/pubsub.pys
   <dictcomp>+   s   	 R   i    s   publish-scheduler(   t   loggert   debugR   t   addR   R   t   len(   R   t   commt   namet   worker(    (    s1   lib/python2.7/site-packages/distributed/pubsub.pyR   '   s    c         C   sä   | rp t  j d | | ƒ |  j | j | ƒ x° |  j | D]. } |  j j | i d d 6| d 6| d 6ƒ q; Wnp | rà t  j d | | ƒ x= |  j | D]. } |  j j | i d d 6| d 6t d 6ƒ q— W|  j | j | ƒ n  d  S(	   Ns   Add worker subscriber: %s %ss   pubsub-add-subscribert   opt   addressR"   s   Add client subscriber: %s %ss   pubsub-publish-schedulert   publish(	   R   R   R   R   R   R   t   worker_sendt   TrueR   (   R   R!   R"   R#   t   clientt   pub(    (    s1   lib/python2.7/site-packages/distributed/pubsub.pyR   0   s    	#	 c         C   st   | |  j  | k rp t j d | | ƒ |  j  | j | ƒ |  j | rp |  j  | rp |  j | =|  j  | =qp n  d  S(   Ns   Remove publisher: %s %s(   R   R   R   t   removeR   (   R   R!   R"   R#   (    (    s1   lib/python2.7/site-packages/distributed/pubsub.pyR   B   s    
c         C   sA  | rp t  j d | | ƒ |  j | j | ƒ xÊ |  j | D]. } |  j j | i d d 6| d 6| d 6ƒ q; WnŠ | rú t  j d | | ƒ |  j | j | ƒ |  j | sú |  j | =x@ |  j | D]. } |  j j | i d d 6| d 6t d 6ƒ qÂ Wqú n  |  j | r=|  j | r=t  j d	 | ƒ |  j | =|  j | =n  d  S(
   Ns   Add worker subscriber: %s %ss   pubsub-remove-subscriberR$   R%   R"   s   Add client subscriber: %s %ss   pubsub-publish-schedulerR&   s   Remove PubSub topic %s(	   R   R   R   R+   R   R   R'   R   t   False(   R   R!   R"   R#   R)   R*   (    (    s1   lib/python2.7/site-packages/distributed/pubsub.pyR   K   s.    	#
	
c         C   sË   x{ t  |  j | ƒ D]f } y0 |  j j | j i d d 6| d 6| d 6ƒ Wq t t f k
 ry |  j d | d | ƒ q Xq W| rÇ x@ |  j | D]. } |  j j	 | i d d 6| d 6| d 6ƒ q’ Wn  d  S(   Ns
   pubsub-msgR$   R"   t   msgR)   (
   t   listR   R   t   client_commst   sendt   KeyErrorR   R   R   R'   (   R   R"   R-   R#   R)   t   ct   sub(    (    s1   lib/python2.7/site-packages/distributed/pubsub.pyR   h   s     	N(
   t   __name__t
   __module__t   __doc__R   t   NoneR   R   R   R   R   (    (    (    s1   lib/python2.7/site-packages/distributed/pubsub.pyR      s   			t   PubSubWorkerExtensionc           B   se   e  Z d  Z d „  Z d d d „ Z d d d „ Z d d d „ Z d d d „ Z d „  Z	 d „  Z
 RS(	   s=    Extend Dask's Worker with routes to handle PubSub machinery c         C   sŽ   | |  _  |  j  j j i |  j d 6|  j d 6|  j d 6|  j d 6ƒ t t j	 ƒ |  _
 t t j	 ƒ |  _ t d „  ƒ |  _ |  |  j  j d <d  S(   Ns   pubsub-add-subscribers   pubsub-remove-subscribers
   pubsub-msgs   pubsub-publish-schedulerc           S   s   t  S(   N(   R,   (    (    (    s1   lib/python2.7/site-packages/distributed/pubsub.pyt   <lambda>ˆ   t    R
   (   R#   R   R   R   R   R   t   publish_schedulerR    t   weakreft   WeakSetR   R   t   publish_to_schedulerR   (   R   R#   (    (    s1   lib/python2.7/site-packages/distributed/pubsub.pyR   {   s    	


c         K   s)   x" |  j  | D] } | | j | <q Wd  S(   N(   R   R   (   R   R"   R%   t   infoR*   (    (    s1   lib/python2.7/site-packages/distributed/pubsub.pyR   Œ   s    c         C   s&   x |  j  | D] } | j | =q Wd  S(   N(   R   R   (   R   R"   R%   R*   (    (    s1   lib/python2.7/site-packages/distributed/pubsub.pyR      s    c         C   s   | |  j  | <d  S(   N(   R>   (   R   R"   R&   (    (    s1   lib/python2.7/site-packages/distributed/pubsub.pyR;   ”   s    c         C   s1   x* |  j  j | g  ƒ D] } | j | ƒ q Wd  S(   N(   R   t   gett   _put(   R   R"   R-   R3   (    (    s1   lib/python2.7/site-packages/distributed/pubsub.pyR   —   s    c         C   s   |  j  j j |  j ƒ d  S(   N(   R#   t   loopt   add_callbackt   cleanup(   R   (    (    s1   lib/python2.7/site-packages/distributed/pubsub.pyt   trigger_cleanup›   s    c         C   sÚ   xc t  |  j ƒ j ƒ  D]L \ } } t | ƒ s i d d 6| d 6} |  j j j | ƒ |  j | =q q Wxm t  |  j ƒ j ƒ  D]V \ } } t | ƒ s| i d d 6| d 6} |  j j j | ƒ |  j | =|  j | =q| q| Wd  S(   Ns   pubsub-remove-subscriberR$   R"   s   pubsub-remove-publisher(	   t   dictR   t   itemsR    R#   t   batched_streamR0   R   R>   (   R   R"   t   sR-   t   p(    (    s1   lib/python2.7/site-packages/distributed/pubsub.pyRD   ž   s    ""
N(   R4   R5   R6   R   R7   R   R   R;   R   RE   RD   (    (    (    s1   lib/python2.7/site-packages/distributed/pubsub.pyR8   x   s   		t   PubSubClientExtensionc           B   s8   e  Z d  Z d „  Z d d d „ Z d „  Z d „  Z RS(   s?    Extend Dask's Client with handlers to handle PubSub machinery c         C   sL   | |  _  |  j  j j i |  j d 6ƒ t t j ƒ |  _ |  |  j  j d <d  S(   Ns
   pubsub-msgR
   (	   R)   t   _stream_handlersR   R   R    R<   R=   R   R   (   R   R)   (    (    s1   lib/python2.7/site-packages/distributed/pubsub.pyR   °   s    	c         C   sZ   x" |  j  | D] } | j | ƒ q W|  j  | sV |  j j j i d d 6| d 6ƒ n  d  S(   Ns   pubsub-remove-subscribersR$   R"   (   R   RA   R)   t   scheduler_commR0   (   R   R"   R-   R3   (    (    s1   lib/python2.7/site-packages/distributed/pubsub.pyR   ·   s
    c         C   s   |  j  j j |  j ƒ d  S(   N(   R)   RB   RC   RD   (   R   (    (    s1   lib/python2.7/site-packages/distributed/pubsub.pyRE   À   s    c         C   sT   xM |  j  j ƒ  D]< \ } } | s i d d 6| d 6} |  j j j | ƒ q q Wd  S(   Ns   pubsub-remove-subscriberR$   R"   (   R   RG   R)   RM   R0   (   R   R"   RI   R-   (    (    s1   lib/python2.7/site-packages/distributed/pubsub.pyRD   Ã   s    N(   R4   R5   R6   R   R7   R   RE   RD   (    (    (    s1   lib/python2.7/site-packages/distributed/pubsub.pyRK   ­   s
   			t   Pubc           B   sA   e  Z d  Z d d d „ Z e j d „  ƒ Z d „  Z d „  Z	 RS(   s×	   Publish data with Publish-Subscribe pattern

    This allows clients and workers to directly communicate data between each
    other with a typical Publish-Subscribe pattern.  This involves two
    components,

    Pub objects, into which we put data:

        >>> pub = Pub('my-topic')
        >>> pub.put(123)

    And Sub objects, from which we collect data:

        >>> sub = Sub('my-topic')
        >>> sub.get()
        123

    Many Pub and Sub objects can exist for the same topic.  All data sent from
    any Pub will be sent to all Sub objects on that topic that are currently
    connected.  Pub's and Sub's find each other using the scheduler, but they
    communicate directly with each other without coordination from the
    scheduler.

    Pubs and Subs use the central scheduler to find each other, but not to
    mediate the communication.  This means that there is very little additional
    latency or overhead, and they are appropriate for very frequent data
    transfers.  For context, most data transfer first checks with the scheduler to find which
    workers should participate, and then does direct worker-to-worker
    transfers.  This checking in with the scheduler provides some stability
    guarantees, but also adds in a few extra network hops.  PubSub doesn't do
    this, and so is faster, but also can easily drop messages if Pubs or Subs
    disappear without notice.

    When using a Pub or Sub from a Client all communications will be routed
    through the scheduler.  This can cause some performance degredation.  Pubs
    an Subs only operate at top-speed when they are both on workers.

    Parameters
    ----------
    name: object (msgpack serializable)
        The name of the group of Pubs and Subs on which to participate

    Examples
    --------
    >>> pub = Pub('my-topic')
    >>> sub = Sub('my-topic')
    >>> pub.put([1, 2, 3])
    >>> sub.get()
    [1, 2, 3]

    You can also use sub within a for loop:

    >>> for msg in sub:  # doctest: +SKIP
    ...     print(msg)

    or an async for loop

    >>> async for msg in sub:  # doctest: +SKIP
    ...     print(msg)

    Similarly the ``.get`` method will return an awaitable if used by an async
    client or within the IOLoop thread of a worker

    >>> await sub.get()  # doctest: +SKIP

    You can see the set of connected worker subscribers by looking at the
    ``.subscribers`` attribute:

    >>> pub.subscribers
    {'tcp://...': {},
     'tcp://...': {}}

    See Also
    --------
    Sub
    c         C   sZ  | d  k r[ | d  k r[ d d l m } m } y | ƒ  } Wq[ t k
 rW | ƒ  } q[ Xn  t ƒ  |  _ | |  _ | |  _ | s‹ | s‹ t	 ‚ |  j rµ |  j j
 |  _
 |  j j |  _ n* |  j rß |  j j
 |  _
 |  j j |  _ n  | |  _ t |  _ g  |  _ |  j j |  j ƒ |  j rV|  j j d } |  j j | j | j |  ƒ t |  | j ƒ n  d  S(   Niÿÿÿÿ(   t
   get_workert
   get_clientR
   (   R7   t   distributedRO   RP   t	   ExceptionRF   R   R#   R)   t   AssertionErrorR   RB   R"   R,   t   _startedt   _bufferRC   t   _startR   R   R   R   RE   (   R   R"   R#   R)   RO   RP   R
   (    (    s1   lib/python2.7/site-packages/distributed/pubsub.pyR     s0    								c         c   sž   |  j  ri |  j j d |  j d |  j  j ƒ V} |  j  j d } |  j j | d ƒ | d | j |  j <n  t	 |  _
 x |  j D] } |  j | ƒ q| W|  j 2d  S(   NR"   R#   R
   R   s   publish-scheduler(   R#   R   R	   R"   R%   R   R   R   R>   R(   RT   RU   t   put(   R   t   resultR
   R-   (    (    s1   lib/python2.7/site-packages/distributed/pubsub.pyRV   7  s    		c         C   sÇ   |  j  s |  j j | ƒ d  Si d d 6|  j d 6t | ƒ d 6} |  j r¤ x$ |  j D] } |  j j | | ƒ qT W|  j j d j	 |  j rÃ |  j j
 j | ƒ qÃ n |  j rÃ |  j j j | ƒ n  d  S(   Ns
   pubsub-msgR$   R"   R-   R
   (   RT   RU   t   appendR"   R   R#   R   t   send_to_workerR   R>   RH   R0   R)   RM   (   R   R-   t   dataR3   (    (    s1   lib/python2.7/site-packages/distributed/pubsub.pyRA   G  s    	$		c         C   s   |  j  j |  j | ƒ d S(   s4    Publish a message to all subscribers of this topic N(   RB   RC   RA   (   R   R-   (    (    s1   lib/python2.7/site-packages/distributed/pubsub.pyRW   W  s    N(
   R4   R5   R6   R7   R   R   t	   coroutineRV   RA   RW   (    (    (    s1   lib/python2.7/site-packages/distributed/pubsub.pyRN   Ê   s
   L	t   Subc           B   si   e  Z d  Z d d d „ Z e j d d „ ƒ Z e Z d d „ Z	 e	 Z
 Z d „  Z d „  Z d „  Z RS(   sc    Subscribe to a Publish/Subscribe topic

    See Also
    --------
    Pub: for full docstring
    c         C   s£  | d  k r[ | d  k r[ d d l m } m } y | ƒ  } Wq[ t k
 rW | ƒ  } q[ Xn  | |  _ | |  _ |  j rˆ |  j j |  _ n |  j r£ |  j j |  _ n  | |  _ t	 ƒ  |  _
 t j j ƒ  |  _ |  j ræ |  j j d } n |  j r|  j j d } n  |  j j | j | j |  ƒ i d d 6|  j d 6} |  j r^|  j j |  j j j | ƒ n1 |  j r†|  j j |  j j j | ƒ n	 t ƒ  ‚ t |  | j ƒ d  S(   Niÿÿÿÿ(   RO   RP   R
   s   pubsub-add-subscriberR$   R"   (   R7   t   distributed.workerRO   RP   RR   R#   R)   RB   R"   R   t   buffert   tornadot   lockst	   Conditiont	   conditionR   RC   R   R   RH   R0   RM   R   RE   (   R   R"   R#   R)   RO   RP   R
   R-   (    (    s1   lib/python2.7/site-packages/distributed/pubsub.pyR   d  s6    										c         c   s½   | d  k	 r! t j d | ƒ } n  t j j ƒ  } xn |  j s  | d  k	 rƒ | t j j ƒ  | } | j ƒ  d k  r‰ t j ƒ  ‚ q‰ n d  } |  j j	 d | ƒ Vq3 Wt j
 |  j j ƒ  ƒ ‚ d  S(   Nt   secondsi    t   timeout(   R7   t   datetimet	   timedeltat   nowR_   t   total_secondsR   t   TimeoutErrorRc   t   waitt   Returnt   popleft(   R   Re   t   startt   timeout2(    (    s1   lib/python2.7/site-packages/distributed/pubsub.pyt   _get‡  s    c         C   st   |  j  r" |  j  j |  j d | ƒS|  j j t ƒ  k rA |  j ƒ  S|  j rW |  j j ƒ  St |  j |  j d | ƒSd S(   s    Get a single message Re   N(	   R)   R   Rp   R#   t	   thread_idR   R_   Rm   RB   (   R   Re   (    (    s1   lib/python2.7/site-packages/distributed/pubsub.pyR@   ™  s    	
	c         C   s   |  S(   N(    (   R   (    (    s1   lib/python2.7/site-packages/distributed/pubsub.pyt   __iter__¦  s    c         C   s   |  S(   N(    (   R   (    (    s1   lib/python2.7/site-packages/distributed/pubsub.pyt	   __aiter__©  s    c         C   s!   |  j  j | ƒ |  j j ƒ  d  S(   N(   R_   RY   Rc   t   notify(   R   R-   (    (    s1   lib/python2.7/site-packages/distributed/pubsub.pyRA   ¬  s    N(   R4   R5   R6   R7   R   R   R\   Rp   t	   __anext__R@   t   nextt   __next__Rr   Rs   RA   (    (    (    s1   lib/python2.7/site-packages/distributed/pubsub.pyR]   \  s   #
		(   t   collectionsR    R   Rf   t   loggingR<   t   tornado.locksR`   R   t   compatibilityR   R   t   coreR   t   utilsR   t   protocol.serializeR   t	   getLoggerR4   R   t   objectR   R8   RK   RN   R]   (    (    (    s1   lib/python2.7/site-packages/distributed/pubsub.pyt   <module>   s   g5’