ó
P'—^c           @@ s   d  Z  d d l m Z 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 m Z d d d	 d
 d d g Z e j j d ƒ Z e j d d k Z e
 d „  ƒ Z d e j f d „  ƒ  YZ y d d l m Z Wn™ e k
 r¢y d d l m Z Wq£e k
 ržy d d l m Z WqŸe k
 ršy d d l m Z Wq›e k
 r–d d l  m Z q›XqŸXq£Xn Xd „  Z! d e" f d „  ƒ  YZ# d e" f d „  ƒ  YZ$ d
 e" f d „  ƒ  YZ% d e j& f d „  ƒ  YZ' e re' Z( n e$ Z( d S(   sN   
    celery.utils.threads
    ~~~~~~~~~~~~~~~~~~~~

    Threading utilities.

i    (   t   absolute_importt   print_functionN(   t   contextmanager(   t   Proxy(   t   THREAD_TIMEOUT_MAXt   itemst   bgThreadt   Localt
   LocalStackt   LocalManagert	   get_identt   default_socket_timeoutt   USE_FAST_LOCALSi   c         c@ s/   t  j ƒ  } t  j |  ƒ d  Vt  j | ƒ d  S(   N(   t   sockett   getdefaulttimeoutt   setdefaulttimeout(   t   timeoutt   prev(    (    s6   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/threads.pyR      s    c           B@ sA   e  Z d d  „ Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   c         K@ sS   t  t |  ƒ j ƒ  t j ƒ  |  _ t j ƒ  |  _ t |  _ | pI |  j	 j
 |  _ d  S(   N(   t   superR   t   __init__t	   threadingt   Eventt   _is_shutdownt   _is_stoppedt   Truet   daemont	   __class__t   __name__t   name(   t   selfR   t   kwargs(    (    s6   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/threads.pyR   '   s
    	c         C@ s   t  d ƒ ‚ d  S(   Ns   subclass responsibility(   t   NotImplementedError(   R   (    (    s6   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/threads.pyt   body.   s    c         O@ s_   t  | j | Œ  d t j ƒt j ƒ  } z, t j | d | d | d d  t j ƒ Wd  ~ Xd  S(   Nt   filei    i   i   (   t   printt   formatt   syst   stderrt   exc_infot	   tracebackt   print_exceptiont   None(   R   t   msgt   fmtR   R&   (    (    s6   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/threads.pyt   on_crash1   s    c         C@ s‘   |  j  } |  j j } zj xc | ƒ  s} y | ƒ  Wq t k
 ry } z$ |  j d |  j | ƒ |  j ƒ  Wd  t j d ƒ Xq Xq WWd  |  j ƒ  Xd  S(   Ns   {0!r} crashed: {1!r}i   (	   R    R   t   is_sett	   ExceptionR,   R   t   _set_stoppedt   ost   _exit(   R   R    t   shutdown_sett   exc(    (    s6   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/threads.pyt   run:   s    	c         C@ s)   y |  j  j ƒ  Wn t k
 r$ n Xd  S(   N(   R   t   sett	   TypeError(   R   (    (    s6   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/threads.pyR/   J   s    c         C@ s:   |  j  j ƒ  |  j j ƒ  |  j ƒ  r6 |  j t ƒ n  d S(   s   Graceful shutdown.N(   R   R5   R   t   waitt   is_alivet   joinR   (   R   (    (    s6   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/threads.pyt   stopR   s    N(	   R   t
   __module__R)   R   R    R,   R4   R/   R:   (    (    (    s6   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/threads.pyR   %   s   					(   t
   getcurrent(   R
   c         C@ s   |  j  ƒ  d S(   sM  Releases the contents of the local for the current context.
    This makes it possible to use locals without a manager.

    Example::

        >>> loc = Local()
        >>> loc.foo = 42
        >>> release_local(loc)
        >>> hasattr(loc, 'foo')
        False

    With this function one can release :class:`Local` objects as well
    as :class:`StackLocal` objects.  However it is not possible to
    release data held by proxies that way, one always has to retain
    a reference to the underlying local object in order to be able
    to release it.

    .. versionadded:: 0.6.1
    N(   t   __release_local__(   t   local(    (    s6   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/threads.pyt   release_localh   s    c           B@ sM   e  Z d	 Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 RS(
   t   __storage__t   __ident_func__c         C@ s*   t  j |  d i  ƒ t  j |  d t ƒ d  S(   NR@   RA   (   t   objectt   __setattr__R
   (   R   (    (    s6   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/threads.pyR   ‚   s    c         C@ s   t  t |  j ƒ ƒ S(   N(   t   iterR   R@   (   R   (    (    s6   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/threads.pyt   __iter__†   s    c         C@ s   t  |  | ƒ S(   s   Create a proxy for a name.(   R   (   R   t   proxy(    (    s6   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/threads.pyt   __call__‰   s    c         C@ s   |  j  j |  j ƒ  d  ƒ d  S(   N(   R@   t   popRA   R)   (   R   (    (    s6   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/threads.pyR=      s    c         C@ s=   y |  j  |  j ƒ  | SWn t k
 r8 t | ƒ ‚ n Xd  S(   N(   R@   RA   t   KeyErrort   AttributeError(   R   R   (    (    s6   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/threads.pyt   __getattr__   s    c         C@ sP   |  j  ƒ  } |  j } y | | | | <Wn" t k
 rK i | | 6| | <n Xd  S(   N(   RA   R@   RI   (   R   R   t   valuet   identt   storage(    (    s6   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/threads.pyRC   –   s    	c         C@ s<   y |  j  |  j ƒ  | =Wn t k
 r7 t | ƒ ‚ n Xd  S(   N(   R@   RA   RI   RJ   (   R   R   (    (    s6   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/threads.pyt   __delattr__ž   s    (   R@   RA   (
   R   R;   t	   __slots__R   RE   RG   R=   RK   RC   RO   (    (    (    s6   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/threads.pyR      s   						t   _LocalStackc           B@ s‰   e  Z d  Z d „  Z d „  Z d „  Z d „  Z e e e ƒ Z [ [ d „  Z	 d „  Z
 d „  Z d „  Z e d	 „  ƒ Z e d
 „  ƒ Z RS(   sä  This class works similar to a :class:`Local` but keeps a stack
    of objects instead.  This is best explained with an example::

        >>> ls = LocalStack()
        >>> ls.push(42)
        >>> ls.top
        42
        >>> ls.push(23)
        >>> ls.top
        23
        >>> ls.pop()
        23
        >>> ls.top
        42

    They can be force released by using a :class:`LocalManager` or with
    the :func:`release_local` function but the correct way is to pop the
    item from the stack after using.  When the stack is empty it will
    no longer be bound to the current context (and as such released).

    By calling the stack without arguments it will return a proxy that
    resolves to the topmost item on the stack.

    c         C@ s   t  ƒ  |  _ d  S(   N(   R   t   _local(   R   (    (    s6   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/threads.pyR   ¿   s    c         C@ s   |  j  j ƒ  d  S(   N(   RR   R=   (   R   (    (    s6   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/threads.pyR=   Â   s    c         C@ s
   |  j  j S(   N(   RR   RA   (   R   (    (    s6   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/threads.pyt   _get__ident_func__Å   s    c         C@ s   t  j |  j d | ƒ d  S(   NRA   (   RB   RC   RR   (   R   RL   (    (    s6   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/threads.pyt   _set__ident_func__È   s    c         @ s   ‡  f d †  } t  | ƒ S(   Nc          @ s(   ˆ  j  }  |  d  k r$ t d ƒ ‚ n  |  S(   Ns   object unbound(   t   topR)   t   RuntimeError(   t   rv(   R   (    s6   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/threads.pyt   _lookupÎ   s    	(   R   (   R   RX   (    (   R   s6   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/threads.pyRG   Í   s    c         C@ sE   t  |  j d d ƒ } | d k r4 g  |  j _ } n  | j | ƒ | S(   s   Pushes a new item to the stackt   stackN(   t   getattrRR   R)   RY   t   append(   R   t   objRW   (    (    s6   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/threads.pyt   pushÕ   s
    c         C@ sZ   t  |  j d d ƒ } | d k r% d St | ƒ d k rL t |  j ƒ | d S| j ƒ  Sd S(   s|   Remove the topmost item from the stack, will return the
        old value or `None` if the stack was already empty.
        RY   i   iÿÿÿÿN(   RZ   RR   R)   t   lenR?   RH   (   R   RY   (    (    s6   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/threads.pyRH   Ý   s    c         C@ s)   t  |  j d d  ƒ } | r% t | ƒ Sd S(   NRY   i    (   RZ   RR   R)   R^   (   R   RY   (    (    s6   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/threads.pyt   __len__ê   s    c         C@ s)   t  |  j d d ƒ } | d k	 r% | Sg  S(   sd   get_current_worker_task uses this to find
        the original task that was executed by the worker.RY   N(   RZ   RR   R)   (   R   RY   (    (    s6   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/threads.pyRY   î   s    c         C@ s1   y |  j  j d SWn t t f k
 r, d SXd S(   s[   The topmost item on the stack.  If the stack is empty,
        `None` is returned.
        iÿÿÿÿN(   RR   RY   RJ   t
   IndexErrorR)   (   R   (    (    s6   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/threads.pyRU   ÷   s    (   R   R;   t   __doc__R   R=   RS   RT   t   propertyRA   RG   R]   RH   R_   RY   RU   (    (    (    s6   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/threads.pyRQ   ¥   s   									c           B@ s8   e  Z d  Z d d d „ Z d „  Z d „  Z d „  Z RS(   sœ  Local objects cannot manage themselves. For that you need a local
    manager.  You can pass a local manager multiple locals or add them
    later by appending them to `manager.locals`.  Everytime the manager
    cleans up it, will clean up all the data left in the locals for this
    context.

    The `ident_func` parameter can be added to override the default ident
    function for the wrapped locals.

    c         C@ s‘   | d  k r g  |  _ n- t | t ƒ r6 | g |  _ n t | ƒ |  _ | d  k	 r„ | |  _ x0 |  j D] } t j | d | ƒ qd Wn	 t |  _ d  S(   NRA   (	   R)   t   localst
   isinstanceR   t   listt
   ident_funcRB   RC   R
   (   R   Rc   Rf   R>   (    (    s6   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/threads.pyR     s    	c         C@ s
   |  j  ƒ  S(   s  Return the context identifier the local objects use internally
        for this context.  You cannot override this method to change the
        behavior but use it to link other context local objects (such as
        SQLAlchemy's scoped sessions) to the Werkzeug locals.(   Rf   (   R   (    (    s6   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/threads.pyR
     s    c         C@ s"   x |  j  D] } t | ƒ q
 Wd S(   s   Manually clean up the data in the locals for this context.

        Call this at the end of the request or use `make_middleware()`.

        N(   Rc   R?   (   R   R>   (    (    s6   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/threads.pyt   cleanup#  s    c         C@ s   d j  |  j j t |  j ƒ ƒ S(   Ns   <{0} storages: {1}>(   R#   R   R   R^   Rc   (   R   (    (    s6   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/threads.pyt   __repr__,  s    N(   R   R;   Ra   R)   R   R
   Rg   Rh   (    (    (    s6   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/threads.pyR	     s
   
			t   _FastLocalStackc           B@ s)   e  Z d  „  Z e d „  ƒ Z d „  Z RS(   c         C@ s+   g  |  _  |  j  j |  _ |  j  j |  _ d  S(   N(   RY   R[   R]   RH   (   R   (    (    s6   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/threads.pyR   3  s    	c         C@ s.   y |  j  d SWn t t f k
 r) d  SXd  S(   Niÿÿÿÿ(   RY   RJ   R`   R)   (   R   (    (    s6   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/threads.pyRU   8  s    c         C@ s   t  |  j ƒ S(   N(   R^   RY   (   R   (    (    s6   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/threads.pyR_   ?  s    (   R   R;   R   Rb   RU   R_   (    (    (    s6   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/threads.pyRi   1  s   	()   Ra   t
   __future__R    R   R0   R   R$   R   R'   t
   contextlibR   t   celery.localR   t   celery.fiveR   R   t   __all__t   environt   getR   t   version_infot   PY3R   t   ThreadR   t   greenletR<   R
   t   ImportErrort   _threadt   threadt   _dummy_threadt   dummy_threadR?   RB   R   RQ   R	   R>   Ri   R   (    (    (    s6   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/threads.pyt   <module>   sH   4 	&]/	