Ñò
mÈKc           @   sË  d  Z  d d k Z d d k Z d d k Z d d k Z d d k Z d d k l Z l Z l	 Z	 l
 Z
 l Z l Z l Z l Z l Z l Z l Z y e Wn e j
 o h  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 i( d j o9 d d k) Z) d f  d „  ƒ  YZ* d e$ f d „  ƒ  YZ+ 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   ENOTCONNt	   ESHUTDOWNt   EINTRt   EISCONNt   EBADFt   ECONNABORTEDt	   errorcodec         C   s.   t  i |  ƒ } | d j o t |  } n | S(   Ns   Unknown error(   t   ost   strerrorR
   (   t   errt   res(    (    s   /usr/lib/python2.6/asyncore.pyt	   _strerror?   s    t   ExitNowc           B   s   e  Z RS(    (   t   __name__t
   __module__(    (    (    s   /usr/lib/python2.6/asyncore.pyR   E   s   c         C   s;   y |  i  ƒ  Wn& t j
 o
 ‚  n |  i ƒ  n Xd  S(   N(   t   handle_read_eventt   _reraised_exceptionst   handle_error(   t   obj(    (    s   /usr/lib/python2.6/asyncore.pyt   readJ   s    c         C   s;   y |  i  ƒ  Wn& t j
 o
 ‚  n |  i ƒ  n Xd  S(   N(   t   handle_write_eventR   R   (   R   (    (    s   /usr/lib/python2.6/asyncore.pyt   writeR   s    c         C   s;   y |  i  ƒ  Wn& t j
 o
 ‚  n |  i ƒ  n Xd  S(   N(   t   handle_expt_eventR   R   (   R   (    (    s   /usr/lib/python2.6/asyncore.pyt
   _exceptionZ   s    c         C   s¯   y‚ | t  i @o |  i ƒ  n | t  i @o |  i ƒ  n | t  i t  i Bt  i B@o |  i ƒ  n | t  i	 @o |  i
 ƒ  n Wn& t j
 o
 ‚  n |  i ƒ  n Xd  S(   N(   t   selectt   POLLINR   t   POLLOUTR   t   POLLHUPt   POLLERRt   POLLNVALt   handle_closet   POLLPRIR   R   R   (   R   t   flags(    (    s   /usr/lib/python2.6/asyncore.pyt	   readwriteb   s    g        c   
      C   s  | d  j o
 t } n | oïg  } g  } g  } x | i ƒ  D]s \ } } | i ƒ  } | i ƒ  } | o | i | ƒ n | o | i | ƒ n | p | o | i | ƒ q= q= Wg  | j o | j o
 | j n o t i |  ƒ d  Sy% t i | | | |  ƒ \ } } } Wn7 t i	 j
 o( }	 |	 i
 d t j o ‚  qOd  Sn Xx; | D]3 } | i | ƒ } | d  j o qVn t | ƒ qVWx; | D]3 } | i | ƒ } | d  j o q”n t | ƒ q”Wx? | D]3 } | i | ƒ } | d  j o qÒn t | ƒ qÒWn d  S(   Ni    (   t   Nonet
   socket_mapt   itemst   readablet   writablet   appendt   timet   sleepR   t   errort   argsR   t   getR   R   R   (
   t   timeoutt   mapt   rt   wt   et   fdR   t   is_rt   is_wR   (    (    s   /usr/lib/python2.6/asyncore.pyt   pollq   sV    
   *%	   c      	   C   s…  | d  j o
 t } n |  d  j	 o t |  d ƒ }  n t i ƒ  } | o6x™ | i ƒ  D]‹ \ } } d } | i ƒ  o | t i t i BO} n | i	 ƒ  o | t i
 O} n | o/ | t i t i Bt i BO} | i | | ƒ qX qX Wy | i |  ƒ } Wn9 t i j
 o* } | i d t j o ‚  n g  } n XxH | D]< \ } } | i | ƒ } | d  j o q=n t | | ƒ q=Wn d  S(   Niè  i    (   R&   R'   t   intR   R9   R(   R)   R   R#   R*   R   R    R   R!   t   registerR.   R/   R   R0   R%   (   R1   R2   t   pollsterR6   R   R$   R3   R   (    (    s   /usr/lib/python2.6/asyncore.pyt   poll2   s8    
  g      >@c         C   s¢   | d  j o
 t } n | o t t d ƒ o
 t } n t } | d  j o  xP | o | |  | ƒ qN Wn4 x0 | o( | d j o | |  | ƒ | d } qn Wd  S(   NR9   i    i   (   R&   R'   t   hasattrR   R=   R9   (   R1   t   use_pollR2   t   countt   poll_fun(    (    s   /usr/lib/python2.6/asyncore.pyt   loop¿   s    

  t
   dispatcherc           B   sU  e  Z e Z e Z e Z e Z d  Z e	 d  g ƒ Z
 d  d  d „ Z d „  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  j o t |  _ n
 | |  _ d  |  _ | o‰ | i d ƒ |  i | | ƒ t |  _ y | i ƒ  |  _	 WqÅ t
 i j
 o: } | i d t j o t |  _ q¸ |  i | ƒ ‚  qÅ Xn
 d  |  _
 d  S(   Ni    (   R&   R'   t   _mapt   _filenot   setblockingt
   set_sockett   Truet	   connectedt   getpeernamet   addrt   socketR.   R/   R   t   Falset   del_channel(   t   selft   sockR2   R   (    (    s   /usr/lib/python2.6/asyncore.pyt   __init__Ú   s     			c         C   sÒ   |  i  i d |  i  i g } |  i o |  i o | i d ƒ n |  i o | i d ƒ n |  i d  j	 oH y | i d |  i ƒ Wqµ t j
 o | i t	 |  i ƒ ƒ qµ Xn d d i
 | ƒ t |  ƒ f S(   Nt   .t	   listeningRJ   s   %s:%ds   <%s at %#x>t    (   t	   __class__R   R   t	   acceptingRL   R+   RJ   R&   t	   TypeErrort   reprt   joint   id(   RP   t   status(    (    s   /usr/lib/python2.6/asyncore.pyt   __repr__ú   s    
c         C   s+   | d  j o |  i } n |  | |  i <d  S(   N(   R&   RE   RF   (   RP   R2   (    (    s   /usr/lib/python2.6/asyncore.pyt   add_channel  s    c         C   sH   |  i  } | d  j o |  i } n | | j o | | =n d  |  _  d  S(   N(   RF   R&   RE   (   RP   R2   R6   (    (    s   /usr/lib/python2.6/asyncore.pyRO     s    	c         C   s?   | | f |  _  t i | | ƒ } | i d ƒ |  i | ƒ d  S(   Ni    (   t   family_and_typeRM   RG   RH   (   RP   t   familyt   typeRQ   (    (    s   /usr/lib/python2.6/asyncore.pyt   create_socket  s    c         C   s)   | |  _  | i ƒ  |  _ |  i | ƒ d  S(   N(   RM   t   filenoRF   R^   (   RP   RQ   R2   (    (    s   /usr/lib/python2.6/asyncore.pyRH     s    	c      	   C   sV   y9 |  i  i t  i t  i |  i  i t  i t  i ƒ d Bƒ Wn t  i j
 o n Xd  S(   Ni   (   RM   t
   setsockoptt
   SOL_SOCKETt   SO_REUSEADDRt
   getsockoptR.   (   RP   (    (    s   /usr/lib/python2.6/asyncore.pyt   set_reuse_addr"  s    	c         C   s   t  S(   N(   RI   (   RP   (    (    s   /usr/lib/python2.6/asyncore.pyR)   3  s    c         C   s   t  S(   N(   RI   (   RP   (    (    s   /usr/lib/python2.6/asyncore.pyR*   6  s    c         C   s@   t  |  _ t i d j o | d j o
 d } n |  i i | ƒ S(   Nt   nti   (   RI   RW   R   t   nameRM   t   listen(   RP   t   num(    (    s   /usr/lib/python2.6/asyncore.pyRk   =  s    	
c         C   s   | |  _  |  i i | ƒ S(   N(   RL   RM   t   bind(   RP   RL   (    (    s   /usr/lib/python2.6/asyncore.pyRm   C  s    	c         C   sz   t  |  _ |  i i | ƒ } | t t t f j o d  S| d t f j o | |  _ |  i	 ƒ  n t i
 | t | ƒ ‚ d  S(   Ni    (   RN   RJ   RM   t
   connect_exR   R    R   R   RL   t   handle_connect_eventR.   R
   (   RP   t   addressR   (    (    s   /usr/lib/python2.6/asyncore.pyt   connectG  s    		c         C   s]   y# |  i  i ƒ  \ } } | | f SWn3 t  i j
 o$ } | i d t j o qY ‚  n Xd  S(   Ni    (   RM   t   acceptR.   R/   R   (   RP   t   connRL   t   why(    (    s   /usr/lib/python2.6/asyncore.pyRr   S  s    c         C   s„   y |  i  i | ƒ } | SWnc t  i j
 oT } | i d t j o d S| i d t t t t f j o |  i	 ƒ  d S‚  n Xd  S(   Ni    (
   RM   t   sendR.   R/   R   R   R   R   R	   R"   (   RP   t   datat   resultRt   (    (    s   /usr/lib/python2.6/asyncore.pyRu   ^  s     
c         C   s   y0 |  i  i | ƒ } | p |  i ƒ  d S| SWnJ t  i j
 o; } | i d t t t t g j o |  i ƒ  d S‚  n Xd  S(   Nt    i    (	   RM   t   recvR"   R.   R/   R   R   R   R	   (   RP   t   buffer_sizeRv   Rt   (    (    s   /usr/lib/python2.6/asyncore.pyRy   k  s    
 
c         C   sm   t  |  _ t  |  _ |  i ƒ  y |  i i ƒ  Wn9 t i j
 o* } | i d t t	 f j o ‚  qi n Xd  S(   Ni    (
   RN   RJ   RW   RO   RM   t   closeR.   R/   R   R   (   RP   Rt   (    (    s   /usr/lib/python2.6/asyncore.pyR{   }  s    		
c         C   s   t  |  i | ƒ S(   N(   t   getattrRM   (   RP   t   attr(    (    s   /usr/lib/python2.6/asyncore.pyt   __getattr__‰  s    c         C   s   t  i i d t | ƒ ƒ d  S(   Ns   log: %s
(   t   syst   stderrR   t   str(   RP   t   message(    (    s   /usr/lib/python2.6/asyncore.pyt   log  s    t   infoc         C   s'   | |  i  j o d | | f GHn d  S(   Ns   %s: %s(   t   ignore_log_types(   RP   R‚   Ra   (    (    s   /usr/lib/python2.6/asyncore.pyt   log_info“  s    c         C   sH   |  i  o |  i ƒ  n- |  i p |  i ƒ  |  i ƒ  n |  i ƒ  d  S(   N(   RW   t   handle_acceptRJ   Ro   t   handle_read(   RP   (    (    s   /usr/lib/python2.6/asyncore.pyR   —  s    


c         C   s   t  |  _ |  i ƒ  d  S(   N(   RI   RJ   t   handle_connect(   RP   (    (    s   /usr/lib/python2.6/asyncore.pyRo   ¢  s    	c         C   sy   |  i  o d  S|  i pR |  i i t i t i ƒ } | d j o t i | t | ƒ ƒ ‚ n |  i ƒ  n |  i	 ƒ  d  S(   Ni    (
   RW   RJ   RM   Rg   Re   t   SO_ERRORR.   R   Ro   t   handle_write(   RP   R   (    (    s   /usr/lib/python2.6/asyncore.pyR   ¦  s    

c         C   sD   |  i  i t  i t  i ƒ } | d j o |  i ƒ  n |  i ƒ  d  S(   Ni    (   RM   Rg   Re   RŠ   R"   t   handle_expt(   RP   R   (    (    s   /usr/lib/python2.6/asyncore.pyR   µ  s    c      	   C   sm   t  ƒ  \ } } } } y t |  ƒ } Wn d t |  ƒ } n X|  i d | | | | f d ƒ |  i ƒ  d  S(   Ns)   <__repr__(self) failed for object at %0x>s:   uncaptured python exception, closing channel %s (%s:%s %s)R.   (   t   compact_tracebackRY   R[   R†   R"   (   RP   t   nilt   tt   vt   tbinfot	   self_repr(    (    s   /usr/lib/python2.6/asyncore.pyR   Ä  s    c         C   s   |  i  d d ƒ d  S(   Ns!   unhandled incoming priority eventRD   (   R†   (   RP   (    (    s   /usr/lib/python2.6/asyncore.pyRŒ   Ø  s    c         C   s   |  i  d d ƒ d  S(   Ns   unhandled read eventRD   (   R†   (   RP   (    (    s   /usr/lib/python2.6/asyncore.pyRˆ   Û  s    c         C   s   |  i  d d ƒ d  S(   Ns   unhandled write eventRD   (   R†   (   RP   (    (    s   /usr/lib/python2.6/asyncore.pyR‹   Þ  s    c         C   s   |  i  d d ƒ d  S(   Ns   unhandled connect eventRD   (   R†   (   RP   (    (    s   /usr/lib/python2.6/asyncore.pyR‰   á  s    c         C   s   |  i  d d ƒ d  S(   Ns   unhandled accept eventRD   (   R†   (   RP   (    (    s   /usr/lib/python2.6/asyncore.pyR‡   ä  s    c         C   s   |  i  d d ƒ |  i ƒ  d  S(   Ns   unhandled close eventRD   (   R†   R{   (   RP   (    (    s   /usr/lib/python2.6/asyncore.pyR"   ç  s    N()   R   R   RN   t   debugRJ   RW   t   closingR&   RL   t	   frozensetR…   RR   R]   R^   RO   Rb   RH   Rh   R)   R*   Rk   Rm   Rq   Rr   Ru   Ry   R{   R~   Rƒ   R†   R   Ro   R   R   R   RŒ   Rˆ   R‹   R‰   R‡   R"   (    (    (    s   /usr/lib/python2.6/asyncore.pyRC   Ñ   sH    																									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  i |  | | ƒ d |  _ d  S(   NRx   (   RC   RR   t
   out_buffer(   RP   RQ   R2   (    (    s   /usr/lib/python2.6/asyncore.pyRR   ò  s    c         C   s3   d } t  i |  |  i d  ƒ } |  i | |  _ d  S(   Ni    i   (   RC   Ru   R—   (   RP   t   num_sent(    (    s   /usr/lib/python2.6/asyncore.pyt   initiate_sendö  s    c         C   s   |  i  ƒ  d  S(   N(   R™   (   RP   (    (    s   /usr/lib/python2.6/asyncore.pyR‹   û  s    c         C   s   |  i  p t |  i ƒ S(   N(   RJ   t   lenR—   (   RP   (    (    s   /usr/lib/python2.6/asyncore.pyR*   þ  s    c         C   sC   |  i  o |  i d t | ƒ ƒ n |  i | |  _ |  i ƒ  d  S(   Ns
   sending %s(   R“   R†   RY   R—   R™   (   RP   Rv   (    (    s   /usr/lib/python2.6/asyncore.pyRu     s    
N(   R   R   R&   RR   R™   R‹   R*   Ru   (    (    (    s   /usr/lib/python2.6/asyncore.pyR–   ð  s
   			c    
      C   sØ   t  i ƒ  \ }  } } g  } | p t d ƒ ‚ n xF | o> | i | i i i | i i i t | i	 ƒ f ƒ | i
 } q5 W~ | d \ } } } d i g  } | D] } | d | q¢ ~ ƒ }	 | | | f |  | |	 f S(   Ns   traceback does not existiÿÿÿÿRU   s
   [%s|%s|%s](   R   t   exc_infot   AssertionErrorR+   t   tb_framet   f_codet   co_filenamet   co_nameR   t	   tb_linenot   tb_nextRZ   (
   R   R   t   tbR‘   t   filet   functiont   linet   _[1]t   xR„   (    (    s   /usr/lib/python2.6/asyncore.pyR     s     .c         C   s±   |  d  j o
 t }  n x‰ |  i ƒ  D]{ } y | i ƒ  Wq$ t j
 o/ } | i d t j o qŸ | p ‚  qŸ q$ t j
 o
 ‚  q$ | p ‚  qŸ q$ Xq$ W|  i ƒ  d  S(   Ni    (	   R&   R'   t   valuesR{   t   OSErrorR/   R   R   t   clear(   R2   t
   ignore_allR¨   (    (    s   /usr/lib/python2.6/asyncore.pyt	   close_all  s"    
 t   posixt   file_wrapperc           B   sA   e  Z d  „  Z d „  Z d „  Z e Z e Z d „  Z d „  Z RS(   c         C   s   t  i | ƒ |  _ d  S(   N(   R   t   dupR6   (   RP   R6   (    (    s   /usr/lib/python2.6/asyncore.pyRR   F  s    c         G   s   t  i |  i | Œ S(   N(   R   R   R6   (   RP   R/   (    (    s   /usr/lib/python2.6/asyncore.pyRy   I  s    c         G   s   t  i |  i | Œ S(   N(   R   R   R6   (   RP   R/   (    (    s   /usr/lib/python2.6/asyncore.pyRu   L  s    c         C   s   t  i |  i ƒ d  S(   N(   R   R{   R6   (   RP   (    (    s   /usr/lib/python2.6/asyncore.pyR{   R  s    c         C   s   |  i  S(   N(   R6   (   RP   (    (    s   /usr/lib/python2.6/asyncore.pyRc   U  s    (	   R   R   RR   Ry   Ru   R   R   R{   Rc   (    (    (    s   /usr/lib/python2.6/asyncore.pyR¯   A  s   				t   file_dispatcherc           B   s   e  Z d d  „ Z d „  Z RS(   c         C   sŽ   t  i |  d  | ƒ t |  _ y | i ƒ  } Wn t j
 o n X|  i | ƒ t i | t i	 d ƒ } | t
 i B} t i | t i | ƒ d  S(   Ni    (   RC   RR   R&   RI   RJ   Rc   t   AttributeErrort   set_filet   fcntlt   F_GETFLR   t
   O_NONBLOCKt   F_SETFL(   RP   R6   R2   R$   (    (    s   /usr/lib/python2.6/asyncore.pyRR   Z  s    	c         C   s/   t  | ƒ |  _ |  i i ƒ  |  _ |  i ƒ  d  S(   N(   R¯   RM   Rc   RF   R^   (   RP   R6   (    (    s   /usr/lib/python2.6/asyncore.pyR³   g  s    N(   R   R   R&   RR   R³   (    (    (    s   /usr/lib/python2.6/asyncore.pyR±   X  s   (,   t   __doc__R   RM   R   R,   R   t   errnoR    R   R   R   R   R   R   R   R   R	   R
   R'   t	   NameErrorR   t	   ExceptionR   t   KeyboardInterruptt
   SystemExitR   R   R   R   R%   R&   R9   R=   t   poll3RN   RB   RC   R–   R   R­   Rj   R´   R¯   R±   (    (    (    s   /usr/lib/python2.6/asyncore.pyt   <module>/   s<   L					, ÿ  	