mò
­fIc           @   s›  d  Z  d k Z d k Z d k Z d k Z d k Z d k Z d k 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 e i f d „  ƒ  YZ d „  Z d „  Z d „  Z d „  Z d	 d d
 „ Z d	 d d „ Z e Z d e d d d „ Z d f  d „  ƒ  YZ d e f d „  ƒ  YZ  d „  Z! d d „ Z" e i# d j o6 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.
N(	   s   EALREADYs   EINPROGRESSs   EWOULDBLOCKs
   ECONNRESETs   ENOTCONNs	   ESHUTDOWNs   EINTRs   EISCONNs	   errorcodet   ExitNowc           B   s   t  Z RS(   N(   t   __name__t
   __module__(    (    (    t%   /mit/python/lib/python2.4/asyncore.pyR    @   s   c         C   s;   y |  i ƒ  Wn& t j
 o
 ‚  n |  i ƒ  n Xd  S(   N(   t   objt   handle_read_eventR    t   handle_error(   R   (    (    R   t   readC   s    c         C   s;   y |  i ƒ  Wn& t j
 o
 ‚  n |  i ƒ  n Xd  S(   N(   R   t   handle_write_eventR    R   (   R   (    (    R   t   writeK   s    c         C   s;   y |  i ƒ  Wn& t j
 o
 ‚  n |  i ƒ  n Xd  S(   N(   R   t   handle_expt_eventR    R   (   R   (    (    R   t
   _exceptionS   s    c         C   sš   ym | t i t i B@o |  i ƒ  n | t i @o |  i ƒ  n | t i t i	 Bt i
 B@o |  i ƒ  n Wn& t j
 o
 ‚  n |  i ƒ  n Xd  S(   N(   t   flagst   selectt   POLLINt   POLLPRIR   R   t   POLLOUTR   t   POLLERRt   POLLHUPt   POLLNVALR
   R    R   (   R   R   (    (    R   t	   readwrite[   s    f0.0c   
      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 |  ƒ n] y% t i | | | |  ƒ \ } } } Wn4 t i j
 o% } | d t j o ‚  qKd  Sn Xx; | D]3 } | i | ƒ }	 |	 d  j o qRn t |	 ƒ qR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   mapt   Nonet
   socket_mapt   rt   wt   et   itemst   fdR   t   readablet   is_rt   writablet   is_wt   appendt   timet   sleept   timeoutR   t   errort   errt   EINTRt   getR   R	   R   (
   R$   R   R   R   R    R&   R   R   R   R   (    (    R   t   pollh   sP    
 *%	   c         C   s‚  | d  j o
 t } n |  d  j	 o t |  d ƒ }  n t i ƒ  } | o3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 |  ƒ } Wn6 t i j
 o' } | d t j o ‚  n g  } n XxH | D]< \ } } | i | ƒ } | d  j o q:n t | | ƒ q:Wn d  S(   Niè  i    (   R   R   R   R$   t   intR   R)   t   pollsterR   R   R   R   R   R   R   R   R   R   R   R   t   registerR   R%   R&   R'   R(   R   (   R$   R   R   R&   R+   R   R   R   (    (    R   t   poll2“   s8    
  f30.0c         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(   NR)   i    i   (   R   R   R   t   use_pollt   hasattrR   R-   t   poll_funR)   t   countR$   (   R$   R.   R   R1   R0   (    (    R   t   loopµ   s    

  t
   dispatcherc           B   s=  t  Z e Z e Z e Z e Z d  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% RS(   Nc         C   s   | d  j o t |  _ n
 | |  _ | oY |  i | | ƒ |  i i d ƒ t	 |  _
 y | i ƒ  |  _ WqŒ t i j
 o qŒ Xn
 d  |  _ d  S(   Ni    (   R   R   R   t   selft   _mapt   sockt
   set_sockett   sockett   setblockingt   Truet	   connectedt   getpeernamet   addrR%   (   R4   R6   R   (    (    R   t   __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	   listeningR;   s   %s:%ds   <%s at %#x>t    (   R4   t	   __class__R   R   t   statust	   acceptingR=   R!   R;   R   t	   TypeErrort   reprt   joint   id(   R4   RC   (    (    R   t   __repr__ä   s    
c         C   s+   | d  j o |  i } n |  | |  i <d  S(   N(   R   R   R4   R5   t   _fileno(   R4   R   (    (    R   t   add_channelñ   s    c         C   sK   |  i } | d  j o |  i } n | i | ƒ o | | =n d  |  _ d  S(   N(   R4   RJ   R   R   R   R5   t   has_key(   R4   R   R   (    (    R   t   del_channel÷   s    	c         C   sT   | | f |  _ t i | | ƒ |  _ |  i i d ƒ |  i i ƒ  |  _ |  i ƒ  d  S(   Ni    (	   t   familyt   typeR4   t   family_and_typeR8   R9   t   filenoRJ   RK   (   R4   RN   RO   (    (    R   t   create_socket   s
    c         C   s)   | |  _ | i ƒ  |  _ |  i | ƒ d  S(   N(   R6   R4   R8   RQ   RJ   RK   R   (   R4   R6   R   (    (    R   R7     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   (   R4   R8   t
   setsockoptt
   SOL_SOCKETt   SO_REUSEADDRt
   getsockoptR%   (   R4   (    (    R   t   set_reuse_addr  s    c         C   s   t  S(   N(   R:   (   R4   (    (    R   R     s    c         C   s   t  S(   N(   R:   (   R4   (    (    R   R   !  s    c         C   s@   t  |  _ t i d j o | d j o
 d } n |  i i | ƒ S(   Nt   nti   i   (   R:   R4   RD   t   ost   namet   numR8   t   listen(   R4   R[   (    (    R   R\   (  s    	
c         C   s   | |  _  |  i i | ƒ S(   N(   R=   R4   R8   t   bind(   R4   R=   (    (    R   R]   .  s    	c         C   s†   t  |  _ |  i i | ƒ } | t t t	 f j o d  Sn | d t
 f j o  | |  _ t |  _ |  i ƒ  n t i | t | f ‚ d  S(   Ni    (   t   FalseR4   R;   R8   t
   connect_ext   addressR&   t   EINPROGRESSt   EALREADYt   EWOULDBLOCKt   EISCONNR=   R:   t   handle_connectR%   t	   errorcode(   R4   R`   R&   (    (    R   t   connect2  s    			c         C   sZ   y# |  i i ƒ  \ } } | | f SWn0 t i j
 o! } | d t j o qV ‚  n Xd  S(   Ni    (   R4   R8   t   acceptt   connR=   R%   t   whyRc   (   R4   Rj   Ri   R=   (    (    R   Rh   ?  s    c         C   sY   y |  i i | ƒ } | SWn8 t i j
 o) } | d t j o d Sn ‚  d Sn Xd  S(   Ni    (   R4   R8   t   sendt   datat   resultR%   Rj   Rc   (   R4   Rl   Rm   Rj   (    (    R   Rk   J  s    c         C   s   y3 |  i i | ƒ } | p |  i ƒ  d Sn | SWnG t i j
 o8 } | d t t	 t
 g j o |  i ƒ  d Sq} ‚  n Xd  S(   Nt    i    (   R4   R8   t   recvt   buffer_sizeRl   t   handle_closeR%   Rj   t
   ECONNRESETt   ENOTCONNt	   ESHUTDOWN(   R4   Rp   Rl   Rj   (    (    R   Ro   U  s    

c         C   s   |  i ƒ  |  i i ƒ  d  S(   N(   R4   RM   R8   t   close(   R4   (    (    R   Ru   g  s    
c         C   s   t  |  i | ƒ S(   N(   t   getattrR4   R8   t   attr(   R4   Rw   (    (    R   t   __getattr__m  s    c         C   s   t  i i d t | ƒ ƒ d  S(   Ns   log: %s
(   t   syst   stderrR	   t   strt   message(   R4   R|   (    (    R   t   logt  s    t   infoc         C   s+   t  p | d j o d | | f GHn d  S(   NR~   s   %s: %s(   t	   __debug__RO   R|   (   R4   R|   RO   (    (    R   t   log_infow  s    c         C   sh   |  i o% |  i p t |  _ n |  i ƒ  n6 |  i p! |  i ƒ  t |  _ |  i ƒ  n |  i ƒ  d  S(   N(   R4   RD   R;   R:   t   handle_acceptRe   t   handle_read(   R4   (    (    R   R   {  s    



	c         C   s/   |  i p |  i ƒ  t |  _ n |  i ƒ  d  S(   N(   R4   R;   Re   R:   t   handle_write(   R4   (    (    R   R   ‰  s    

c         C   s   |  i ƒ  d  S(   N(   R4   t   handle_expt(   R4   (    (    R   R
     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_tracebackt   nilt   tt   vt   tbinfoRF   R4   t	   self_reprRH   R€   Ru   (   R4   R‰   R†   Rˆ   RŠ   R‡   (    (    R   R   “  s    c         C   s   |  i d d ƒ d  S(   Ns   unhandled exceptiont   warning(   R4   R€   (   R4   (    (    R   R„   §  s    c         C   s   |  i d d ƒ d  S(   Ns   unhandled read eventR‹   (   R4   R€   (   R4   (    (    R   R‚   ª  s    c         C   s   |  i d d ƒ d  S(   Ns   unhandled write eventR‹   (   R4   R€   (   R4   (    (    R   Rƒ   ­  s    c         C   s   |  i d d ƒ d  S(   Ns   unhandled connect eventR‹   (   R4   R€   (   R4   (    (    R   Re   °  s    c         C   s   |  i d d ƒ d  S(   Ns   unhandled accept eventR‹   (   R4   R€   (   R4   (    (    R   R   ³  s    c         C   s   |  i d d ƒ |  i ƒ  d  S(   Ns   unhandled close eventR‹   (   R4   R€   Ru   (   R4   (    (    R   Rq   ¶  s    (&   R   R   R^   t   debugR;   RD   t   closingR   R=   R>   RI   RK   RM   RR   R7   RW   R   R   R\   R]   Rg   Rh   Rk   Ro   Ru   Rx   R}   R€   R   R   R
   R   R„   R‚   Rƒ   Re   R   Rq   (    (    (    R   R3   Ç   sD   																								t   dispatcher_with_sendc           B   s;   t  Z d  d  d „ Z d „  Z d „  Z d „  Z d „  Z RS(   Nc         C   s    t  i |  | | ƒ d |  _ d  S(   NRn   (   R3   R>   R4   R6   R   t
   out_buffer(   R4   R6   R   (    (    R   R>   Á  s    c         C   s3   d } t i |  |  i d  ƒ } |  i | |  _ d  S(   Ni    i   (   t   num_sentR3   Rk   R4   R   (   R4   R   (    (    R   t   initiate_sendÅ  s    c         C   s   |  i ƒ  d  S(   N(   R4   R‘   (   R4   (    (    R   Rƒ   Ê  s    c         C   s   |  i p t |  i ƒ S(   N(   R4   R;   t   lenR   (   R4   (    (    R   R   Í  s    c         C   sC   |  i o |  i d t | ƒ ƒ n |  i | |  _ |  i ƒ  d  S(   Ns
   sending %s(   R4   RŒ   R€   RF   Rl   R   R‘   (   R4   Rl   (    (    R   Rk   Ð  s    
(   R   R   R   R>   R‘   Rƒ   R   Rk   (    (    (    R   RŽ   ¿  s
   			c    
      C   sÁ   t  i ƒ  \ } } }	 g  }  xF |	 o> |  i |	 i i i	 |	 i i i
 t |	 i ƒ f ƒ |	 i }	 q W~	 |  d \ } } } d i g  } |  D] } | d | q‹ ~ ƒ } | | | f | | | f S(   NiÿÿÿÿRA   s
   [%s|%s|%s](   Ry   t   exc_infoR‡   Rˆ   t   tbR‰   R!   t   tb_framet   f_codet   co_filenamet   co_nameR{   t	   tb_linenot   tb_nextt   filet   functiont   lineRG   t   _[1]t   xR~   (
   R‰   Rœ   R›   R~   Rž   R‡   Rˆ   RŸ   R   R”   (    (    R   R…   Ú  s      1.c         C   sI   |  d  j o
 t }  n x! |  i ƒ  D] } | i i ƒ  q$ W|  i ƒ  d  S(   N(   R   R   R   t   valuesRŸ   R8   Ru   t   clear(   R   RŸ   (    (    R   t	   close_allí  s    
 t   posixt   file_wrapperc           B   sA   t  Z d „  Z d „  Z d „  Z e Z e Z d „  Z d „  Z RS(   Nc         C   s   | |  _  d  S(   N(   R   R4   (   R4   R   (    (    R   R>     s    c         G   s   t  i |  i | Œ S(   N(   RY   R   R4   R   t   args(   R4   R¥   (    (    R   Ro     s    c         G   s   t  i |  i | Œ S(   N(   RY   R	   R4   R   R¥   (   R4   R¥   (    (    R   Rk     s    c         C   s   t  i |  i ƒ d  S(   N(   RY   Ru   R4   R   (   R4   (    (    R   Ru     s    c         C   s   |  i S(   N(   R4   R   (   R4   (    (    R   RQ     s    (	   R   R   R>   Ro   Rk   R   R	   Ru   RQ   (    (    (    R   R¤     s   				t   file_dispatcherc           B   s   t  Z d  d „ Z d „  Z RS(   Nc         C   sh   t  i |  d  | ƒ t |  _ |  i | ƒ t	 i	 | t	 i
 d ƒ } | t i B} t	 i	 | t	 i | ƒ d  S(   Ni    (   R3   R>   R4   R   R   R:   R;   t   set_fileR   t   fcntlt   F_GETFLR   RY   t
   O_NONBLOCKt   F_SETFL(   R4   R   R   R   (    (    R   R>     s    	c         C   s&   | |  _ t | ƒ |  _ |  i ƒ  d  S(   N(   R   R4   RJ   R¤   R8   RK   (   R4   R   (    (    R   R§   %  s    	(   R   R   R   R>   R§   (    (    (    R   R¦     s   	('   t   __doc__t
   exceptionsR   R8   Ry   R"   RY   t   errnoRb   Ra   Rc   Rr   Rs   Rt   R'   Rd   Rf   R   t	   NameErrort	   ExceptionR    R   R	   R   R   R   R)   R-   t   poll3R^   R2   R3   RŽ   R…   R¢   RZ   R¨   R¤   R¦   (    R   R¤   RŽ   R    R…   R)   R   Rd   Rf   Rr   R   R-   R±   Ra   Rc   R   R¨   Rs   R¢   R   Ry   R¦   R'   R3   Rt   R8   R	   R"   R­   Rb   RY   R2   (    (    R   t   ?/   s8   						=				+ ø		