;
Éâ"Ic               @   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 m Z m Z m	 Z	 m
 Z
 m Z m Z m Z m Z m Z m Z m Z y e Wn e k
 o i  Z Yn Xd „  Z Gd „  d e ƒ 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  Gd „  d ƒ Z! Gd „  d e! ƒ Z" d „  Z# e e d „ Z$ e j% d k o3 d d l& Z& Gd „  d ƒ Z' Gd „  d e! ƒ Z( n d S(   u   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(   u   EALREADYu   EINPROGRESSu   EWOULDBLOCKu
   ECONNRESETu   ENOTCONNu	   ESHUTDOWNu   EINTRu   EISCONNu   EBADFu   ECONNABORTEDu	   errorcodec             C   s.   t  j |  ƒ } | d k o t |  } n | S(   Nu   Unknown error(   u   osu   strerroru	   errorcode(   u   erru   res(    (    u%   /mit/python/lib/python3.0/asyncore.pyu	   _strerror>   s    c             B   s   |  Ee  Z d  S(   N(   u   __name__u
   __module__(   u
   __locals__(    (    u%   /mit/python/lib/python3.0/asyncore.pyu   ExitNowD   s   
u   ExitNowc          
   C   sF   y |  j  ƒ  Wn1 t t t f k
 o ‚  Yn |  j ƒ  Yn Xd  S(   N(   u   handle_read_eventu   ExitNowu   KeyboardInterruptu
   SystemExitu   handle_error(   u   obj(    (    u%   /mit/python/lib/python3.0/asyncore.pyu   readG   s    c          
   C   sF   y |  j  ƒ  Wn1 t t t f k
 o ‚  Yn |  j ƒ  Yn Xd  S(   N(   u   handle_write_eventu   ExitNowu   KeyboardInterruptu
   SystemExitu   handle_error(   u   obj(    (    u%   /mit/python/lib/python3.0/asyncore.pyu   writeO   s    c          
   C   sF   y |  j  ƒ  Wn1 t t t f k
 o ‚  Yn |  j ƒ  Yn Xd  S(   N(   u   handle_expt_eventu   ExitNowu   KeyboardInterruptu
   SystemExitu   handle_error(   u   obj(    (    u%   /mit/python/lib/python3.0/asyncore.pyu
   _exceptionW   s    c          
   C   sº   y‚ | t  j t  j B@o |  j ƒ  n | t  j @o |  j ƒ  n | t  j t  j B@o |  j ƒ  n | t  j	 @o |  j
 ƒ  n Wn1 t t t f k
 o ‚  Yn |  j ƒ  Yn Xd  S(   N(   u   selectu   POLLINu   POLLPRIu   handle_read_eventu   POLLOUTu   handle_write_eventu   POLLERRu   POLLNVALu   handle_expt_eventu   POLLHUPu   handle_closeu   ExitNowu   KeyboardInterruptu
   SystemExitu   handle_error(   u   obju   flags(    (    u%   /mit/python/lib/python3.0/asyncore.pyu	   readwrite_   s    g        c       
      C   s)  | d  k o
 t } n | og  } g  } g  } x‡ t | j ƒ  ƒ D]s \ } } | j ƒ  } | j ƒ  } | o | j | ƒ n | o | j | ƒ n | p | o | j | ƒ qC qC Wg  | k o | k o
 | k n o t j |  ƒ d  Sy% t	 j	 | | | |  ƒ \ } } } WnI t	 j
 k
 o: }	 z$ |	 j d t k o ‚  n d  SWYd  d  }	 ~	 Xn Xx; | D]3 } | j | ƒ } | d  k o qnn t | ƒ qnWx; | D]3 } | j | ƒ } | d  k o q¬n t | ƒ q¬Wx? | D]3 } | j | ƒ } | d  k o qên t | ƒ qêWn d  S(   Ni    (   u   Noneu
   socket_mapu   listu   itemsu   readableu   writableu   appendu   timeu   sleepu   selectu   erroru   argsu   EINTRu   getu   readu   writeu
   _exception(
   u   timeoutu   mapu   ru   wu   eu   fdu   obju   is_ru   is_wu   err(    (    u%   /mit/python/lib/python3.0/asyncore.pyu   polln   sV    
   *%   c             C   s  | d  k o
 t } n |  d  k	 o t |  d ƒ }  n t j ƒ  } | oNxŸ t | j ƒ  ƒ D]‹ \ } } d } | j ƒ  o | t j t j	 BO} n | j
 ƒ  o | t j O} n | o/ | t j t j Bt j BO} | j | | ƒ q^ q^ Wy | j |  ƒ } WnK t j k
 o< } z& | j d t k o ‚  n g  } WYd  d  } ~ Xn XxH | D]< \ } } | j | ƒ } | d  k o qUn t | | ƒ qUWn d  S(   Niè  i    (   u   Noneu
   socket_mapu   intu   selectu   pollu   listu   itemsu   readableu   POLLINu   POLLPRIu   writableu   POLLOUTu   POLLERRu   POLLHUPu   POLLNVALu   registeru   erroru   argsu   EINTRu   getu	   readwrite(   u   timeoutu   mapu   pollsteru   fdu   obju   flagsu   ru   err(    (    u%   /mit/python/lib/python3.0/asyncore.pyu   poll2š   s8    
  g      >@c             C   s¢   | d  k o
 t } n | o t t d ƒ o
 t } n t } | d  k o  xP | o | |  | ƒ qN Wn4 x0 | o( | d k o | |  | ƒ | d } qn Wd  S(   Nu   polli    i   (   u   Noneu
   socket_mapu   hasattru   selectu   poll2u   poll(   u   timeoutu   use_pollu   mapu   countu   poll_fun(    (    u%   /mit/python/lib/python3.0/asyncore.pyu   loop¼   s    

  c             B   sL  |  Ee  Z d  Z d  Z d  Z d  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% d „  Z& d S(!   c             C   sÛ   | d  k o t |  _ n
 | |  _ d  |  _ | o› | j d ƒ |  j | | ƒ d |  _ y | j ƒ  |  _	 Wq× t
 j k
 oL } z6 | j d t k o d |  _ n |  j | ƒ ‚  WYd  d  } ~ Xq× Xn
 d  |  _
 d  S(   Ni    TF(   u   Noneu
   socket_mapu   _mapu   _filenou   setblockingu
   set_socketu   Trueu	   connectedu   getpeernameu   addru   socketu   erroru   argsu   ENOTCONNu   Falseu   del_channel(   u   selfu   socku   mapu   err(    (    u%   /mit/python/lib/python3.0/asyncore.pyu   __init__Ö   s     			c          
   C   sÓ   |  j  j d |  j  j g } |  j o |  j o | j d ƒ n |  j o | j d ƒ n |  j d  k	 oI y | j d |  j ƒ Wq¶ t k
 o | j t	 |  j ƒ ƒ Yq¶ Xn d d j
 | ƒ t |  ƒ f S(   Nu   .u	   listeningu	   connectedu   %s:%du   <%s at %#x>u    (   u	   __class__u
   __module__u   __name__u	   acceptingu   addru   appendu	   connectedu   Noneu	   TypeErroru   repru   joinu   id(   u   selfu   status(    (    u%   /mit/python/lib/python3.0/asyncore.pyu   __repr__ö   s    
 c             C   s+   | d  k o |  j } n |  | |  j <d  S(   N(   u   Noneu   _mapu   _fileno(   u   selfu   map(    (    u%   /mit/python/lib/python3.0/asyncore.pyu   add_channel  s    c             C   sH   |  j  } | d  k o |  j } n | | k o | | =n d  |  _  d  S(   N(   u   _filenou   Noneu   _map(   u   selfu   mapu   fd(    (    u%   /mit/python/lib/python3.0/asyncore.pyu   del_channel	  s    	c             C   s?   | | f |  _  t j | | ƒ } | j d ƒ |  j | ƒ d  S(   Ni    (   u   family_and_typeu   socketu   setblockingu
   set_socket(   u   selfu   familyu   typeu   sock(    (    u%   /mit/python/lib/python3.0/asyncore.pyu   create_socket  s    c             C   s)   | |  _  | j ƒ  |  _ |  j | ƒ d  S(   N(   u   socketu   filenou   _filenou   add_channel(   u   selfu   socku   map(    (    u%   /mit/python/lib/python3.0/asyncore.pyu
   set_socket  s    	c             C   sW   y9 |  j  j t  j t  j |  j  j t  j t  j ƒ d Bƒ Wn t  j k
 o Yn Xd  S(   Ni   (   u   socketu
   setsockoptu
   SOL_SOCKETu   SO_REUSEADDRu
   getsockoptu   error(   u   self(    (    u%   /mit/python/lib/python3.0/asyncore.pyu   set_reuse_addr  s    	c             C   s   d S(   NT(   u   True(   u   self(    (    u%   /mit/python/lib/python3.0/asyncore.pyu   readable/  s    c             C   s   d S(   NT(   u   True(   u   self(    (    u%   /mit/python/lib/python3.0/asyncore.pyu   writable2  s    c             C   s@   d |  _ t j d k o | d k o
 d } n |  j j | ƒ S(   Nu   nti   T(   u   Trueu	   acceptingu   osu   nameu   socketu   listen(   u   selfu   num(    (    u%   /mit/python/lib/python3.0/asyncore.pyu   listen9  s    	
c             C   s   | |  _  |  j j | ƒ S(   N(   u   addru   socketu   bind(   u   selfu   addr(    (    u%   /mit/python/lib/python3.0/asyncore.pyu   bind?  s    	c             C   sz   d |  _ |  j j | ƒ } | t t t f k o d  S| d t f k o | |  _ |  j	 ƒ  n t j
 | t | ƒ ‚ d  S(   Ni    F(   u   Falseu	   connectedu   socketu
   connect_exu   EINPROGRESSu   EALREADYu   EWOULDBLOCKu   EISCONNu   addru   handle_connect_eventu   erroru	   errorcode(   u   selfu   addressu   err(    (    u%   /mit/python/lib/python3.0/asyncore.pyu   connectC  s    		c             C   so   y# |  j  j ƒ  \ } } | | f SWnE t  j k
 o6 } z  | j d t k o n ‚  WYd  d  } ~ Xn Xd  S(   Ni    (   u   socketu   acceptu   erroru   argsu   EWOULDBLOCK(   u   selfu   connu   addru   why(    (    u%   /mit/python/lib/python3.0/asyncore.pyu   acceptO  s    c             C   s–   y |  j  j | ƒ } | SWnu t  j k
 of } zP | j d t k o d S| j d t t t t f k o |  j	 ƒ  d S‚  WYd  d  } ~ Xn Xd  S(   Ni    (
   u   socketu   sendu   erroru   argsu   EWOULDBLOCKu
   ECONNRESETu   ENOTCONNu	   ESHUTDOWNu   ECONNABORTEDu   handle_close(   u   selfu   datau   resultu   why(    (    u%   /mit/python/lib/python3.0/asyncore.pyu   sendZ  s     
c             C   s“   y0 |  j  j | ƒ } | p |  j ƒ  d S| SWn\ t  j k
 oM } z7 | j d t t t t g k o |  j ƒ  d S‚  WYd  d  } ~ Xn Xd  S(   Ns    i    (	   u   socketu   recvu   handle_closeu   erroru   argsu
   ECONNRESETu   ENOTCONNu	   ESHUTDOWNu   ECONNABORTED(   u   selfu   buffer_sizeu   datau   why(    (    u%   /mit/python/lib/python3.0/asyncore.pyu   recvg  s    
 
c             C   s   d |  _ d |  _ |  j ƒ  y |  j j ƒ  WnK t j k
 o< } z& | j d t t	 f k o ‚  n WYd  d  } ~ Xn Xd  S(   Ni    F(
   u   Falseu	   connectedu	   acceptingu   del_channelu   socketu   closeu   erroru   argsu   ENOTCONNu   EBADF(   u   selfu   why(    (    u%   /mit/python/lib/python3.0/asyncore.pyu   closey  s    		
c             C   s   t  |  j | ƒ S(   N(   u   getattru   socket(   u   selfu   attr(    (    u%   /mit/python/lib/python3.0/asyncore.pyu   __getattr__…  s    c             C   s   t  j j d t | ƒ ƒ d  S(   Nu   log: %s
(   u   sysu   stderru   writeu   str(   u   selfu   message(    (    u%   /mit/python/lib/python3.0/asyncore.pyu   logŒ  s    u   infoc             C   s0   t  p | d k o t d | | f ƒ n d  S(   Nu   infou   %s: %s(   u	   __debug__u   print(   u   selfu   messageu   type(    (    u%   /mit/python/lib/python3.0/asyncore.pyu   log_info  s    c             C   sH   |  j  o |  j ƒ  n- |  j p |  j ƒ  |  j ƒ  n |  j ƒ  d  S(   N(   u	   acceptingu   handle_acceptu	   connectedu   handle_connect_eventu   handle_read(   u   self(    (    u%   /mit/python/lib/python3.0/asyncore.pyu   handle_read_event“  s    


c             C   s   d |  _ |  j ƒ  d  S(   NT(   u   Trueu	   connectedu   handle_connect(   u   self(    (    u%   /mit/python/lib/python3.0/asyncore.pyu   handle_connect_eventž  s    	c             C   sy   |  j  o d  S|  j pR |  j j t j t j ƒ } | d k o t j | t | ƒ ƒ ‚ n |  j ƒ  n |  j	 ƒ  d  S(   Ni    (
   u	   acceptingu	   connectedu   socketu
   getsockoptu
   SOL_SOCKETu   SO_ERRORu   erroru	   _strerroru   handle_connect_eventu   handle_write(   u   selfu   err(    (    u%   /mit/python/lib/python3.0/asyncore.pyu   handle_write_event¢  s    

c          	   C   s”   d } y% |  j j } t j } | | k } Wn t k
 o Yn X| o= |  j j t j t j ƒ } t	 | ƒ } t j
 | | ƒ ‚ n |  j ƒ  d  S(   NT(   u   Trueu   handle_exptu   __func__u
   dispatcheru   AttributeErroru   socketu
   getsockoptu
   SOL_SOCKETu   SO_ERRORu	   _strerroru   error(   u   selfu   xu   y1u   y2u   erru   msg(    (    u%   /mit/python/lib/python3.0/asyncore.pyu   handle_expt_event±  s    	c             C   sn   t  ƒ  \ } } } } y t |  ƒ } Wn d t |  ƒ } Yn X|  j d | | | | f d ƒ |  j ƒ  d  S(   Nu)   <__repr__(self) failed for object at %0x>u:   uncaptured python exception, closing channel %s (%s:%s %s)u   error(   u   compact_tracebacku   repru   idu   log_infou   handle_close(   u   selfu   nilu   tu   vu   tbinfou	   self_repr(    (    u%   /mit/python/lib/python3.0/asyncore.pyu   handle_errorÅ  s    c             C   s   |  j  d d ƒ d  S(   Nu   unhandled exceptionu   warning(   u   log_info(   u   self(    (    u%   /mit/python/lib/python3.0/asyncore.pyu   handle_exptÙ  s    c             C   s   |  j  d d ƒ d  S(   Nu   unhandled read eventu   warning(   u   log_info(   u   self(    (    u%   /mit/python/lib/python3.0/asyncore.pyu   handle_readÜ  s    c             C   s   |  j  d d ƒ d  S(   Nu   unhandled write eventu   warning(   u   log_info(   u   self(    (    u%   /mit/python/lib/python3.0/asyncore.pyu   handle_writeß  s    c             C   s   |  j  d d ƒ d  S(   Nu   unhandled connect eventu   warning(   u   log_info(   u   self(    (    u%   /mit/python/lib/python3.0/asyncore.pyu   handle_connectâ  s    c             C   s   |  j  d d ƒ d  S(   Nu   unhandled accept eventu   warning(   u   log_info(   u   self(    (    u%   /mit/python/lib/python3.0/asyncore.pyu   handle_acceptå  s    c             C   s   |  j  d d ƒ |  j ƒ  d  S(   Nu   unhandled close eventu   warning(   u   log_infou   close(   u   self(    (    u%   /mit/python/lib/python3.0/asyncore.pyu   handle_closeè  s    NF('   u   __name__u
   __module__u   Falseu   debugu	   connectedu	   acceptingu   closingu   Noneu   addru   __init__u   __repr__u   add_channelu   del_channelu   create_socketu
   set_socketu   set_reuse_addru   readableu   writableu   listenu   bindu   connectu   acceptu   sendu   recvu   closeu   __getattr__u   logu   log_infou   handle_read_eventu   handle_connect_eventu   handle_write_eventu   handle_expt_eventu   handle_erroru   handle_exptu   handle_readu   handle_writeu   handle_connectu   handle_acceptu   handle_close(   u
   __locals__(    (    u%   /mit/python/lib/python3.0/asyncore.pyu
   dispatcherÎ   sF   
 																									u
   dispatcherc             B   sA   |  Ee  Z d d d  „ Z d „  Z d „  Z d „  Z d „  Z d S(   c             C   s    t  j |  | | ƒ d |  _ d  S(   Ns    (   u
   dispatcheru   __init__u
   out_buffer(   u   selfu   socku   map(    (    u%   /mit/python/lib/python3.0/asyncore.pyu   __init__ó  s    c             C   s?   d } t  j |  |  j d  d … ƒ } |  j | d  … |  _ d  S(   Ni    i   (   u
   dispatcheru   sendu
   out_buffer(   u   selfu   num_sent(    (    u%   /mit/python/lib/python3.0/asyncore.pyu   initiate_send÷  s    c             C   s   |  j  ƒ  d  S(   N(   u   initiate_send(   u   self(    (    u%   /mit/python/lib/python3.0/asyncore.pyu   handle_writeü  s    c             C   s   |  j  p t |  j ƒ S(   N(   u	   connectedu   lenu
   out_buffer(   u   self(    (    u%   /mit/python/lib/python3.0/asyncore.pyu   writableÿ  s    c             C   sC   |  j  o |  j d t | ƒ ƒ n |  j | |  _ |  j ƒ  d  S(   Nu
   sending %s(   u   debugu   log_infou   repru
   out_bufferu   initiate_send(   u   selfu   data(    (    u%   /mit/python/lib/python3.0/asyncore.pyu   send  s    
N(   u   __name__u
   __module__u   Noneu   __init__u   initiate_sendu   handle_writeu   writableu   send(   u
   __locals__(    (    u%   /mit/python/lib/python3.0/asyncore.pyu   dispatcher_with_sendñ  s
   
			u   dispatcher_with_sendc              C   sÃ   t  j ƒ  \ }  } } g  } | p t d ƒ ‚ n xF | o> | j | j j j | j j j t | j	 ƒ f ƒ | j
 } q5 W~ | d \ } } } d j d „  | Dƒ ƒ } | | | f |  | | f S(   Nu   traceback does not existiÿÿÿÿu    c             S   s   g  } |  ] } | d  | q
 S(   u
   [%s|%s|%s](    (   u   .0u   _[1]u   x(    (    u%   /mit/python/lib/python3.0/asyncore.pyu
   <listcomp>  s    (   u   sysu   exc_infou   AssertionErroru   appendu   tb_frameu   f_codeu   co_filenameu   co_nameu   stru	   tb_linenou   tb_nextu   join(   u   tu   vu   tbu   tbinfou   fileu   functionu   lineu   info(    (    u%   /mit/python/lib/python3.0/asyncore.pyu   compact_traceback  s     c             C   sÔ   |  d  k o
 t }  n x¬ t |  j ƒ  ƒ D]˜ } y | j ƒ  Wq* t k
 oA } z+ | j d t k o n | p ‚  n WYd  d  } ~ Xq* t t	 t
 f k
 o ‚  Yq* | p ‚  n Yq* Xq* W|  j ƒ  d  S(   Ni    (   u   Noneu
   socket_mapu   listu   valuesu   closeu   OSErroru   argsu   EBADFu   ExitNowu   KeyboardInterruptu
   SystemExitu   clear(   u   mapu
   ignore_allu   x(    (    u%   /mit/python/lib/python3.0/asyncore.pyu	   close_all   s"    
 u   posixc             B   sG   |  Ee  Z d  „  Z d „  Z d „  Z e Z e Z d „  Z d „  Z d S(   c             C   s   t  j | ƒ |  _ d  S(   N(   u   osu   dupu   fd(   u   selfu   fd(    (    u%   /mit/python/lib/python3.0/asyncore.pyu   __init__G  s    c             G   s   t  j |  j | Œ S(   N(   u   osu   readu   fd(   u   selfu   args(    (    u%   /mit/python/lib/python3.0/asyncore.pyu   recvJ  s    c             G   s   t  j |  j | Œ S(   N(   u   osu   writeu   fd(   u   selfu   args(    (    u%   /mit/python/lib/python3.0/asyncore.pyu   sendM  s    c             C   s   t  j |  j ƒ d  S(   N(   u   osu   closeu   fd(   u   self(    (    u%   /mit/python/lib/python3.0/asyncore.pyu   closeS  s    c             C   s   |  j  S(   N(   u   fd(   u   self(    (    u%   /mit/python/lib/python3.0/asyncore.pyu   filenoV  s    N(	   u   __name__u
   __module__u   __init__u   recvu   sendu   readu   writeu   closeu   fileno(   u
   __locals__(    (    u%   /mit/python/lib/python3.0/asyncore.pyu   file_wrapperB  s   
				u   file_wrapperc             B   s#   |  Ee  Z d d  „ Z d „  Z d S(   c          
   C   s   t  j |  d  | ƒ d |  _ y | j ƒ  } Wn t k
 o Yn X|  j | ƒ t j | t j	 d ƒ } | t
 j B} t j | t j | ƒ d  S(   Ni    T(   u
   dispatcheru   __init__u   Noneu   Trueu	   connectedu   filenou   AttributeErroru   set_fileu   fcntlu   F_GETFLu   osu
   O_NONBLOCKu   F_SETFL(   u   selfu   fdu   mapu   flags(    (    u%   /mit/python/lib/python3.0/asyncore.pyu   __init__[  s    	c             C   s&   | |  _  t | ƒ |  _ |  j ƒ  d  S(   N(   u   _filenou   file_wrapperu   socketu   add_channel(   u   selfu   fd(    (    u%   /mit/python/lib/python3.0/asyncore.pyu   set_fileh  s    	N(   u   __name__u
   __module__u   Noneu   __init__u   set_file(   u
   __locals__(    (    u%   /mit/python/lib/python3.0/asyncore.pyu   file_dispatcherY  s   
u   file_dispatcher()   u   __doc__u   selectu   socketu   sysu   timeu   osu   errnou   EALREADYu   EINPROGRESSu   EWOULDBLOCKu
   ECONNRESETu   ENOTCONNu	   ESHUTDOWNu   EINTRu   EISCONNu   EBADFu   ECONNABORTEDu	   errorcodeu
   socket_mapu	   NameErroru	   _strerroru	   Exceptionu   ExitNowu   readu   writeu
   _exceptionu	   readwriteu   Noneu   pollu   poll2u   poll3u   Falseu   loopu
   dispatcheru   dispatcher_with_sendu   compact_tracebacku	   close_allu   nameu   fcntlu   file_wrapperu   file_dispatcher(    (    (    u%   /mit/python/lib/python3.0/asyncore.pyu   <module>/   s:   L					, ÿ $	