Your IP : 172.28.240.42


Current Path : /usr/lib/python2.7/
Upload File :
Current File : //usr/lib/python2.7/asyncore.pyc


|_c@sdZddlZddlZddlZddlZddlZddlZddlmZm	Z	m
Z
mZmZm
Z
mZmZmZmZmZmZmZmZeee
eeeefZyeWnek
riZnXdZdefdYZeeefZdZ dZ!d	Z"d
Z#de$dZ%de$d
Z&e&Z'de(e$e$dZ)dfdYZ*de*fdYZ+dZ,e$e(dZ-ej.dkrddl/Z/dfdYZ0de*fdYZ1ndS(sBasic 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.
iN(tEALREADYtEINPROGRESStEWOULDBLOCKt
ECONNRESETtEINVALtENOTCONNt	ESHUTDOWNtEINTRtEISCONNtEBADFtECONNABORTEDtEPIPEtEAGAINt	errorcodecCsKytj|SWn3tttfk
rF|tkr>t|Sd|SXdS(NsUnknown error %s(toststrerrort
ValueErrort
OverflowErrort	NameErrorR
(terr((s/usr/lib/python2.7/asyncore.pyt	_strerrorDstExitNowcBseZRS((t__name__t
__module__(((s/usr/lib/python2.7/asyncore.pyRLscCs9y|jWn$tk
r$n|jnXdS(N(thandle_read_eventt_reraised_exceptionsthandle_error(tobj((s/usr/lib/python2.7/asyncore.pytreadQs
cCs9y|jWn$tk
r$n|jnXdS(N(thandle_write_eventRR(R((s/usr/lib/python2.7/asyncore.pytwriteYs
cCs9y|jWn$tk
r$n|jnXdS(N(thandle_expt_eventRR(R((s/usr/lib/python2.7/asyncore.pyt
_exceptionas
cCsyz|tj@r|jn|tj@r7|jn|tj@rQ|jn|tjtjBtj	B@ry|j
nWnctjk
r}|j
dtkr|jq|j
n$tk
rn|jnXdS(Ni(tselecttPOLLINRtPOLLOUTRtPOLLPRIRtPOLLHUPtPOLLERRtPOLLNVALthandle_closetsocketterrortargst
_DISCONNECTEDRR(Rtflagste((s/usr/lib/python2.7/asyncore.pyt	readwriteis"








gc
Cs|dkrt}n|rg}g}g}x|jD]v\}}|j}|j}|rt|j|n|r|jr|j|n|s|r:|j|q:q:Wg|ko|ko|knrtj|dSy%t	j	||||\}}}Wn3t	j
k
rF}	|	jdtkr?qGdSnXx9|D]1}|j
|}|dkruqNnt|qNWx9|D]1}|j
|}|dkrqnt|qWx<|D]1}|j
|}|dkrqnt|qWndS(Ni(tNonet
socket_maptitemstreadabletwritabletappendt	acceptingttimetsleepR!R*R+RtgetRRR (
ttimeouttmaptrtwR.tfdRtis_rtis_wR((s/usr/lib/python2.7/asyncore.pytpoll}sN	'
%


cCs}|dkrt}n|dk	r4t|d}ntj}|ryx|jD]\}}d}|jr|tjtjBO}n|j	r|j
r|tjO}n|rS|tjtj
BtjBO}|j||qSqSWy|j|}Wn5tjk
r0}|jdtkr'ng}nXxE|D]:\}}|j|}|dkreq8nt||q8WndS(Nii(R0R1tintR!RAR2R3R"R$R4R6R#R&R%R'tregisterR*R+RR9R/(R:R;tpollsterR>RR-R<R((s/usr/lib/python2.7/asyncore.pytpoll2s4	
g>@cCs|dkrt}n|r3ttdr3t}nt}|dkrbxJ|r^|||qHWn0x-|r|dkr||||d}qeWdS(NRAii(R0R1thasattrR!RERA(R:tuse_pollR;tcounttpoll_fun((s/usr/lib/python2.7/asyncore.pytloops			
t
dispatchercBs[eZeZeZeZeZd Ze	dgZ
d d dZdZeZ
d dZd dZdZd dZdZdZd	Zd
ZdZdZd
ZdZdZdZdZdZddZdZdZ dZ!dZ"dZ#dZ$dZ%dZ&dZ'dZ(dZ)RS(!twarningcCs|dkrt|_n	||_d|_|r|jd|j||t|_y|j|_	Wqt
jk
r}|jdt
krt|_q|j|qXn	d|_
dS(Ni(R0R1t_mapt_filenotsetblockingt
set_sockettTruet	connectedtgetpeernametaddrR)R*R+RtFalsetdel_channel(tselftsockR;R((s/usr/lib/python2.7/asyncore.pyt__init__s 		
	

cCs|jjd|jjg}|jr?|jr?|jdn|jrX|jdn|jdk	ry|jd|jWqtk
r|jt	|jqXnddj
|t|fS(Nt.t	listeningRRs%s:%ds<%s at %#x>t (t	__class__RRR6RTR5RRR0t	TypeErrortreprtjointid(RWtstatus((s/usr/lib/python2.7/asyncore.pyt__repr__s	
cCs)|dkr|j}n|||j<dS(N(R0RMRN(RWR;((s/usr/lib/python2.7/asyncore.pytadd_channelscCsD|j}|dkr!|j}n||kr7||=nd|_dS(N(RNR0RM(RWR;R>((s/usr/lib/python2.7/asyncore.pyRVs	
cCs?||f|_tj||}|jd|j|dS(Ni(tfamily_and_typeR)RORP(RWtfamilyttypeRX((s/usr/lib/python2.7/asyncore.pyt
create_socket&s
cCs)||_|j|_|j|dS(N(R)tfilenoRNRd(RWRXR;((s/usr/lib/python2.7/asyncore.pyRP,s	cCsTy9|jjtjtj|jjtjtjdBWntjk
rOnXdS(Ni(R)t
setsockoptt
SOL_SOCKETtSO_REUSEADDRt
getsockoptR*(RW((s/usr/lib/python2.7/asyncore.pytset_reuse_addr2s	cCstS(N(RQ(RW((s/usr/lib/python2.7/asyncore.pyR3CscCstS(N(RQ(RW((s/usr/lib/python2.7/asyncore.pyR4FscCs=t|_tjdkr-|dkr-d}n|jj|S(Ntnti(RQR6RtnameR)tlisten(RWtnum((s/usr/lib/python2.7/asyncore.pyRqMs		cCs||_|jj|S(N(RTR)tbind(RWRT((s/usr/lib/python2.7/asyncore.pyRsSs	cCst|_|jj|}|tttfksK|tkrOtj	dkrOdS|dt
fkrw||_|jntj
|t|dS(NRotcei(sntRt(RURRR)t
connect_exRRRRRRpRRTthandle_connect_eventR*R
(RWtaddressR((s/usr/lib/python2.7/asyncore.pytconnectWs		
cCsty|jj\}}WnJtk
r-dStjk
re}|jdtttfkr_dSnX||fSdS(Ni(	R)tacceptR^R0R*R+RR
R(RWtconnRTtwhy((s/usr/lib/python2.7/asyncore.pyRycs
cCsry|jj|}|SWnQtjk
rm}|jdtkrFdS|jdtkrg|jdSnXdS(Ni(R)tsendR*R+RR,R((RWtdatatresultR{((s/usr/lib/python2.7/asyncore.pyR|qs
cCsoy.|jj|}|s)|jdS|SWn:tjk
rj}|jdtkrd|jdSnXdS(Nti(R)trecvR(R*R+R,(RWtbuffer_sizeR}R{((s/usr/lib/python2.7/asyncore.pyR~s

cCsit|_t|_|jy|jjWn5tjk
rd}|jdtt	fkreqenXdS(Ni(
RURRR6RVR)tcloseR*R+RR	(RWR{((s/usr/lib/python2.7/asyncore.pyRs		
cCsyt|j|}Wn-tk
rEtd|jj|fn9Xdi|jjd6|d6}tj|tdd|SdS(Ns!%s instance has no attribute '%s'sB%(me)s.%(attr)s is deprecated. Use %(me)s.socket.%(attr)s instead.tmetattrt
stackleveli(tgetattrR)tAttributeErrorR]RtwarningstwarntDeprecationWarning(RWRtretattrtmsg((s/usr/lib/python2.7/asyncore.pyt__getattr__s
cCstjjdt|dS(Nslog: %s
(tsyststderrRtstr(RWtmessage((s/usr/lib/python2.7/asyncore.pytlogstinfocCs%||jkr!d||fGHndS(Ns%s: %s(tignore_log_types(RWRRg((s/usr/lib/python2.7/asyncore.pytlog_infoscCsD|jr|jn*|js6|j|jn
|jdS(N(R6t
handle_acceptRRRvthandle_read(RW((s/usr/lib/python2.7/asyncore.pyRs	
	

cCsY|jjtjtj}|dkrBtj|t|n|jt|_dS(Ni(	R)RmRktSO_ERRORR*Rthandle_connectRQRR(RWR((s/usr/lib/python2.7/asyncore.pyRvs

cCss|jr
dS|jse|jjtjtj}|dkrXtj|t|n|jn|j	dS(Ni(
R6RRR)RmRkRR*RRvthandle_write(RWR((s/usr/lib/python2.7/asyncore.pyRs		
cCsB|jjtjtj}|dkr4|jn
|jdS(Ni(R)RmRkRR(thandle_expt(RWR((s/usr/lib/python2.7/asyncore.pyRs
cCsmt\}}}}yt|}Wndt|}nX|jd||||fd|jdS(Ns)<__repr__(self) failed for object at %0x>s:uncaptured python exception, closing channel %s (%s:%s %s)R*(tcompact_tracebackR_RaRR((RWtniltttvttbinfot	self_repr((s/usr/lib/python2.7/asyncore.pyRscCs|jdddS(Ns!unhandled incoming priority eventRL(R(RW((s/usr/lib/python2.7/asyncore.pyRscCs|jdddS(Nsunhandled read eventRL(R(RW((s/usr/lib/python2.7/asyncore.pyRscCs|jdddS(Nsunhandled write eventRL(R(RW((s/usr/lib/python2.7/asyncore.pyRscCs|jdddS(Nsunhandled connect eventRL(R(RW((s/usr/lib/python2.7/asyncore.pyRscCs|jdddS(Nsunhandled accept eventRL(R(RW((s/usr/lib/python2.7/asyncore.pyRscCs|jdd|jdS(Nsunhandled close eventRL(RR(RW((s/usr/lib/python2.7/asyncore.pyR(sN(*RRRUtdebugRRR6tclosingR0RTt	frozensetRRYRct__str__RdRVRhRPRnR3R4RqRsRxRyR|RRRRRRRvRRRRRRRRR((((s/usr/lib/python2.7/asyncore.pyRKsJ 	
										
														tdispatcher_with_sendcBs;eZdddZdZdZdZdZRS(cCs tj|||d|_dS(NR(RKRYt
out_buffer(RWRXR;((s/usr/lib/python2.7/asyncore.pyRYscCs3d}tj||jd }|j||_dS(Nii(RKR|R(RWtnum_sent((s/usr/lib/python2.7/asyncore.pyt
initiate_sendscCs|jdS(N(R(RW((s/usr/lib/python2.7/asyncore.pyRscCs|jpt|jS(N(RRtlenR(RW((s/usr/lib/python2.7/asyncore.pyR4scCsA|jr#|jdt|n|j||_|jdS(Ns
sending %s(RRR_RR(RWR}((s/usr/lib/python2.7/asyncore.pyR| s	N(RRR0RYRRR4R|(((s/usr/lib/python2.7/asyncore.pyRs
			c	Cstj\}}}g}|s0tdnxD|rv|j|jjj|jjjt|j	f|j
}q3W~|d\}}}djg|D]}d|^q}|||f|||fS(Nstraceback does not existiR\s
[%s|%s|%s](Rtexc_infotAssertionErrorR5ttb_frametf_codetco_filenametco_nameRt	tb_linenottb_nextR`(	RRttbRtfiletfunctiontlinetxR((s/usr/lib/python2.7/asyncore.pyR*s	
&cCs|dkrt}nx|jD]q}y|jWq"tk
rm}|jdtkr^q|sqq"tk
rq"|sqq"Xq"W|jdS(Ni(	R0R1tvaluesRtOSErrorR+R	Rtclear(R;t
ignore_allR((s/usr/lib/python2.7/asyncore.pyt	close_all>s 		
tposixtfile_wrappercBsMeZdZdZdZddZeZeZdZ	dZ
RS(cCstj||_dS(N(RtdupR>(RWR>((s/usr/lib/python2.7/asyncore.pyRYescGstj|j|S(N(RRR>(RWR+((s/usr/lib/python2.7/asyncore.pyRhscGstj|j|S(N(RRR>(RWR+((s/usr/lib/python2.7/asyncore.pyR|kscCs9|tjkr)|tjkr)|r)dStddS(Nis-Only asyncore specific behaviour implemented.(R)RkRtNotImplementedError(RWtleveltoptnametbuflen((s/usr/lib/python2.7/asyncore.pyRmns
cCstj|jdS(N(RRR>(RW((s/usr/lib/python2.7/asyncore.pyRyscCs|jS(N(R>(RW((s/usr/lib/python2.7/asyncore.pyRi|sN(RRRYRR|R0RmRRRRi(((s/usr/lib/python2.7/asyncore.pyR`s				tfile_dispatchercBseZddZdZRS(cCstj|d|t|_y|j}Wntk
r?nX|j|tj|tj	d}|t
jB}tj|tj|dS(Ni(
RKRYR0RQRRRiRtset_filetfcntltF_GETFLRt
O_NONBLOCKtF_SETFL(RWR>R;R-((s/usr/lib/python2.7/asyncore.pyRYs	


cCs/t||_|jj|_|jdS(N(RR)RiRNRd(RWR>((s/usr/lib/python2.7/asyncore.pyRsN(RRR0RYR(((s/usr/lib/python2.7/asyncore.pyRs
(2t__doc__R!R)RR7RRterrnoRRRRRRRRRR	R
RRR
RR,R1RRt	ExceptionRtKeyboardInterruptt
SystemExitRRRR R/R0RAREtpoll3RURJRKRRRRpRRR(((s/usr/lib/python2.7/asyncore.pyt<module>/sB^

					-!1