σ
O'^c           @@  s  d  Z  d d l m Z d d l Z d d l Z d d l m Z 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 d	 d l m Z d	 d l m Z 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" d Z# d Z$ d d g Z% e e&  Z' e' j( e' j) Z( Z) d e* f d     YZ+ d e* f d     YZ, d S(   s9   
kombu.pidbox
===============

Generic process mailbox.

i    (   t   absolute_importN(   t   defaultdictt   deque(   t   copy(   t   count(   t   local(   t   timei   (   t   Exchanget   Queuet   Consumert   Producer(   t   LamportClock(   t   maybe_declaret   oid_from(   t   InconsistencyError(   t   range(   t
   get_logger(   t   cached_propertyt   kwdictt   uuidt   reprcalli
   sύ   A node named {node.hostname} is already using this process mailbox!

Maybe you forgot to shutdown the other node or did not do so properly?
Or if you meant to start multiple nodes on the same host please make sure
you give each node a unique node name!
t   Nodet   Mailboxc           B@  sΉ   e  Z d Z d Z d Z d Z d Z d d d d d   Z d e	 d d  Z
 d   Z d   Z d d d  Z d d d d  Z i  d  Z d   Z d   Z d d	  Z e Z d
   Z RS(   c         C@  sX   | |  _  | |  _ | |  _ | |  _ |  j j j |  _ | d  k rK i  } n  | |  _ d  S(   N(	   t   channelt   mailboxt   hostnamet   statet   clockt   adjustt   adjust_clockt   Nonet   handlers(   t   selfR   R   R   R   R   (    (    s.   /tmp/pip-unpacked-wheel-UAnTfW/kombu/pidbox.pyt   __init__;   s    					c         @  sm     j  j   j  }   f d   } | | _ t | p<   j | g d | d | d  k rc   j  j n | | S(   Nc         @  s&   | r" t  j t j d     n  d  S(   Nt   node(   t   warningst   warnt   W_PIDBOX_IN_USEt   format(   t   namet   messagest	   consumers(   R    (    s.   /tmp/pip-unpacked-wheel-UAnTfW/kombu/pidbox.pyt   verify_exclusiveI   s    t   no_ackt   accept(   R   t	   get_queueR   t   on_declaredR	   R   R   R,   (   R    R   R+   R,   t   optionst   queueR*   (    (   R    s.   /tmp/pip-unpacked-wheel-UAnTfW/kombu/pidbox.pyR	   F   s    	c         C@  s   | |  j  | j <| S(   N(   R   t   __name__(   R    t   fun(    (    s.   /tmp/pip-unpacked-wheel-UAnTfW/kombu/pidbox.pyt   handlerT   s    c         C@  s   t  d | d d d  S(   Ns   Cannot decode message: %rt   exc_infoi   (   t   error(   R    t   messaget   exc(    (    s.   /tmp/pip-unpacked-wheel-UAnTfW/kombu/pidbox.pyt   on_decode_errorX   s    c         C@  s;   |  j  d | d | p |  j g d |  j  } | j   | S(   NR   t	   callbacksR8   (   R	   t   handle_messageR8   t   consume(   R    R   t   callbackt   consumer(    (    s.   /tmp/pip-unpacked-wheel-UAnTfW/kombu/pidbox.pyt   listen[   s
    
c   	      K@  sμ   | p	 i  } t  d t | d
 d | | |  | r= |  j pC |  j } y | | t |   } WnL t k
 ru   n9 t k
 r­ } t d | d d i t |  d 6} n X| rθ |  j	 i | |  j
 6d | d d | d d	 | n  | S(   Ns1   pidbox received method %s [reply_to:%s ticket:%s]t   kwargss   pidbox command error: %rR4   i   R5   t   exchanget   routing_keyt   ticket(    (   t   debugR   t   handle_callt   handle_castR   t
   SystemExitt	   ExceptionR5   t   reprt   replyR   (	   R    t   methodt	   argumentst   reply_toRB   R?   t   handleRI   R7   (    (    s.   /tmp/pip-unpacked-wheel-UAnTfW/kombu/pidbox.pyt   dispatchb   s"    


c         C@  s   |  j  | |  j |  S(   N(   R   R   (   R    RJ   RK   (    (    s.   /tmp/pip-unpacked-wheel-UAnTfW/kombu/pidbox.pyRM   w   s    c         C@  s   |  j  | |  S(   N(   RM   (   R    RJ   RK   (    (    s.   /tmp/pip-unpacked-wheel-UAnTfW/kombu/pidbox.pyRD   z   s    c         C@  s   |  j  | |  S(   N(   RM   (   R    RJ   RK   (    (    s.   /tmp/pip-unpacked-wheel-UAnTfW/kombu/pidbox.pyRE   }   s    c         C@  sd   | j  d  } | r7 |  j | j j  d  p0 d  n  | sM |  j | k r` |  j t |    Sd  S(   Nt   destinationR   i    (   t   getR   t   headersR   RN   R   (   R    t   bodyR6   RO   (    (    s.   /tmp/pip-unpacked-wheel-UAnTfW/kombu/pidbox.pyR:      s
    "c      	   K@  s2   |  j  j | | | | d |  j d |  j  j d  S(   NR   t
   serializer(   R   t   _publish_replyR   RS   (   R    t   dataR@   RA   RB   R?   (    (    s.   /tmp/pip-unpacked-wheel-UAnTfW/kombu/pidbox.pyRI      s    	N(   R1   t
   __module__R   R   R   R   R   R   R!   t   TrueR	   R3   R8   R>   RN   RM   RD   RE   R:   t   dispatch_from_messageRI   (    (    (    s.   /tmp/pip-unpacked-wheel-UAnTfW/kombu/pidbox.pyR   *   s&   
				c        	   B@  sa  e  Z e Z d  Z d Z d Z d Z d Z	 d Z
 d Z d g Z d Z d d d d d d  Z d   Z d d d d d  Z i  d d d d  Z i  d  Z i  d	  Z i  d
 d d d d  Z d   Z e d    Z d   Z d d  Z d d d d d d  Z d d e d
 d d d d d  Z d d
 d d d d  Z d   Z d   Z e d    Z RS(   s	   %s.pidboxs   reply.%s.pidboxt   directt   jsonc         C@  sΔ   | |  _  | |  _ | |  _ | d  k r0 t   n | |  _ |  j |  j  |  j  |  _ |  j |  j   |  _	 t
   |  _ t t  |  _ | d  k r |  j n | |  _ | d  k r· |  j n | |  _ d  S(   N(   t	   namespacet
   connectiont   typeR   R   R   t   _get_exchangeR@   t   _get_reply_exchanget   reply_exchangeR   t   _tlsR   R   t	   unclaimedR,   RS   (   R    R[   R]   R\   R   R,   RS   (    (    s.   /tmp/pip-unpacked-wheel-UAnTfW/kombu/pidbox.pyR!   ¨   s    			c         C@  s   t  |   } | | _ | S(   N(   R   R\   (   R    R\   t   bound(    (    s.   /tmp/pip-unpacked-wheel-UAnTfW/kombu/pidbox.pyt   __call__Ά   s    	c         C@  s.   | p t  j   } |  j | | | | d |  S(   NR   (   t   sockett   gethostnamet   node_cls(   R    R   R   R   R   (    (    s.   /tmp/pip-unpacked-wheel-UAnTfW/kombu/pidbox.pyR   »   s    c         C@  s+   |  j  | | | d t d | d | d | S(   NRI   t   timeoutR<   R   (   t
   _broadcastRW   (   R    RO   t   commandR?   Rh   R<   R   (    (    s.   /tmp/pip-unpacked-wheel-UAnTfW/kombu/pidbox.pyt   callΏ   s    c         C@  s   |  j  | | | d t S(   NRI   (   Ri   t   False(   R    RO   Rj   R?   (    (    s.   /tmp/pip-unpacked-wheel-UAnTfW/kombu/pidbox.pyt   castΖ   s    c         C@  s   |  j  | | d t S(   NRI   (   Ri   Rl   (   R    Rj   R?   (    (    s.   /tmp/pip-unpacked-wheel-UAnTfW/kombu/pidbox.pyt   abcastΙ   s    i   c         C@  s.   |  j  | | d t d | d | d | d | S(   NRI   Rh   t   limitR<   R   (   Ri   RW   (   R    Rj   R?   Rh   Ro   R<   R   (    (    s.   /tmp/pip-unpacked-wheel-UAnTfW/kombu/pidbox.pyt
   multi_callΜ   s    c         C@  sU   |  j  } t d | |  j j f d |  j d | d t d t d i t t d  d 6S(	   Ns   %s.%sR@   RA   t   durablet   auto_deletet   queue_argumentsiθ  s	   x-expires(   t   oidR   R`   R'   Rl   RW   t   intt   REPLY_QUEUE_EXPIRES(   R    Rt   (    (    s.   /tmp/pip-unpacked-wheel-UAnTfW/kombu/pidbox.pyt   get_reply_queueΣ   s    		c         C@  s
   |  j    S(   N(   Rw   (   R    (    (    s.   /tmp/pip-unpacked-wheel-UAnTfW/kombu/pidbox.pyt   reply_queueή   s    c         C@  s,   t  d | |  j f d |  j d t d t S(   Ns   %s.%s.pidboxR@   Rq   Rr   (   R   R[   R@   Rl   RW   (   R    R   (    (    s.   /tmp/pip-unpacked-wheel-UAnTfW/kombu/pidbox.pyR-   β   s    	c   	      K@  s    | p |  j  j } t | d d d d d t } t | d t } yF | j | d | d | d	 | g d
 i | d 6|  j j   d 6| Wn t k
 r n Xd  S(   Nt   exchange_typeRY   t   delivery_modet	   transientRq   t   auto_declareR@   RA   t   declareRQ   RB   R   (	   R\   t   default_channelR   Rl   R
   t   publishR   t   forwardR   (	   R    RI   R@   RA   RB   R   t   optst   chant   producer(    (    s.   /tmp/pip-unpacked-wheel-UAnTfW/kombu/pidbox.pyRT   θ   s    	c      
   C@  sω   i | d 6| d 6| d 6} | p* |  j  j }	 |  j }
 | r t |  j |   | j d | d i |  j j d 6|  j d 6 n  | p |  j	 } t
 |	 d t } | j | d |
 j d	 |
 g d
 i |  j j   d 6| rδ t   | n d d 6d | d  S(   NRJ   RK   RO   RB   RL   R@   RA   R|   R}   RQ   R   i    t   expiresRS   (   R\   R~   R@   R   Rx   t   updateR`   R'   Rt   RS   R
   Rl   R   R   R   R   (   R    R]   RK   RO   t   reply_ticketR   Rh   RS   R6   R   R@   R   (    (    s.   /tmp/pip-unpacked-wheel-UAnTfW/kombu/pidbox.pyt   _publishϊ   s"    

	c
         C@  s  | d  k	 r@ t | t t f  r@ t d j t |     n  | pI i  } | r[ t   p^ d  }
 | pp |  j j	 } | d  k r  | r  | r t
 |  p d  } n  |	 p¬ |  j }	 |  j | | d | d |
 d | d | d |	 |
 r|  j |
 d | d | d | d | Sd  S(	   Ns(   destination must be a list/tuple not {0}RO   R   R   Rh   RS   Ro   R<   (   R   t
   isinstancet   listt   tuplet
   ValueErrorR&   R]   R   R\   R~   t   lenRS   R   t   _collect(   R    Rj   RK   RO   RI   Rh   Ro   R<   R   RS   R   R   (    (    s.   /tmp/pip-unpacked-wheel-UAnTfW/kombu/pidbox.pyRi     s*    c      
   @  s;  | d  k r |  j } n  | p' |  j j } |  j } t | | g d | d t }	 g   |  j  |  j j	   y  j
   SWn t k
 r n X      f d   }
 |	 j |
  zj |	 ^ xR | rΨ t |  pή t   D]5 } y |  j j d |  Wqί t j k
 rPqί Xqί W SWd  QXWd  | j | j  Xd  S(   NR,   R+   c         @  s   | j  j }   | d  p d  | d  } | rG t   | k rG d  S| d   } |  k r  ru  |   n   j |   n  | j |   d  S(   NR   i    R   RB   (   RQ   RP   R   t   append(   RR   R6   t   headerR   t   this_id(   R   R<   t	   responsesRB   Rb   (    s.   /tmp/pip-unpacked-wheel-UAnTfW/kombu/pidbox.pyt
   on_message?  s    Rh   (   R   R,   R\   R~   Rx   R	   RW   Rb   R   R   t   popt   KeyErrort   register_callbackR   R   t   drain_eventsRe   Rh   t   after_reply_message_receivedR'   (   R    RB   Ro   Rh   R<   R   R,   R   R0   R=   R   t   i(    (   R   R<   R   RB   Rb   s.   /tmp/pip-unpacked-wheel-UAnTfW/kombu/pidbox.pyR   .  s.    		"	c         C@  s#   t  |  j | d | d t d d S(   NR]   Rq   Rz   R{   (   R   t   exchange_fmtRl   (   R    R[   R]   (    (    s.   /tmp/pip-unpacked-wheel-UAnTfW/kombu/pidbox.pyR^   Z  s    c         C@  s#   t  |  j | d d d t d d S(   NR]   RY   Rq   Rz   R{   (   R   t   reply_exchange_fmtRl   (   R    R[   (    (    s.   /tmp/pip-unpacked-wheel-UAnTfW/kombu/pidbox.pyR_   `  s    c         C@  s=   y |  j  j SWn( t k
 r8 t |   } |  j  _ | SXd  S(   N(   Ra   t   OIDt   AttributeErrorR   (   R    Rt   (    (    s.   /tmp/pip-unpacked-wheel-UAnTfW/kombu/pidbox.pyRt   f  s
    N(    R1   RV   R   Rg   R   R   R   R[   R\   R]   R@   R`   R,   RS   R!   Rd   Rk   Rm   Rn   Rp   Rw   R   Rx   R-   RT   R   Rl   Ri   R   R^   R_   Rt   (    (    (    s.   /tmp/pip-unpacked-wheel-UAnTfW/kombu/pidbox.pyR      sF   								*		(-   t   __doc__t
   __future__R    Re   R#   t   collectionsR   R   R   t	   itertoolsR   t	   threadingR   R   t    R   R   R	   R
   t   clocksR   t   commonR   R   t
   exceptionsR   t   fiveR   t   logR   t   utilsR   R   R   R   Rv   R%   t   __all__R1   t   loggerRC   R5   t   objectR   R   (    (    (    s.   /tmp/pip-unpacked-wheel-UAnTfW/kombu/pidbox.pyt   <module>   s,   ""d