ó
è¾bc           @   s  d  Z  d d l 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 m	 Z	 m
 Z
 m Z m Z m Z m Z m Z m Z m Z m Z m Z m Z m Z e e e e e e e f ƒ Z y e Wn e k
 rë i  Z n Xd „  Z d e f d „  ƒ  YZ e e e f Z d „  Z  d „  Z! d	 „  Z" d
 „  Z# d e$ d „ Z% d e$ d „ Z& e& Z' d e( e$ e$ d „ Z) d f  d „  ƒ  YZ* d e* f d „  ƒ  YZ+ d „  Z, e$ e( d „ Z- e j. d k rÿd d l/ Z/ d f  d „  ƒ  YZ0 d e* f d „  ƒ  YZ1 n  d S(   s   Basic infrastructure for asynchronous socket service clients and servers.

There are only two ways to have a program on a single processor do "more
than one thing at a time".  Multi-threaded programming is the simplest and
most popular way to do it, but there is another very different technique,
that lets you have nearly all the advantages of multi-threading, without
actually using multiple threads. it's really only practical if your program
is largely I/O bound. If your program is CPU bound, then pre-emptive
scheduled threads are probably what you really need. Network servers are
rarely CPU-bound, however.

If your operating system supports the select() system call in its I/O
library (and nearly all do), then you can use it to juggle multiple
communication channels at once; doing other work while your I/O is taking
place in the "background."  Although this strategy can seem strange and
complex, especially at first, it is in many ways easier to understand and
control than multi-threaded programming. The module documented here solves
many of the difficult problems for you, making the task of building
sophisticated high-performance network servers and clients a snap.
iÿÿÿÿN(   t   EALREADYt   EINPROGRESSt   EWOULDBLOCKt
   ECONNRESETt   EINVALt   ENOTCONNt	   ESHUTDOWNt   EINTRt   EISCONNt   EBADFt   ECONNABORTEDt   EPIPEt   EAGAINt	   errorcodec         C   sK   y t  j |  ƒ SWn3 t t t f k
 rF |  t k r> t |  Sd |  SXd  S(   Ns   Unknown error %s(   t   ost   strerrort
   ValueErrort   OverflowErrort	   NameErrorR   (   t   err(    (    s   /usr/lib/python2.7/asyncore.pyt	   _strerrorD   s    t   ExitNowc           B   s   e  Z RS(    (   t   __name__t
   __module__(    (    (    s   /usr/lib/python2.7/asyncore.pyR   L   s   c         C   s9   y |  j  ƒ  Wn$ t k
 r$ ‚  n |  j ƒ  n Xd  S(   N(   t   handle_read_eventt   _reraised_exceptionst   handle_error(   t   obj(    (    s   /usr/lib/python2.7/asyncore.pyt   readQ   s    c         C   s9   y |  j  ƒ  Wn$ t k
 r$ ‚  n |  j ƒ  n Xd  S(   N(   t   handle_write_eventR   R   (   R   (    (    s   /usr/lib/python2.7/asyncore.pyt   writeY   s    c         C   s9   y |  j  ƒ  Wn$ t k
 r$ ‚  n |  j ƒ  n Xd  S(   N(   t   handle_expt_eventR   R   (   R   (    (    s   /usr/lib/python2.7/asyncore.pyt
   _exceptiona   s    c         C   sä   yz | t  j @r |  j ƒ  n  | t  j @r7 |  j ƒ  n  | t  j @rQ |  j ƒ  n  | t  j t  j Bt  j	 B@ry |  j
 ƒ  n  Wnc t j k
 r¼ } | j d t k r¯ |  j ƒ  qà |  j
 ƒ  n$ t k
 rÏ ‚  n |  j ƒ  n Xd  S(   Ni    (   t   selectt   POLLINR   t   POLLOUTR   t   POLLPRIR   t   POLLHUPt   POLLERRt   POLLNVALt   handle_closet   sockett   errort   argst   _DISCONNECTEDR   R   (   R   t   flagst   e(    (    s   /usr/lib/python2.7/asyncore.pyt	   readwritei   s"    g        c   
      C   s  | d  k r t } n  | rþg  } g  } g  } x„ | j ƒ  D]v \ } } | j ƒ  } | j ƒ  } | rt | j | ƒ n  | r” | j r” | j | ƒ n  | s  | r: | j | ƒ q: q: Wg  | k oÖ | k oÖ | k n rì t j |  ƒ d  Sy% t	 j	 | | | |  ƒ \ } } } Wn3 t	 j
 k
 rF}	 |	 j d t k r?‚  qGd  Sn Xx9 | D]1 } | j | ƒ } | d  k ruqNn  t | ƒ qNWx9 | D]1 } | j | ƒ } | d  k r±qŠn  t | ƒ qŠWx< | D]1 } | j | ƒ } | d  k ríqÆn  t | ƒ qÆWn  d  S(   Ni    (   t   Nonet
   socket_mapt   itemst   readablet   writablet   appendt	   acceptingt   timet   sleepR!   R*   R+   R   t   getR   R   R    (
   t   timeoutt   mapt   rt   wR.   t   fdR   t   is_rt   is_wR   (    (    s   /usr/lib/python2.7/asyncore.pyt   poll}   sN    	  '%c         C   s}  | d  k r t } n  |  d  k	 r4 t |  d ƒ }  n  t j ƒ  } | ryx | j ƒ  D] \ } } d } | j ƒ  rˆ | t j t j BO} n  | j	 ƒ  r® | j
 r® | t j O} n  | rS | t j t j Bt j BO} | j | | ƒ qS qS Wy | j |  ƒ } Wn5 t j k
 r0} | j d t k r'‚  n  g  } n XxE | D]: \ } } | j | ƒ } | d  k req8n  t | | ƒ q8Wn  d  S(   Niè  i    (   R0   R1   t   intR!   RA   R2   R3   R"   R$   R4   R6   R#   R&   R%   R'   t   registerR*   R+   R   R9   R/   (   R:   R;   t   pollsterR>   R   R-   R<   R   (    (    s   /usr/lib/python2.7/asyncore.pyt   poll2ª   s4    	
g      >@c         C   s–   | d  k r t } n  | r3 t t d ƒ r3 t } n t } | d  k rb xJ | r^ | |  | ƒ qH Wn0 x- | r‘ | d k r‘ | |  | ƒ | d } qe Wd  S(   NRA   i    i   (   R0   R1   t   hasattrR!   RE   RA   (   R:   t   use_pollR;   t   countt   poll_fun(    (    s   /usr/lib/python2.7/asyncore.pyt   loopÍ   s    			t
   dispatcherc           B   sa  e  Z e Z e Z e Z e Z e Z d  Z	 e
 d  g ƒ Z d  d  d „ Z d „  Z e Z d  d „ Z d  d „ Z d „  Z d  d „ Z d „  Z d „  Z d	 „  Z d
 „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d d „ Z d „  Z  d „  Z! d „  Z" d „  Z# d „  Z$ d „  Z% d „  Z& d „  Z' d „  Z( d „  Z) d „  Z* RS(!   t   warningc         C   sÇ   | d  k r t |  _ n	 | |  _ d  |  _ | rº | j d ƒ |  j | | ƒ t |  _ y | j ƒ  |  _	 WqÃ t
 j k
 r¶ } | j d t t f k r£ t |  _ q· |  j | ƒ ‚  qÃ Xn	 d  |  _
 d  S(   Ni    (   R0   R1   t   _mapt   _filenot   setblockingt
   set_sockett   Truet	   connectedt   getpeernamet   addrR)   R*   R+   R   R   t   Falset   del_channel(   t   selft   sockR;   R   (    (    s   /usr/lib/python2.7/asyncore.pyt   __init__é   s     			
c         C   sÉ   |  j  j d |  j  j g } |  j r? |  j r? | j d ƒ n |  j rX | j d ƒ n  |  j d  k	 r¬ y | j d |  j ƒ Wq¬ t k
 r¨ | j t	 |  j ƒ ƒ q¬ Xn  d d j
 | ƒ t |  ƒ f S(   Nt   .t	   listeningRR   s   %s:%ds   <%s at %#x>t    (   t	   __class__R   R   R6   RT   R5   RR   R0   t	   TypeErrort   reprt   joint   id(   RW   t   status(    (    s   /usr/lib/python2.7/asyncore.pyt   __repr__	  s    	c         C   s)   | d  k r |  j } n  |  | |  j <d  S(   N(   R0   RM   RN   (   RW   R;   (    (    s   /usr/lib/python2.7/asyncore.pyt   add_channel  s    c         C   sD   |  j  } | d  k r! |  j } n  | | k r7 | | =n  d  |  _  d  S(   N(   RN   R0   RM   (   RW   R;   R>   (    (    s   /usr/lib/python2.7/asyncore.pyRV     s    	
c         C   s?   | | f |  _  t j | | ƒ } | j d ƒ |  j | ƒ d  S(   Ni    (   t   family_and_typeR)   RO   RP   (   RW   t   familyt   typeRX   (    (    s   /usr/lib/python2.7/asyncore.pyt   create_socket'  s    c         C   s)   | |  _  | j ƒ  |  _ |  j | ƒ d  S(   N(   R)   t   filenoRN   Rd   (   RW   RX   R;   (    (    s   /usr/lib/python2.7/asyncore.pyRP   -  s    	c         C   sT   y9 |  j  j t  j t  j |  j  j t  j t  j ƒ d Bƒ Wn t  j k
 rO n Xd  S(   Ni   (   R)   t
   setsockoptt
   SOL_SOCKETt   SO_REUSEADDRt
   getsockoptR*   (   RW   (    (    s   /usr/lib/python2.7/asyncore.pyt   set_reuse_addr3  s    	c         C   s   t  S(   N(   RQ   (   RW   (    (    s   /usr/lib/python2.7/asyncore.pyR3   D  s    c         C   s   t  S(   N(   RQ   (   RW   (    (    s   /usr/lib/python2.7/asyncore.pyR4   G  s    c         C   s=   t  |  _ t j d k r- | d k r- d } n  |  j j | ƒ S(   Nt   nti   (   RQ   R6   R   t   nameR)   t   listen(   RW   t   num(    (    s   /usr/lib/python2.7/asyncore.pyRq   N  s    		c         C   s   | |  _  |  j j | ƒ S(   N(   RT   R)   t   bind(   RW   RT   (    (    s   /usr/lib/python2.7/asyncore.pyRs   T  s    	c         C   s£   t  |  _ t |  _ |  j j | ƒ } | t t t f k sT | t	 k ra t
 j d k ra | |  _ d  S| d t f k r‰ | |  _ |  j ƒ  n t j | t | ƒ ‚ d  S(   NRo   t   cei    (   Ro   Rt   (   RU   RR   RQ   t
   connectingR)   t
   connect_exR   R    R   R   R   Rp   RT   R   t   handle_connect_eventR*   R   (   RW   t   addressR   (    (    s   /usr/lib/python2.7/asyncore.pyt   connectX  s    				c         C   st   y |  j  j ƒ  \ } } WnJ t k
 r- d  St  j k
 re } | j d t t t f k r_ d  S‚  n X| | f Sd  S(   Ni    (	   R)   t   acceptR^   R0   R*   R+   R   R
   R   (   RW   t   connRT   t   why(    (    s   /usr/lib/python2.7/asyncore.pyRz   f  s    c         C   sr   y |  j  j | ƒ } | SWnQ t  j k
 rm } | j d t k rF d S| j d t k rg |  j ƒ  d S‚  n Xd  S(   Ni    (   R)   t   sendR*   R+   R   R,   R(   (   RW   t   datat   resultR|   (    (    s   /usr/lib/python2.7/asyncore.pyR}   t  s    
c         C   so   y. |  j  j | ƒ } | s) |  j ƒ  d S| SWn: t  j k
 rj } | j d t k rd |  j ƒ  d S‚  n Xd  S(   Nt    i    (   R)   t   recvR(   R*   R+   R,   (   RW   t   buffer_sizeR~   R|   (    (    s   /usr/lib/python2.7/asyncore.pyR     s    

c         C   sr   t  |  _ t  |  _ t  |  _ |  j ƒ  y |  j j ƒ  Wn5 t j k
 rm } | j d t	 t
 f k rn ‚  qn n Xd  S(   Ni    (   RU   RR   R6   Ru   RV   R)   t   closeR*   R+   R   R	   (   RW   R|   (    (    s   /usr/lib/python2.7/asyncore.pyRƒ   “  s    			
c         C   s‚   y t  |  j | ƒ } Wn- t k
 rE t d |  j j | f ƒ ‚ n9 Xd i |  j j d 6| d 6} t j | t d d ƒ| Sd  S(   Ns!   %s instance has no attribute '%s'sB   %(me)s.%(attr)s is deprecated. Use %(me)s.socket.%(attr)s instead.t   met   attrt
   stackleveli   (   t   getattrR)   t   AttributeErrorR]   R   t   warningst   warnt   DeprecationWarning(   RW   R…   t   retattrt   msg(    (    s   /usr/lib/python2.7/asyncore.pyt   __getattr__   s    c         C   s   t  j j d t | ƒ ƒ d  S(   Ns   log: %s
(   t   syst   stderrR   t   str(   RW   t   message(    (    s   /usr/lib/python2.7/asyncore.pyt   log°  s    t   infoc         C   s%   | |  j  k r! d | | f GHn  d  S(   Ns   %s: %s(   t   ignore_log_types(   RW   R’   Rg   (    (    s   /usr/lib/python2.7/asyncore.pyt   log_info³  s    c         C   sP   |  j  r |  j ƒ  n6 |  j sB |  j r5 |  j ƒ  n  |  j ƒ  n
 |  j ƒ  d  S(   N(   R6   t   handle_acceptRR   Ru   Rw   t   handle_read(   RW   (    (    s   /usr/lib/python2.7/asyncore.pyR   ·  s    			c         C   sb   |  j  j t  j t  j ƒ } | d k rB t  j | t | ƒ ƒ ‚ n  |  j ƒ  t |  _ t	 |  _
 d  S(   Ni    (   R)   Rm   Rk   t   SO_ERRORR*   R   t   handle_connectRQ   RR   RU   Ru   (   RW   R   (    (    s   /usr/lib/python2.7/asyncore.pyRw   Ã  s    
	c         C   s=   |  j  r d  S|  j s/ |  j r/ |  j ƒ  q/ n  |  j ƒ  d  S(   N(   R6   RR   Ru   Rw   t   handle_write(   RW   (    (    s   /usr/lib/python2.7/asyncore.pyR   Ë  s    			c         C   sB   |  j  j t  j t  j ƒ } | d k r4 |  j ƒ  n
 |  j ƒ  d  S(   Ni    (   R)   Rm   Rk   R™   R(   t   handle_expt(   RW   R   (    (    s   /usr/lib/python2.7/asyncore.pyR   Ö  s    c         C   sm   t  ƒ  \ } } } } y t |  ƒ } Wn d t |  ƒ } n X|  j d | | | | f d ƒ |  j ƒ  d  S(   Ns)   <__repr__(self) failed for object at %0x>s:   uncaptured python exception, closing channel %s (%s:%s %s)R*   (   t   compact_tracebackR_   Ra   R–   R(   (   RW   t   nilt   tt   vt   tbinfot	   self_repr(    (    s   /usr/lib/python2.7/asyncore.pyR   å  s    c         C   s   |  j  d d ƒ d  S(   Ns!   unhandled incoming priority eventRL   (   R–   (   RW   (    (    s   /usr/lib/python2.7/asyncore.pyRœ   ù  s    c         C   s   |  j  d d ƒ d  S(   Ns   unhandled read eventRL   (   R–   (   RW   (    (    s   /usr/lib/python2.7/asyncore.pyR˜   ü  s    c         C   s   |  j  d d ƒ d  S(   Ns   unhandled write eventRL   (   R–   (   RW   (    (    s   /usr/lib/python2.7/asyncore.pyR›   ÿ  s    c         C   s   |  j  d d ƒ d  S(   Ns   unhandled connect eventRL   (   R–   (   RW   (    (    s   /usr/lib/python2.7/asyncore.pyRš     s    c         C   s   |  j  d d ƒ d  S(   Ns   unhandled accept eventRL   (   R–   (   RW   (    (    s   /usr/lib/python2.7/asyncore.pyR—     s    c         C   s   |  j  d d ƒ |  j ƒ  d  S(   Ns   unhandled close eventRL   (   R–   Rƒ   (   RW   (    (    s   /usr/lib/python2.7/asyncore.pyR(     s    N(+   R   R   RU   t   debugRR   R6   Ru   t   closingR0   RT   t	   frozensetR•   RY   Rc   t   __str__Rd   RV   Rh   RP   Rn   R3   R4   Rq   Rs   Ry   Rz   R}   R   Rƒ   RŽ   R“   R–   R   Rw   R   R   R   Rœ   R˜   R›   Rš   R—   R(   (    (    (    s   /usr/lib/python2.7/asyncore.pyRK   ß   sL    																									t   dispatcher_with_sendc           B   s;   e  Z d d d  „ Z d „  Z d „  Z d „  Z d „  Z RS(   c         C   s    t  j |  | | ƒ d |  _ d  S(   NR€   (   RK   RY   t
   out_buffer(   RW   RX   R;   (    (    s   /usr/lib/python2.7/asyncore.pyRY     s    c         C   s3   d } t  j |  |  j d  ƒ } |  j | |  _ d  S(   Ni    i   (   RK   R}   R¨   (   RW   t   num_sent(    (    s   /usr/lib/python2.7/asyncore.pyt   initiate_send  s    c         C   s   |  j  ƒ  d  S(   N(   Rª   (   RW   (    (    s   /usr/lib/python2.7/asyncore.pyR›     s    c         C   s   |  j  p t |  j ƒ S(   N(   RR   t   lenR¨   (   RW   (    (    s   /usr/lib/python2.7/asyncore.pyR4     s    c         C   sA   |  j  r# |  j d t | ƒ ƒ n  |  j | |  _ |  j ƒ  d  S(   Ns
   sending %s(   R£   R–   R_   R¨   Rª   (   RW   R~   (    (    s   /usr/lib/python2.7/asyncore.pyR}   "  s    	N(   R   R   R0   RY   Rª   R›   R4   R}   (    (    (    s   /usr/lib/python2.7/asyncore.pyR§     s
   			c    	      C   sÌ   t  j ƒ  \ }  } } g  } | s0 t d ƒ ‚ n  xD | rv | j | j j j | j j j t | j	 ƒ f ƒ | j
 } q3 W~ | d \ } } } d j g  | D] } d | ^ qš ƒ } | | | f |  | | f S(   Ns   traceback does not existiÿÿÿÿR\   s
   [%s|%s|%s](   R   t   exc_infot   AssertionErrorR5   t   tb_framet   f_codet   co_filenamet   co_nameR‘   t	   tb_linenot   tb_nextR`   (	   RŸ   R    t   tbR¡   t   filet   functiont   linet   xR”   (    (    s   /usr/lib/python2.7/asyncore.pyR   ,  s    	&c         C   s¥   |  d  k r t }  n  x |  j ƒ  D]q } y | j ƒ  Wq" t k
 rm } | j d t k r^ q“ | s“ ‚  q“ q" t k
 r€ ‚  q" | s“ ‚  q“ q" Xq" W|  j ƒ  d  S(   Ni    (	   R0   R1   t   valuesRƒ   t   OSErrorR+   R	   R   t   clear(   R;   t
   ignore_allR¸   (    (    s   /usr/lib/python2.7/asyncore.pyt	   close_all@  s     		t   posixt   file_wrapperc           B   sM   e  Z d  „  Z d „  Z d „  Z d d „ Z e Z e Z d „  Z	 d „  Z
 RS(   c         C   s   t  j | ƒ |  _ d  S(   N(   R   t   dupR>   (   RW   R>   (    (    s   /usr/lib/python2.7/asyncore.pyRY   g  s    c         G   s   t  j |  j | Œ S(   N(   R   R   R>   (   RW   R+   (    (    s   /usr/lib/python2.7/asyncore.pyR   j  s    c         G   s   t  j |  j | Œ S(   N(   R   R   R>   (   RW   R+   (    (    s   /usr/lib/python2.7/asyncore.pyR}   m  s    c         C   s9   | t  j k r) | t  j k r) | r) d St d ƒ ‚ d  S(   Ni    s-   Only asyncore specific behaviour implemented.(   R)   Rk   R™   t   NotImplementedError(   RW   t   levelt   optnamet   buflen(    (    s   /usr/lib/python2.7/asyncore.pyRm   p  s
    c         C   s6   |  j  d k  r d  S|  j  } d |  _  t j | ƒ d  S(   Ni    iÿÿÿÿ(   R>   R   Rƒ   (   RW   R>   (    (    s   /usr/lib/python2.7/asyncore.pyRƒ   {  s
    		c         C   s   |  j  S(   N(   R>   (   RW   (    (    s   /usr/lib/python2.7/asyncore.pyRi   ‚  s    N(   R   R   RY   R   R}   R0   Rm   R   R   Rƒ   Ri   (    (    (    s   /usr/lib/python2.7/asyncore.pyR¿   b  s   				t   file_dispatcherc           B   s   e  Z d d  „ Z d „  Z RS(   c         C   sŒ   t  j |  d  | ƒ t |  _ y | j ƒ  } Wn t k
 r? n X|  j | ƒ t j | t j	 d ƒ } | t
 j B} t j | t j | ƒ d  S(   Ni    (   RK   RY   R0   RQ   RR   Ri   Rˆ   t   set_filet   fcntlt   F_GETFLR   t
   O_NONBLOCKt   F_SETFL(   RW   R>   R;   R-   (    (    s   /usr/lib/python2.7/asyncore.pyRY   ‡  s    	c         C   s/   t  | ƒ |  _ |  j j ƒ  |  _ |  j ƒ  d  S(   N(   R¿   R)   Ri   RN   Rd   (   RW   R>   (    (    s   /usr/lib/python2.7/asyncore.pyRÆ   ”  s    N(   R   R   R0   RY   RÆ   (    (    (    s   /usr/lib/python2.7/asyncore.pyRÅ   …  s   (2   t   __doc__R!   R)   R   R7   R‰   R   t   errnoR    R   R   R   R   R   R   R   R   R	   R
   R   R   R   R¥   R,   R1   R   R   t	   ExceptionR   t   KeyboardInterruptt
   SystemExitR   R   R   R    R/   R0   RA   RE   t   poll3RU   RJ   RK   R§   R   R½   Rp   RÇ   R¿   RÅ   (    (    (    s   /usr/lib/python2.7/asyncore.pyt   <module>/   sB   ^
					-!ÿ 3	#