ó
ßüÚ\c           @   s   d  d l  Z  d  d l Z d  d l m Z d  d l Z d d l m Z d d l m Z m	 Z	 m
 Z
 m Z e  j e ƒ Z d e f d „  ƒ  YZ d S(	   iÿÿÿÿN(   t   refi   (   t   Adaptivei   (   t   format_bytest   PeriodicCallbackt
   log_errorst   ignoringt   Clusterc           B   sY   e  Z d  Z d „  Z e d „  ƒ Z e d „  ƒ Z d „  Z d „  Z d „  Z	 d „  Z
 RS(   sê   Superclass for cluster objects

    This expects a local Scheduler defined on the object.  It provides
    common methods and an IPython widget display.

    Clusters inheriting from this class should provide the following:

    1.  A local ``Scheduler`` object at ``.scheduler``
    2.  scale_up and scale_down methods as defined below::

        def scale_up(self, n: int):
            ''' Brings total worker count up to ``n`` '''

        def scale_down(self, workers: List[str]):
            ''' Close the workers with the given addresses '''

    This will provide a general ``scale`` method as well as an IPython widget
    for display.

    Examples
    --------

    >>> from distributed.deploy import Cluster
    >>> class MyCluster(cluster):
    ...     def scale_up(self, n):
    ...         ''' Bring the total worker count up to n '''
    ...         pass
    ...     def scale_down(self, workers):
    ...         ''' Close the workers with the given addresses '''
    ...         pass

    >>> cluster = MyCluster()
    >>> cluster.scale(5)                       # scale manually
    >>> cluster.adapt(minimum=1, maximum=100)  # scale automatically

    See Also
    --------
    LocalCluster: a simple implementation with local workers
    c         K   sm   t  t ƒ  |  j j ƒ  Wd QXt |  d ƒ s; i  |  _ n  |  j j | ƒ t |  j |  |  j  |  _ |  j S(   s¿    Turn on adaptivity

        For keyword arguments see dask.distributed.Adaptive

        Examples
        --------
        >>> cluster.adapt(minimum=0, maximum=10, interval='500ms')
        Nt   _adaptive_options(	   R   t   AttributeErrort	   _adaptivet   stopt   hasattrR   t   updateR   t	   scheduler(   t   selft   kwargs(    (    s9   lib/python2.7/site-packages/distributed/deploy/cluster.pyt   adapt7   s    	c         C   s
   |  j  j S(   N(   R   t   address(   R   (    (    s9   lib/python2.7/site-packages/distributed/deploy/cluster.pyt   scheduler_addressH   s    c         C   sg   t  j j d ƒ } |  j j j d ƒ d j d ƒ d } |  j j d j } | j d | d | t	 j
  S(	   Ns   distributed.dashboard.links   ://i   t   :i    t   bokeht   hostt   port(   t   daskt   configt   getR   R   t   splitt   servicesR   t   formatt   ost   environ(   R   t   templateR   R   (    (    s9   lib/python2.7/site-packages/distributed/deploy/cluster.pyt   dashboard_linkL   s    &c         C   sµ   t  ƒ  ¦ | t |  j j ƒ k r> |  j j j |  j | ƒ nm |  j j d t |  j j ƒ | ƒ } t j	 d | ƒ |  j j j |  j j
 d | ƒ|  j j j |  j | ƒ Wd QXd S(   s5   Scale cluster to n workers

        Parameters
        ----------
        n: int
            Target number of workers

        Example
        -------
        >>> cluster.scale(10)  # scale cluster to ten workers

        See Also
        --------
        Cluster.scale_up
        Cluster.scale_down
        t   ns   Closing workers: %st   workersN(   R   t   lenR   R"   t   loopt   add_callbackt   scale_upt   workers_to_closet   loggert   debugt   retire_workerst
   scale_down(   R   R!   t   to_close(    (    s9   lib/python2.7/site-packages/distributed/deploy/cluster.pyt   scaleS   s    
c         C   sy   t  |  j j ƒ } t d „  |  j j j ƒ  Dƒ ƒ } t d „  |  j j j ƒ  Dƒ ƒ } t | ƒ } d | | | f } | S(   Nc         s   s   |  ] } | j  Vq d  S(   N(   t   ncores(   t   .0t   ws(    (    s9   lib/python2.7/site-packages/distributed/deploy/cluster.pys	   <genexpr>s   s    c         s   s   |  ] } | j  Vq d  S(   N(   t   memory_limit(   R/   R0   (    (    s9   lib/python2.7/site-packages/distributed/deploy/cluster.pys	   <genexpr>t   s    s¢  
<div>
  <style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
  </style>
  <table style="text-align: right;">
    <tr><th>Workers</th> <td>%d</td></tr>
    <tr><th>Cores</th> <td>%d</td></tr>
    <tr><th>Memory</th> <td>%s</td></tr>
  </table>
</div>
(   R#   R   R"   t   sumt   valuesR   (   R   R"   t   corest   memoryt   text(    (    s9   lib/python2.7/site-packages/distributed/deploy/cluster.pyt   _widget_statusq   s    ""
c            s~  y ˆ j  SWn t k
 r n Xd d l m } m } m } m } m } m } m	 } | d d ƒ } d ˆ j
 j k r ˆ j }	 d |	 |	 f }	 n d }	 d t ˆ ƒ j }
 | |
 ƒ }
 | |	 ƒ } | ˆ j ƒ  d	 | d
 d ƒ ƒ‰ | d d d d	 | ƒ‰ | d d d	 | ƒ } | d d d d	 | ƒ‰ | d d d d	 | ƒ‰  | d d d	 | ƒ } | | ˆ | g ƒ | ˆ ˆ  | g ƒ g d	 | d
 d ƒ ƒ} d | _ | j d d ƒ | j d d ƒ | |
 | ˆ | g ƒ | g ƒ } | ˆ _  ‡  ‡ ‡ f d †  } | j | ƒ ‡ ‡ f d †  } | j | ƒ t ˆ j
 ƒ } ‡ ‡ f d †  } t | d d ˆ j
 j ƒ} | ˆ j
 j d <| j ƒ  | S(   s5    Create IPython widget for display within a notebook iÿÿÿÿ(   t   Layoutt   VBoxt   HBoxt   IntTextt   Buttont   HTMLt	   Accordiont   widtht   150pxR   s=   <p><b>Dashboard: </b><a href="%s" target="_blank">%s</a></p>
t    s   <h2>%s</h2>t   layoutt	   min_widthi    t   descriptiont   Workerst   Scalet   Minimumt   Maximumt   Adaptt   500pxs   Manual Scalingi   s   Adaptive Scalingc            s    ˆ j  d ˆ j d ˆ  j ƒ d  S(   Nt   minimumt   maximum(   R   t   value(   t   b(   RL   RK   R   (    s9   lib/python2.7/site-packages/distributed/deploy/cluster.pyt   adapt_cb¿   s    c      	      sJ   t  ƒ  ; ˆ  j } t t ƒ  ˆ j j ƒ  Wd  QXˆ j | ƒ Wd  QXd  S(   N(   R   RM   R   R   R	   R
   R-   (   RN   R!   (   t   requestR   (    s9   lib/python2.7/site-packages/distributed/deploy/cluster.pyt   scale_cbÄ   s
    
	c              s   ˆ  j  ƒ  ˆ _ d  S(   N(   R7   RM   (    (   R   t   status(    s9   lib/python2.7/site-packages/distributed/deploy/cluster.pyR   Ï   s    iô  t   io_loops   cluster-reprN(   t   _cached_widgetR   t
   ipywidgetsR8   R9   R:   R;   R<   R=   R>   R   R   R    t   typet   __name__R7   t   Nonet   selected_indext	   set_titlet   on_clickR    R   R$   t   periodic_callbackst   start(   R   R8   R9   R:   R;   R<   R=   R>   RB   t   linkt   titlet	   dashboardR-   R   t	   accordiont   boxRO   RQ   t   scheduler_refR   t   pc(    (   RL   RK   RP   R   RR   s9   lib/python2.7/site-packages/distributed/deploy/cluster.pyt   _widget’   sN    4	!'	!	
c         K   s   |  j  ƒ  j |   S(   N(   Re   t   _ipython_display_(   R   R   (    (    s9   lib/python2.7/site-packages/distributed/deploy/cluster.pyRf   Ø   s    (   RW   t
   __module__t   __doc__R   t   propertyR   R    R-   R7   Re   Rf   (    (    (    s9   lib/python2.7/site-packages/distributed/deploy/cluster.pyR      s   '			!	F(   t   loggingR   t   weakrefR    R   t   adaptiveR   t   utilsR   R   R   R   t	   getLoggerRW   R(   t   objectR   (    (    (    s9   lib/python2.7/site-packages/distributed/deploy/cluster.pyt   <module>   s   "