ó
j4Vdc           @€  s´   d  Z  d d l m 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 e f d	 „  ƒ  YZ d
 „  Z e d k r° e d ƒ e d ƒ n  d S(   sÝ   
Sliding-window-based job/task queue class (& example of use.)

May use ``multiprocessing.Process`` or ``threading.Thread`` objects as queue
items, though within Fabric itself only ``Process`` objects are used/supported.
iÿÿÿÿ(   t   with_statementN(   t   Process(   t   env(   t   ssh(   t   settingst   JobQueuec           B€  sM   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 RS(   s;  
    The goal of this class is to make a queue of processes to run, and go
    through them running X number at any given time. 

    So if the bubble is 5 start with 5 running and move the bubble of running
    procs along the queue looking something like this:

        Start
        ...........................
        [~~~~~]....................
        ___[~~~~~].................
        _________[~~~~~]...........
        __________________[~~~~~]..
        ____________________[~~~~~]
        ___________________________
                                End 
    c         C€  sU   g  |  _  g  |  _ g  |  _ d |  _ | |  _ | |  _ t |  _ t |  _ t |  _	 d S(   s8   
        Setup the class to resonable defaults.
        i    N(
   t   _queuedt   _runningt
   _completedt   _num_of_jobst   _maxt   _comms_queuet   Falset	   _finishedt   _closedt   _debug(   t   selft   max_runningt   comms_queue(    (    s^   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/fabric/job_queue.pyt   __init__$   s    								c         C€  s7   |  j  r/ t g  |  j  D] } | j ƒ  ^ q ƒ St Sd S(   sš   
        Simply states if all procs are alive or not. Needed to determine when
        to stop looping, and pop dead procs off and add live ones.
        N(   R   t   allt   is_aliveR   (   R   t   x(    (    s^   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/fabric/job_queue.pyt
   _all_alive2   s    	&c         C€  s   |  j  S(   sJ   
        Just going to use number of jobs as the JobQueue length.
        (   R	   (   R   (    (    s^   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/fabric/job_queue.pyt   __len__<   s    c         C€  s   |  j  r d GHn  t |  _ d S(   s”   
        A sanity check, so that the need to care about new jobs being added in
        the last throws of the job_queue's run are negated.
        s   job queue closed.N(   R   t   TrueR   (   R   (    (    s^   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/fabric/job_queue.pyt   closeB   s    	c         C€  sG   |  j  sC |  j j | ƒ |  j d 7_ |  j rC d | j GHqC n  d S(   s¶  
        Add the Process() to the queue, so that later it can be checked up on.
        That is if the JobQueue is still open.

        If the queue is closed, this will just silently do nothing.

        To get data back out of this process, give ``process`` access to a
        ``multiprocessing.Queue`` object, and give it here as ``queue``. Then
        ``JobQueue.run`` will include the queue's contents in its return value.
        i   s   job queue appended %s.N(   R   R   t   appendR	   R   t   name(   R   t   process(    (    s^   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/fabric/job_queue.pyR   L   s
    		c         €  s  ‡  f d †  } i  } x' ˆ  j  D] } t j d	 ƒ | | j <q Wˆ  j sW t d ƒ ‚ n  ˆ  j rh d GHn  x# t ˆ  j ƒ ˆ  j	 k  r | ƒ  qk Wx<ˆ  j
 sÌx, t ˆ  j ƒ ˆ  j	 k  rÈ ˆ  j  rÈ | ƒ  q Wˆ  j ƒ  s_xf t ˆ  j ƒ D]U \ } } | j ƒ  så ˆ  j rd | j GHn  ˆ  j j | ƒ } ˆ  j j | ƒ qå qå Wˆ  j r_d t ˆ  j ƒ GHq_n  ˆ  j  pnˆ  j s¬ˆ  j r‚d GHn  x ˆ  j D] } | j ƒ  qŒWt ˆ  _
 n  ˆ  j | ƒ t j t j ƒ q‘ Wˆ  j | ƒ x7 ˆ  j D], } t | t ƒ rä| j | | j d <qäqäW| S(
   s;  
        This is the workhorse. It will take the intial jobs from the _queue,
        start them, add them to _running, and then go into the main running
        loop.

        This loop will check for done procs, if found, move them out of
        _running into _completed. It also checks for a _running queue with open
        spots, which it will then fill as discovered.

        To end the loop, there have to be no running procs, and no more procs
        to be run in the queue.

        This function returns an iterable of all its children's exit codes.
        c          €  sm   ˆ  j  j ƒ  }  ˆ  j r' d |  j GHn  t d t d |  j d |  j ƒ  |  j ƒ  Wd QXˆ  j j |  ƒ d S(   s¢  
            Helper function to do the job of poping a new proc off the queue
            start it, then add it to the running queue. This will eventually
            depleate the _queue, which is a condition of stopping the running
            while loop.

            It also sets the env.host_string from the job.name, so that fabric
            knows that this is the host to be making connections on.
            s*   Popping '%s' off the queue and starting itt   clean_revertt   host_stringt   hostN(	   R   t   popR   R   R   R   t   startR   R   (   t   job(   R   (    s^   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/fabric/job_queue.pyt   _advance_the_queuel   s    
	"t	   exit_codet   resultss    Need to close() before starting.s   Job queue starting.s"   Job queue found finished proc: %s.s   Job queue has %d running.s   Job queue finished.(   R%   R&   (   R   t   dictt   fromkeysR   R   t	   ExceptionR   t   lenR   R
   R   R   t	   enumerateR   R!   R   R   t   joinR   t   _fill_resultst   timet   sleepR   t   io_sleept
   isinstanceR   t   exitcode(   R   R$   R&   R#   t   idt   done(    (   R   s^   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/fabric/job_queue.pyt   run]   sH    		$			c         C€  sR   xK t  rM y) |  j j ƒ  } | d | | d d <Wq t j k
 rI Pq Xq Wd S(   s¤   
        Attempt to pull data off self._comms_queue and add to 'results' dict.
        If no data is available (i.e. the queue is empty), bail immediately.
        t   resultR   R&   N(   R   R   t
   get_nowaitt   Queuet   Empty(   R   R&   t   datum(    (    s^   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/fabric/job_queue.pyR-   ¸   s    	(
   t   __name__t
   __module__t   __doc__R   R   R   R   R   R5   R-   (    (    (    s^   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/fabric/job_queue.pyR      s   		
		
		[c      	   C€  s¿   d „  } |  d k r( d d l  m } n |  d k rG d d l m } n  t j ƒ  } t d | ƒ } t | _ x9 t d ƒ D]+ } | j	 | d	 | d
 | g d i  ƒ ƒ qx W| j
 ƒ  | j ƒ  d S(   sg   
    This will run the queue through it's paces, and show a simple way of using
    the job queue.
    c         S€  s	   |  GHd S(   sC   
        Simple function to give a simple task to execute.
        N(    (   t   number(    (    s^   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/fabric/job_queue.pyt   print_numberÍ   s    t   multiprocessingiÿÿÿÿ(   R   t	   threading(   t   Threadi   i   t   targett   argst   kwargsN(   R@   R   RA   RB   R8   R   R   R   t   rangeR   R   R5   (   t   parallel_typeR?   t   Buckett   queuet   jobsR   (    (    s^   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/fabric/job_queue.pyt	   try_usingÇ   s    			
t   __main__R@   RA   (   R=   t
   __future__R    R.   R8   R@   R   t   fabric.stateR   t   fabric.networkR   t   fabric.context_managersR   t   objectR   RK   R;   (    (    (    s^   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/fabric/job_queue.pyt   <module>   s   µ	$
