ó
ßüÚ\c           @` så   d  d l  m Z m Z m Z d  d l m Z m Z d  d l m Z 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 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   defaultdictt   deque(   t	   timedeltaN(   t   geni   (   t   _get_global_client(   t
   log_errors(   t
   get_workert   LockExtensionc           B` sG   e  Z d  Z d „  Z e j d d d d d „ ƒ Z d d d d „ Z RS(   s’    An extension for the scheduler to manage Locks

    This adds the following routes to the scheduler

    *  lock_acquire
    *  lock_release
    c         C` s_   | |  _  t t ƒ |  _ t ƒ  |  _ |  j  j j i |  j d 6|  j	 d 6ƒ |  |  j  j
 d <d  S(   Nt   lock_acquiret   lock_releaset   locks(   t	   schedulerR   R   t   eventst   dictt   idst   handlerst   updatet   acquiret   releaset
   extensions(   t   selfR   (    (    s/   lib/python2.7/site-packages/distributed/lock.pyt   __init__   s    	c   	   	   c` sM  t  ƒ  >t | t ƒ r( t | ƒ } n  | |  j k r@ t } nÉ xÆ | |  j k rt j j ƒ  } |  j	 | j
 | ƒ | j ƒ  } | d  k	 r« t j t d | ƒ | ƒ } n  z1 y	 | VWn t j k
 rÔ t } Pn Xt } Wd  |  j	 | j ƒ  } | | k st ‚ XqC W| r4| |  j k s$t ‚ | |  j | <n  t j | ƒ ‚ Wd  QXd  S(   Nt   seconds(   R   t
   isinstancet   listt   tupleR   t   Truet   tornadoR   t   EventR   t   appendt   waitt   NoneR   t   with_timeoutR   t   TimeoutErrort   Falset   popleftt   AssertionErrort   Return(	   R   t   streamt   namet   idt   timeoutt   resultt   eventt   futuret   event2(    (    s/   lib/python2.7/site-packages/distributed/lock.pyR   &   s0    
	 	
c         C` sž   t  ƒ   t | t ƒ r( t | ƒ } n  |  j j | ƒ | k rO t d ƒ ‚ n  |  j | =|  j | rŠ |  j j	 j
 |  j | d j ƒ n
 |  j | =Wd  QXd  S(   Ns#   This lock has not yet been acquiredi    (   R   R   R   R   R   t   gett
   ValueErrorR   R   t   loopt   add_callbackt   set(   R   R)   R*   R+   (    (    s/   lib/python2.7/site-packages/distributed/lock.pyR   C   s    

$N(	   t   __name__t
   __module__t   __doc__R   R   t	   coroutineR"   R   R   (    (    (    s/   lib/python2.7/site-packages/distributed/lock.pyR
      s
   	t   Lockc           B` s}   e  Z d  Z d
 d
 d „ Z e d
 d „ Z d „  Z d „  Z d „  Z	 d „  Z
 e j d „  ƒ Z e j d „  ƒ Z d	 „  Z RS(   sœ   Distributed Centralized Lock

    Parameters
    ----------
    name: string
        Name of the lock to acquire.  Choosing the same name allows two
        disconnected processes to coordinate a lock.

    Examples
    --------
    >>> lock = Lock('x')  # doctest: +SKIP
    >>> lock.acquire(timeout=1)  # doctest: +SKIP
    >>> # do things with protected resource
    >>> lock.release()  # doctest: +SKIP
    c         C` sY   | p t  ƒ  p t ƒ  j |  _ | p4 d t j ƒ  j |  _ t j ƒ  j |  _ t |  _	 d  S(   Ns   lock-(
   R   R	   t   clientt   uuidt   uuid4t   hexR*   R+   R%   t   _locked(   R   R*   R;   (    (    s/   lib/python2.7/site-packages/distributed/lock.pyR   a   s    c         C` sj   | s* | d k	 r! t d ƒ ‚ n  d } n  |  j j |  j j j d |  j d |  j d | ƒ} t |  _	 | S(   s”   Acquire the lock

        Parameters
        ----------
        blocking : bool, optional
            If false, don't wait on the lock in the scheduler at all.
        timeout : number, optional
            Seconds to wait on the lock in the scheduler.  This does not
            include local coroutine time, network transfer time, etc..
            It is forbidden to specify a timeout when blocking is false.

        Examples
        --------
        >>> lock = Lock('x')  # doctest: +SKIP
        >>> lock.acquire(timeout=1)  # doctest: +SKIP

        Returns
        -------
        True or False whether or not it sucessfully acquired the lock
        s/   can't specify a timeout for a non-blocking calli    R*   R+   R,   N(
   R"   R2   R;   t   syncR   R   R*   R+   R   R?   (   R   t   blockingR,   R-   (    (    s/   lib/python2.7/site-packages/distributed/lock.pyR   g   s    						c         C` sU   |  j  ƒ  s t d ƒ ‚ n  |  j j |  j j j d |  j d |  j ƒ} t |  _	 | S(   s&    Release the lock if already acquired s   Lock is not yet acquiredR*   R+   (
   t   lockedR2   R;   R@   R   R   R*   R+   R%   R?   (   R   R-   (    (    s/   lib/python2.7/site-packages/distributed/lock.pyR   Š   s    	$	c         C` s   |  j  S(   N(   R?   (   R   (    (    s/   lib/python2.7/site-packages/distributed/lock.pyRB   ”   s    c         C` s   |  j  ƒ  |  S(   N(   R   (   R   (    (    s/   lib/python2.7/site-packages/distributed/lock.pyt	   __enter__—   s    
c         O` s   |  j  ƒ  d  S(   N(   R   (   R   t   argst   kwargs(    (    s/   lib/python2.7/site-packages/distributed/lock.pyt   __exit__›   s    c         c` s   |  j  ƒ  Vt j |  ƒ ‚ d  S(   N(   R   R   R(   (   R   (    (    s/   lib/python2.7/site-packages/distributed/lock.pyt
   __aenter__ž   s    c         o` s   |  j  ƒ  Vd  S(   N(   R   (   R   RD   RE   (    (    s/   lib/python2.7/site-packages/distributed/lock.pyt	   __aexit__£   s    c         C` s   t  |  j f f S(   N(   R:   R*   (   R   (    (    s/   lib/python2.7/site-packages/distributed/lock.pyt
   __reduce__§   s    N(   R6   R7   R8   R"   R   R   R   R   RB   RC   RF   R   R9   RG   RH   RI   (    (    (    s/   lib/python2.7/site-packages/distributed/lock.pyR:   P   s   #	
			(   t
   __future__R    R   R   t   collectionsR   R   t   datetimeR   t   loggingR<   R   R   t   tornado.locksR;   R   t   utilsR   t   workerR	   t	   getLoggerR6   t   loggert   objectR
   R:   (    (    (    s/   lib/python2.7/site-packages/distributed/lock.pyt   <module>   s   >