ó
ßüÚ\c           @` só   d  d l  m Z m Z m Z d  d l m Z d  d l Z d  d l Z d  d l Z d  d l	 m
 Z
 d  d l Z	 d d l m Z m Z m Z d d l m Z m Z m Z d d l m Z e j e ƒ Z d	 e f d
 „  ƒ  YZ d e f d „  ƒ  YZ d S(   i    (   t   print_functiont   divisiont   absolute_import(   t   defaultdictN(   t   geni   (   t   Futuret   _get_global_clientt   Client(   t   tokeyt   synct   thread_state(   t
   get_clientt   QueueExtensionc           B` s§   e  Z d  Z d „  Z d	 d	 d	 d d „ Z d	 d	 d	 d „ Z e j d	 d	 d	 d	 d	 d	 d „ ƒ Z	 d	 d	 d	 d „ Z
 e j d	 d	 d	 d	 e d „ ƒ Z d	 d	 d	 d „ Z RS(
   sÈ    An extension for the scheduler to manage queues

    This adds the following routes to the scheduler

    *  queue_create
    *  queue_release
    *  queue_put
    *  queue_get
    *  queue_size
    c         C` s©   | |  _  t ƒ  |  _ t ƒ  |  _ t d „  ƒ |  _ |  j  j j i |  j d 6|  j	 d 6|  j
 d 6|  j d 6ƒ |  j  j j i |  j d 6|  j d 6ƒ |  |  j  j d <d  S(	   Nc           S` s   d S(   Ni    (    (    (    (    s1   lib/python2.7/site-packages/distributed/queues.pyt   <lambda>"   t    t   queue_createt	   queue_putt	   queue_gett   queue_qsizes   queue-future-releaset   queue_releaset   queues(   t	   schedulert   dictR   t   client_refcountR   t   future_refcountt   handlerst   updatet   createt   putt   gett   qsizet   stream_handlerst   future_releaset   releaset
   extensions(   t   selfR   (    (    s1   lib/python2.7/site-packages/distributed/queues.pyt   __init__   s    	


i    c         C` sR   | |  j  k r; t j  j d | ƒ |  j  | <d |  j | <n |  j | c d 7<d  S(   Nt   maxsizei   (   R   t   tornadot   QueueR   (   R#   t   streamt   namet   clientR%   (    (    s1   lib/python2.7/site-packages/distributed/queues.pyR   3   s    c         C` s¨   | |  j  k r d  S|  j | c d 8<|  j | d k r¤ |  j | =|  j  | j } |  j  | =|  j j d g  | D]  } | d d k rp | d ^ qp d d | ƒ n  d  S(	   Ni   i    t   keyst   typeR   t   valueR*   s   queue-%s(   R   R   t   _queueR   t   client_releases_keys(   R#   R(   R)   R*   t   futurest   d(    (    s1   lib/python2.7/site-packages/distributed/queues.pyR!   :   s    

-c         c` s°   | d  k	 r\ i d d 6| d 6} |  j | | f c d 7<|  j j d | g d d | ƒ n i d d 6| d 6} | d  k	 r‘ t j d	 | ƒ } n  |  j | j | d
 | ƒVd  S(   NR   R,   R-   i   R+   R*   s   queue-%st   msgpackt   secondst   timeout(   t   NoneR   R   t   client_desires_keyst   datetimet	   timedeltaR   R   (   R#   R(   R)   t   keyt   dataR*   R4   t   record(    (    s1   lib/python2.7/site-packages/distributed/queues.pyR   H   s    #c         C` si   |  j  | | f c d 8<|  j  | | f d k re |  j j d | g d d | ƒ |  j  | | f =n  d  S(   Ni   i    R+   R*   s   queue-%s(   R   R   R/   (   R#   R)   R9   R*   (    (    s1   lib/python2.7/site-packages/distributed/queues.pyR    V   s     c         #` sA  ‡  f d †  } | rç ˆ  j  | } g  } | t k rd x | j ƒ  s` | j ƒ  V}	 | j |	 ƒ q7 WnR | d  k	 r… d }
 t |
 ƒ ‚ n  x. t | ƒ D]  } | j ƒ  V}	 | j |	 ƒ q’ Wg  | D] } | | ƒ ^ q½ } t j	 | ƒ ‚ nV | d  k	 rt
 j d | ƒ } n  ˆ  j  | j d | ƒ V}	 | |	 ƒ }	 t j	 |	 ƒ ‚ d  S(   Nc         ` s–   |  d d k r’ |  j  ƒ  }  |  d } ˆ  j j j | ƒ } | d	 k	 rP | j n d } | |  d <| d k r’ | j j |  d <| j j |  d <q’ n  |  S(
   s    Add task status if known R,   R   R-   t   lostt   statet   erredt	   exceptiont	   tracebackN(	   t   copyR   t   tasksR   R5   R=   t   exception_blameR?   R@   (   R;   R9   t   tsR=   (   R#   (    s1   lib/python2.7/site-packages/distributed/queues.pyt   process^   s    

sN   Dask queues don't support simultaneous use of integer batch sizes and timeoutsR3   R4   (   R   t   Truet   emptyR   t   appendR5   t   NotImplementedErrort   rangeR   t   ReturnR7   R8   (   R#   R(   R)   R*   R4   t   batchRE   t   qt   outR;   t   msgt   it   o(    (   R#   s1   lib/python2.7/site-packages/distributed/queues.pyR   \   s*    c         C` s   |  j  | j ƒ  S(   N(   R   R   (   R#   R(   R)   R*   (    (    s1   lib/python2.7/site-packages/distributed/queues.pyR   ‡   s    N(   t   __name__t
   __module__t   __doc__R$   R5   R   R!   R   t	   coroutineR   R    t   FalseR   R   (    (    (    s1   lib/python2.7/site-packages/distributed/queues.pyR      s   
	*R'   c           B` s§   e  Z d  Z d d d d „ Z d „  Z e j d d „ ƒ Z d d „ Z	 d e
 d „ Z d „  Z e j d e
 d „ ƒ Z e j d	 „  ƒ Z d
 „  Z d „  Z d „  Z RS(   s©   Distributed Queue

    This allows multiple clients to share futures or small bits of data between
    each other with a multi-producer/multi-consumer queue.  All metadata is
    sequentialized through the scheduler.

    Elements of the Queue must be either Futures or msgpack-encodable data
    (ints, strings, lists, dicts).  All data is sent through the scheduler so
    it is wise not to send large objects.  To share large objects scatter the
    data and share the future instead.

    .. warning::

       This object is experimental and has known issues in Python 2

    Examples
    --------
    >>> from dask.distributed import Client, Queue  # doctest: +SKIP
    >>> client = Client()  # doctest: +SKIP
    >>> queue = Queue('x')  # doctest: +SKIP
    >>> future = client.submit(f, x)  # doctest: +SKIP
    >>> queue.put(future)  # doctest: +SKIP

    See Also
    --------
    Variable: shared variable between clients
    i    c         C` s®   | p t  ƒ  |  _ | p( d t j ƒ  j |  _ |  j j sL t t d t	 ƒ rs |  j j
 j d |  j d | ƒ |  _ n7 t |  j j |  j j
 j d |  j d | ƒt j |  _ d  S(   Ns   queue-t   on_event_loop_threadR)   R%   (   R   R*   t   uuidt   uuid4t   hexR)   t   asynchronoust   getattrR
   RV   R   R   t   _startedR	   t   loopR   t   moment(   R#   R)   R*   R%   (    (    s1   lib/python2.7/site-packages/distributed/queues.pyR$   ¨   s    		c         ` s%   t  j ‡  f d †  ƒ } | ƒ  j ƒ  S(   Nc           3` s   ˆ  j  Vt j ˆ  ƒ ‚ d  S(   N(   R]   R   RK   (    (   R#   (    s1   lib/python2.7/site-packages/distributed/queues.pyt   _»   s    (   R   RU   t	   __await__(   R#   R`   (    (   R#   s1   lib/python2.7/site-packages/distributed/queues.pyRa   º   s    c         c` sk   t  | t ƒ rA |  j j j d t | j ƒ d | d |  j ƒ Vn& |  j j j d | d | d |  j ƒ Vd  S(   NR9   R4   R)   R:   (   t
   isinstanceR   R*   R   R   R   R9   R)   (   R#   R-   R4   (    (    s1   lib/python2.7/site-packages/distributed/queues.pyt   _putÂ   s
    #c         K` s   |  j  j |  j | d | | S(   s    Put data into the queue R4   (   R*   R	   Rc   (   R#   R-   R4   t   kwargs(    (    s1   lib/python2.7/site-packages/distributed/queues.pyR   Í   s    c         K` s"   |  j  j |  j d | d | | S(   sœ   Get data from the queue

        Parameters
        ----------
        timeout: Number (optional)
            Time in seconds to wait before timing out
        batch: boolean, int (optional)
            If True then return all elements currently waiting in the queue.
            If an integer than return that many elements from the queue
            If False (default) then return one item at a time
         R4   RL   (   R*   R	   t   _get(   R#   R4   RL   Rd   (    (    s1   lib/python2.7/site-packages/distributed/queues.pyR   Ñ   s    c         K` s   |  j  j |  j |  S(   s)    Current number of elements in the queue (   R*   R	   t   _qsize(   R#   Rd   (    (    s1   lib/python2.7/site-packages/distributed/queues.pyR   ß   s    c         #` sz   ˆ  j  j j d | d ˆ  j d | ƒ V} ‡  f d †  } | t k rR | | ƒ } n t t | | ƒ ƒ } t j | ƒ ‚ d  S(   NR4   R)   RL   c         ` s¡   |  d d k r“ t  |  d ˆ  j d t d |  d ƒ} |  d d k rd | j j |  d |  d ƒ n  ˆ  j j i d	 d
 6ˆ  j d 6|  d d 6ƒ n
 |  d } | S(   NR,   R   R-   t   informR=   R>   R?   R@   s   queue-future-releaset   opR)   R9   (   R   R*   RF   t   _statet	   set_errort   _send_to_schedulerR)   (   R1   R-   (   R#   (    s1   lib/python2.7/site-packages/distributed/queues.pyRE   é   s    &	&
(	   R*   R   R   R)   RV   t   listt   mapR   RK   (   R#   R4   RL   t   respRE   t   result(    (   R#   s1   lib/python2.7/site-packages/distributed/queues.pyRe   ã   s    c         c` s/   |  j  j j d |  j ƒ V} t j | ƒ ‚ d  S(   NR)   (   R*   R   R   R)   R   RK   (   R#   Ro   (    (    s1   lib/python2.7/site-packages/distributed/queues.pyRf   ý   s    c         C` s:   |  j  j d k r6 |  j  j i d d 6|  j d 6ƒ n  d  S(   Nt   runningR   Rh   R)   (   R*   t   statusRk   R)   (   R#   (    (    s1   lib/python2.7/site-packages/distributed/queues.pyt   close  s    c         C` s   |  j  |  j j j f S(   N(   R)   R*   R   t   address(   R#   (    (    s1   lib/python2.7/site-packages/distributed/queues.pyt   __getstate__  s    c         C` sz   | \ } } y( t  | ƒ } | j j | k s3 t ‚ Wn) t t f k
 r_ t | d t ƒ} n X|  j d | d | ƒ d  S(   Nt   set_as_defaultR)   R*   (   R   R   Rs   t   AssertionErrort   AttributeErrorR   RV   R$   (   R#   R=   R)   Rs   R*   (    (    s1   lib/python2.7/site-packages/distributed/queues.pyt   __setstate__	  s    N(   RR   RS   RT   R5   R$   Ra   R   RU   Rc   R   RV   R   R   Re   Rf   Rr   Rt   Rx   (    (    (    s1   lib/python2.7/site-packages/distributed/queues.pyR'   ‹   s   	
			(   t
   __future__R    R   R   t   collectionsR   R7   t   loggingRX   R&   R   t   tornado.queuesR*   R   R   R   t   utilsR   R	   R
   t   workerR   t	   getLoggerRR   t   loggert   objectR   R'   (    (    (    s1   lib/python2.7/site-packages/distributed/queues.pyt   <module>   s   y