ó
gbc           @   s   d  d l  Z  d  d l m Z m Z d  d l m Z m Z d  d l m Z d e f d     YZ	 d e
 f d     YZ d	 e
 f d
     YZ d S(   i˙˙˙˙N(   t   warningt   info(   t   sleept   time(   t   TaskPoolt   JobPoolc           B   sV   e  Z d  Z d d  Z d   Z d   Z d   Z d d
 d  Z d   Z	 d	   Z
 RS(   sm   
    JobPool is dedicated to manage a job queue. These jobs
    will run in a limited number of thread. 
    g{ŽGáz?c         C   sq   t  |   |  _ t |  j |  j |  |  _ | |  _ t   |  _ t j	 d |  j
  |  _ |  j j   t |  _ d S(   sŐ   
        
        @param count: number of thread dedicated to this JobPool
        @type count: int
        @param precision: delay between two check for new job (in second)
        @type precision: float
        t   targetN(   t   JobIteratort	   _iteratorR   t   _runJobt	   _taskPoolt
   _precisiont   sett   _toRunt	   threadingt   Threadt   _runnert   _runnerThreadt   startt   Falset
   _finalyzed(   t   selft   countt	   precision(    (    s9   lib/python2.7/site-packages/obitools/parallel/jobqueue.pyt   __init__   s    		c         C   s@   x% |  j  D] } t d t |   q
 Wt d t |    d  S(   Ns   Job %d finnisheds   All jobs in %d JobPool finished(   R
   R   t   id(   R   t   rep(    (    s9   lib/python2.7/site-packages/obitools/parallel/jobqueue.pyR       s    c         C   s   |  j  S(   N(   R   (   R   (    (    s9   lib/python2.7/site-packages/obitools/parallel/jobqueue.pyt   _jobIterator%   s    c         C   sE   t    | _ t d t |   |   | _ t    | _ t | _ | S(   Ns   Job %d started(   R   t   startedR   R   t   resultt   endedt   Truet   finished(   R   t   job(    (    s9   lib/python2.7/site-packages/obitools/parallel/jobqueue.pyR	   (   s    	g      đ?c         C   s˘   |  j  s t d   | j d k	 rC t d t |   t |  St   | _ | | _ | | _ t |  } t | _ | |  | <|  j	 j
 |  t d |  | S(   sb  
        Submit a new job to the JobPool.
        
        @param job: the new submited job
        @type job: Job instance
        @param priority: priority level of this job (higher is better)
        @type priority: float
        @param userid: a user identifier (Default is None)
         
        @return: job identifier
        @rtype: int
        s$   This jobPool does not accept new jobs   Job %d was already submitteds   Job %d submittedN(   R   t   AssertionErrort	   submittedt   NoneR    R   R   t   priorityt   useridR   t   addR   (   R   R!   R%   R&   t   i(    (    s9   lib/python2.7/site-packages/obitools/parallel/jobqueue.pyt   submit0   s    	
			
c         C   s   |  j  j   t |  _ d S(   sW   
        Indicate to the JobPool, that no new jobs will
        be submitted. 
        N(   R   t   finalyzeR   R   (   R   (    (    s9   lib/python2.7/site-packages/obitools/parallel/jobqueue.pyR*   P   s    c         C   s   |  j    d  S(   N(   R*   (   R   (    (    s9   lib/python2.7/site-packages/obitools/parallel/jobqueue.pyt   __del__X   s    N(   t   __name__t
   __module__t   __doc__R   R   R   R	   R$   R)   R*   R+   (    (    (    s9   lib/python2.7/site-packages/obitools/parallel/jobqueue.pyR      s   			 	R   c           B   s,   e  Z d    Z d   Z d   Z d   Z RS(   c         C   s%   | |  _  t |  _ t j   |  _ d  S(   N(   t   _poolR   t	   _finalyzeR   t   Lockt	   _nextLock(   R   t   pool(    (    s9   lib/python2.7/site-packages/obitools/parallel/jobqueue.pyR   ]   s    		c         C   s   |  S(   N(    (   R   (    (    s9   lib/python2.7/site-packages/obitools/parallel/jobqueue.pyt   __iter__c   s    c         C   s   t  |  _ d S(   s[   
        Indicate to the JobIterator, that no new jobs will
        be submitted. 
        N(   R   R0   (   R   (    (    s9   lib/python2.7/site-packages/obitools/parallel/jobqueue.pyR*   f   s    c         C   sĺ   |  j  j   x§ |  j j s& |  j rś d } d } x; |  j j D]- } | j   } | | k r? | } | } q? q? W| d k	 rŁ |  j j j |  |  j  j   | f St	 |  j j
  q W|  j  j   t d t |  j   t  d S(   s[   
        
        @return: the next job to run       
        @rtype: Job instance
        i    s   No more jobs in %d JobPoolN(   R2   t   acquireR/   R   R0   R$   t   runScoret   removet   releaseR   R   R   R   t   StopIteration(   R   R   t   maxScoret   kt   s(    (    s9   lib/python2.7/site-packages/obitools/parallel/jobqueue.pyt   nextn   s"    (   R,   R-   R   R4   R*   R=   (    (    (    s9   lib/python2.7/site-packages/obitools/parallel/jobqueue.pyR   \   s   			t   Jobc           B   s)   e  Z d d d   Z d   Z d   Z RS(   c         O   sz   | d k	 s t  | |  _ | |  _ | |  _ t |  _ t |  _ d |  _ d |  _	 d |  _
 | d k	 rv | j |   n  d S(   sŮ  
        Create a new job
        
        @param pool: the jobpool used to run job. Can be None to not
                    execute the job immediately.
        @type pool: JobPool instance

        @param function: the function to run for the job
        @type function: callable object
        
        @param args: parametters for function call 
        @param kwargs: named parametters for function call 
        
        @precondition: function cannot be None
        N(   R$   R"   t   _argst   _kwargst	   _functionR   t   runningR    R#   R%   R&   R)   (   R   R3   t   functiont   argst   kwargs(    (    s9   lib/python2.7/site-packages/obitools/parallel/jobqueue.pyR      s    								c         C   s   t    |  j |  j S(   sa   
        @return: the score used to ordonnance job in the queue
        @rtype: C{float}
        (   R   R#   R%   (   R   (    (    s9   lib/python2.7/site-packages/obitools/parallel/jobqueue.pyR6   §   s    c         C   s   |  j  |  j |  j   S(   N(   RA   R?   R@   (   R   (    (    s9   lib/python2.7/site-packages/obitools/parallel/jobqueue.pyt   __call__Ż   s    N(   R,   R-   R$   R   R6   RF   (    (    (    s9   lib/python2.7/site-packages/obitools/parallel/jobqueue.pyR>      s   	(   R   t   loggingR    R   R   R   t   obitools.parallelR   t   dictR   t   objectR   R>   (    (    (    s9   lib/python2.7/site-packages/obitools/parallel/jobqueue.pyt   <module>   s   T,