ó
ßüÚ\c           @` s  d  d l  m Z m Z m Z d  d l Z d  d l m Z d  d l 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 d d l m Z d d	 l m Z d d
 l m Z d d l m Z m Z m Z m Z m Z m Z m Z m Z m Z d d l  m! Z! d d l" m# Z# d d l$ m% Z% m& Z& m' Z' e j( e) ƒ Z* d e f d „  ƒ  YZ+ d „  Z, e
 j- ƒ  Z. e j/ d „  ƒ Z0 d S(   i    (   t   print_functiont   divisiont   absolute_importN(   t	   timedelta(   t   factors(   t   geni   (   t   Clusteri   (   t   get_thread_identity(   t   CommClosedError(	   t   get_ip_interfacet   synct   ignoringt   Allt   silence_loggingt
   LoopRunnert
   log_errorst   thread_statet   parse_timedelta(   t   Nanny(   t	   Scheduler(   t   Workert   parse_memory_limitt   _ncorest   LocalClusterc           B` sj  e  Z d  Z d d e d d d d e j d d d d d e d d d d d d „ Z d „  Z	 d „  Z
 e d „  ƒ Z d „  Z d „  Z e j d d d	 „ ƒ Z e j d
 d „ ƒ Z d „  Z e j d „  ƒ Z d „  Z e j d d „ ƒ Z d d „ Z e j d „  ƒ Z e j d „  ƒ Z d „  Z d „  Z d „  Z e j d „  ƒ Z e j d „  ƒ Z e d „  ƒ Z RS(   sR
   Create local Scheduler and Workers

    This creates a "cluster" of a scheduler and workers running on the local
    machine.

    Parameters
    ----------
    n_workers: int
        Number of workers to start
    processes: bool
        Whether to use processes (True) or threads (False).  Defaults to True
    threads_per_worker: int
        Number of threads per each worker
    scheduler_port: int
        Port of the scheduler.  8786 by default, use 0 to choose a random port
    silence_logs: logging level
        Level of logs to print out to stdout.  ``logging.WARN`` by default.
        Use a falsey value like False or None for no change.
    ip: string
        IP address on which the scheduler will listen, defaults to only localhost
    dashboard_address: str
        Address on which to listen for the Bokeh diagnostics server like
        'localhost:8787' or '0.0.0.0:8787'.  Defaults to ':8787'.
        Set to ``None`` to disable the dashboard.
        Use port 0 for a random port.
    diagnostics_port: int
        Deprecated.  See dashboard_address.
    asynchronous: bool (False by default)
        Set to True if using this cluster within async/await functions or within
        Tornado gen.coroutines.  This should remain False for normal use.
    worker_kwargs: dict
        Extra worker arguments, will be passed to the Worker constructor.
    blocked_handlers: List[str]
        A list of strings specifying a blacklist of handlers to disallow on the Scheduler,
        like ``['feed', 'run_function']``
    service_kwargs: Dict[str, Dict]
        Extra keywords to hand to the running services
    security : Security
    protocol: str (optional)
        Protocol to use like ``tcp://``, ``tls://``, ``inproc://``
        This defaults to sensible choice given other keyword arguments like
        ``processes`` and ``security``
    interface: str (optional)
        Network interface to use.  Defaults to lo/localhost
    worker_class: Worker
        Worker class used to instantiate workers from.

    Examples
    --------
    >>> cluster = LocalCluster()  # Create a local cluster with as many workers as cores  # doctest: +SKIP
    >>> cluster  # doctest: +SKIP
    LocalCluster("127.0.0.1:8786", workers=8, ncores=8)

    >>> c = Client(cluster)  # connect to local cluster  # doctest: +SKIP

    Add a new worker to the cluster

    >>> w = cluster.start_worker(ncores=2)  # doctest: +SKIP

    Shut down the extra worker

    >>> cluster.stop_worker(w)  # doctest: +SKIP

    Pass extra keyword arguments to Bokeh

    >>> LocalCluster(service_kwargs={'bokeh': {'prefix': '/foo'}})  # doctest: +SKIP
    i    s   :8787c      	   K` s€  | d  k	 r! d } t | ƒ ‚ n  |
 d  k	 rC t j d ƒ |
 }	 n  d  |  _ | |  _ | d  k r» | r‰ d | k r‰ | j d ƒ d } q» | r˜ d } q» |  j r² | r² d } q» d } n  | j d ƒ s× | d } n  | |  _ | |  _	 | |  _
 | |  _ | |  _ | pi  } | pi  } | r7t d | ƒ |  _ n  | d  k ry| d  k ry| rjt t ƒ \ } } qyd	 } t } n  | d  k r§| d  k	 r§t d	 t | ƒ } n  | rÞ| d  k rÞt d	 t t j t | ƒ ƒ ƒ } n  | r	d
 | k r	t d d	 | ƒ | d
 <n  | j i | d 6| d 6ƒ t d | d | ƒ |  _ |  j j |  _ |	 t k	 rÝ|	 d  k	 rÝy$ d d l m } d d l m } Wn t k
 r§t  j! d ƒ qÝX| | p´i  j" d i  ƒ f | d |	 f <| | d <n  t# d |  j d | d | d | ƒ |  _$ | |  _% g  |  _& | |  _' | r5| |  j' d <n  | sP| sGt( n t) } n  | |  _* |  j+ d | d | ƒ t, j- |  ƒ d  S(   Ns¡   The start= parameter is deprecated. LocalCluster always starts. For asynchronous operation use the following: 

  cluster = yield LocalCluster(asynchronous=True)sM   diagnostics_port has been deprecated. Please use `dashboard_address=` insteads   ://i    s   tls://s	   inproc://s   tcp://t   leveli   t   memory_limitt   autot   ncorest   servicest   loopt   asynchronous(   t   BokehScheduler(   t   BokehWorkers4   To start diagnostics web server please install Bokeht   bokeht   securityt   blocked_handlerst   ipt	   n_workers(   R!   i    (.   t   Nonet
   ValueErrort   warningst   warnt   statust	   processest   splitt   endswitht   protocolt   silence_logst   _asynchronousR"   t	   interfaceR   t   _old_logging_levelt   nprocesses_nthreadsR   t   maxt   intt   matht   ceilR   t   updateR   t   _loop_runnerR   t   Falset   distributed.bokeh.schedulerR   t   distributed.bokeh.workerR    t   ImportErrort   loggert   debugt   getR   t	   schedulert   scheduler_portt   workerst   worker_kwargsR   R   t   worker_classt   startt   clusters_to_closet   add(   t   selfR%   t   threads_per_workerR+   R   RF   R$   RB   R/   t   dashboard_addresst   diagnostics_portR   t   worker_servicest   service_kwargsR   R"   R.   R#   R1   RE   RD   t   msgR   R    (    (    s7   lib/python2.7/site-packages/distributed/deploy/local.pyt   __init__h   s†    												%%					c         C` s0   d |  j  t |  j ƒ t d „  |  j Dƒ ƒ f S(   Ns'   LocalCluster(%r, workers=%d, ncores=%d)c         s` s   |  ] } | j  Vq d  S(   N(   R   (   t   .0t   w(    (    s7   lib/python2.7/site-packages/distributed/deploy/local.pys	   <genexpr>â   s    (   t   scheduler_addresst   lenRC   t   sum(   RI   (    (    s7   lib/python2.7/site-packages/distributed/deploy/local.pyt   __repr__Þ   s    c         C` s   |  j  j ƒ  S(   N(   t   _startedt	   __await__(   RI   (    (    s7   lib/python2.7/site-packages/distributed/deploy/local.pyRX   å   s    c         C` s@   |  j  p? t t d t ƒ p? t |  j d ƒ o? |  j j t ƒ  k S(   NR   t   _thread_identity(   R0   t   getattrR   R:   t   hasattrR   RY   R   (   RI   (    (    s7   lib/python2.7/site-packages/distributed/deploy/local.pyR   è   s    	c         O` s„   | j  d d  ƒ s |  j rj | j  d d  ƒ } | | | Ž  } | d  k	 rf t j t d | ƒ | ƒ } n  | St |  j | | | Ž Sd  S(   NR   t   callback_timeoutt   seconds(   t   popR&   R   R   t   with_timeoutR   R
   R   (   RI   t   funct   argst   kwargsR\   t   future(    (    s7   lib/python2.7/site-packages/distributed/deploy/local.pyR
   ñ   s    c         K` sB   |  j  j ƒ  |  j r+ |  j |   |  _ n |  j |  j |  d  S(   N(   R9   RF   R0   t   _startRW   R
   (   RI   Rb   (    (    s7   lib/python2.7/site-packages/distributed/deploy/local.pyRF   û   s    	c         c` sù   |  j  d k r d S|  j d k r. |  j } nu | d k r^ |  j rU t |  j ƒ } q^ d } n  d | k rs | } n |  j | } |  j r£ | d t |  j ƒ 7} n  |  j j | ƒ g  t	 | ƒ D] } |  j
 |  j   ^ qÀ Vd |  _  t j |  ƒ ‚ d S(   s-   
        Start all cluster services.
        t   runningNs	   inproc://s	   127.0.0.1s   ://t   :(   R*   R.   R&   R1   R	   RB   t   strRA   RF   t   ranget   _start_workerRD   R   t   Return(   RI   R$   R%   t   addresst   i(    (    s7   lib/python2.7/site-packages/distributed/deploy/local.pyRd     s"    				*	i<   c      	   k` s  |  j  r9 |  j  j d ƒ r9 t j d |  j  d d ƒd  S|  j rO t | d <n  |  j |  j j d |  j	 d | d |  j
 | V} |  j j | ƒ x6 | j  d	 k rÈ | j |  j j k rÈ t j d
 ƒ Vq“ W| j  d	 k r|  j j  d k r|  j j | ƒ t j d ƒ ‚ n  t j | ƒ ‚ d  S(   Nt   closs*   Tried to start a worker while status=='%s't
   stackleveli   t   quietR   t   death_timeoutR/   t   closedg{®Gáz„?Re   s   Worker failed to start(   R*   t
   startswithR(   R)   R+   t   TrueRE   RA   Rk   R   R/   RC   t   appendt   worker_addressR   t   sleept   removet   TimeoutErrorRj   (   RI   Rp   Rb   RR   (    (    s7   lib/python2.7/site-packages/distributed/deploy/local.pyRi   "  s&    		
'!c         K` s   |  j  |  j |  S(   s   Add a new worker to the running cluster

        Parameters
        ----------
        port: int (optional)
            Port on which to serve the worker, defaults to 0 or random
        ncores: int (optional)
            Number of threads to use.  Defaults to number of logical cores

        Examples
        --------
        >>> c = LocalCluster()  # doctest: +SKIP
        >>> c.start_worker(ncores=2)  # doctest: +SKIP

        Returns
        -------
        The created Worker or Nanny object.  Can be discarded.
        (   R
   Ri   (   RI   Rb   (    (    s7   lib/python2.7/site-packages/distributed/deploy/local.pyt   start_worker@  s    c         c` s1   | j  ƒ  V| |  j k r- |  j j | ƒ n  d  S(   N(   t   closeRC   Rw   (   RI   RR   (    (    s7   lib/python2.7/site-packages/distributed/deploy/local.pyt   _stop_workerU  s    c         C` s   |  j  |  j | ƒ d S(   sÝ    Stop a running worker

        Examples
        --------
        >>> c = LocalCluster()  # doctest: +SKIP
        >>> w = c.start_worker(ncores=2)  # doctest: +SKIP
        >>> c.stop_worker(w)  # doctest: +SKIP
        N(   R
   R{   (   RI   RR   (    (    s7   lib/python2.7/site-packages/distributed/deploy/local.pyt   stop_worker[  s    	t   2sc         c` sÖ   |  j  d k r d  Sd |  _  t t j t t ƒ 4 t j t d t | ƒ ƒ |  j	 j
 d t ƒ ƒ VWd  QXt t j ƒ J t j t d t | ƒ ƒ t g  |  j D] } |  j | ƒ ^ qœ ƒ ƒ VWd  QX|  j 2d |  _  d  S(   NRq   t   closingR]   t   close_workers(   R*   R   R   Rx   R   t   OSErrorR_   R   R   RA   Rz   Rs   R   RC   R{   (   RI   t   timeoutRR   (    (    s7   lib/python2.7/site-packages/distributed/deploy/local.pyt   _closef  s    	3i   c         ` s§   ˆ  j  d k r d Sy ˆ  j ˆ  j d | ƒ} Wn t k
 rH d } n Xt ˆ  d ƒ rŠ ˆ  j rz | j ‡  f d †  ƒ qŠ t ˆ  j	 ƒ n  ˆ  j s£ ˆ  j
 j ƒ  n  | S(   s    Close the cluster Rq   NR\   R2   c         ` s   t  ˆ  j ƒ S(   N(   R   R2   (   t   _(   RI   (    s7   lib/python2.7/site-packages/distributed/deploy/local.pyt   <lambda>ˆ  t    (   R*   R
   R‚   t   RuntimeErrorR&   R[   R   t   add_done_callbackR   R2   R9   t   stop(   RI   R   t   result(    (   RI   s7   lib/python2.7/site-packages/distributed/deploy/local.pyRz   {  s    
		c      	   k` sŽ   t  ƒ   t j |  j | ƒ } g  t | t |  j j ƒ ƒ D] } |  j |   ^ q< Vg  |  j D] } | j	 d k r` | ^ q` |  _ Wd QXd S(   sñ    Bring the total count of workers up to ``n``

        This function/coroutine should bring the total number of workers up to
        the number ``n``.

        This can be implemented either as a function or as a Tornado coroutine.
        Rq   N(
   R   t   toolzt   mergeRD   Rh   RT   RA   RC   Ri   R*   (   RI   t   nRb   t   kwargs2Rl   RR   (    (    s7   lib/python2.7/site-packages/distributed/deploy/local.pyt   scale_up’  s
    	
4c         #` s¡   t  ƒ  ’ g  |  j D] } | j d k r | ^ q |  _ t ˆ  ƒ ‰  t d „  ˆ  Dƒ ƒ rv ‡  f d †  |  j Dƒ ‰  n  g  ˆ  D] } |  j | ƒ ^ q} VWd QXd S(   sB   Remove ``workers`` from the cluster

        Given a list of worker addresses this function should remove those
        workers from the cluster.  This may require tracking which jobs are
        associated to which worker address.

        This can be implemented either as a function or as a Tornado coroutine.
        Rq   c         s` s   |  ] } t  | t ƒ Vq d  S(   N(   t
   isinstanceRg   (   RQ   RR   (    (    s7   lib/python2.7/site-packages/distributed/deploy/local.pys	   <genexpr>µ  s    c         ` s%   h  |  ] } | j  ˆ  k r | ’ q S(    (   Ru   (   RQ   RR   (   RC   (    s7   lib/python2.7/site-packages/distributed/deploy/local.pys	   <setcomp>¶  s   	 N(   R   RC   R*   t   sett   allR{   (   RI   RC   RR   (    (   RC   s7   lib/python2.7/site-packages/distributed/deploy/local.pyt
   scale_down¥  s    

.c         C` s   |  j  ƒ  d  S(   N(   Rz   (   RI   (    (    s7   lib/python2.7/site-packages/distributed/deploy/local.pyt   __del__»  s    c         C` s   |  S(   N(    (   RI   (    (    s7   lib/python2.7/site-packages/distributed/deploy/local.pyt	   __enter__¾  s    c         G` s   |  j  ƒ  d  S(   N(   Rz   (   RI   Ra   (    (    s7   lib/python2.7/site-packages/distributed/deploy/local.pyt   __exit__Á  s    c         c` s   |  j  Vt j |  ƒ ‚ d  S(   N(   RW   R   Rj   (   RI   (    (    s7   lib/python2.7/site-packages/distributed/deploy/local.pyt
   __aenter__Ä  s    c         c` s   |  j  ƒ  Vd  S(   N(   R‚   (   RI   t   typt   valuet	   traceback(    (    s7   lib/python2.7/site-packages/distributed/deploy/local.pyt	   __aexit__É  s    c         C` s'   y |  j  j SWn t k
 r" d SXd  S(   Ns   <unstarted>(   RA   Rk   R'   (   RI   (    (    s7   lib/python2.7/site-packages/distributed/deploy/local.pyRS   Í  s    N(    t   __name__t
   __module__t   __doc__R&   Rs   t   loggingt   WARNR:   RP   RV   RX   t   propertyR   R
   RF   R   t	   coroutineRd   Ri   Ry   R{   R|   R‚   Rz   RŽ   R’   R“   R”   R•   R–   Rš   RS   (    (    (    s7   lib/python2.7/site-packages/distributed/deploy/local.pyR   #   sV   Cb				
						c         ` sK   ˆ  d k r ˆ  } n" t  ‡  f d †  t ˆ  ƒ Dƒ ƒ } ˆ  | } | | f S(   sB  
    The default breakdown of processes and threads for a given number of cores

    Parameters
    ----------
    n: int
        Number of available cores

    Examples
    --------
    >>> nprocesses_nthreads(4)
    (4, 1)
    >>> nprocesses_nthreads(32)
    (8, 4)

    Returns
    -------
    nprocesses, nthreads
    i   c         3` s*   |  ]  } | t  j ˆ  ƒ k r | Vq d  S(   N(   R6   t   sqrt(   RQ   t   f(   RŒ   (    s7   lib/python2.7/site-packages/distributed/deploy/local.pys	   <genexpr>ì  s    (   t   minR   (   RŒ   R+   t   threads(    (   RŒ   s7   lib/python2.7/site-packages/distributed/deploy/local.pyR3   Õ  s
    	"
c          C` s+   x$ t  t ƒ D] }  |  j d d ƒ q Wd  S(   NR   i
   (   t   listRG   Rz   (   t   cluster(    (    s7   lib/python2.7/site-packages/distributed/deploy/local.pyt   close_clustersô  s    (1   t
   __future__R    R   R   t   atexitt   datetimeR   Rž   R6   R(   t   weakrefRŠ   t
   dask.utilsR   t   tornadoR   R§   R   t   compatibilityR   t   coreR   t   utilsR	   R
   R   R   R   R   R   R   R   t   nannyR   RA   R   t   workerR   R   R   t	   getLoggerR›   R>   R   R3   t   WeakSetRG   t   registerR¨   (    (    (    s7   lib/python2.7/site-packages/distributed/deploy/local.pyt   <module>   s,   @ÿ ³	