RhOdZddlZddlZddlZddlZddlZddlZddlmZm Z m Z m Z m Z m Z mZmZmZmZmZmZmZee e eeeehZ edZGddeZeeefZdZdZ d Z!d Z"dd Z#dd Z$e$Z%dd Z&GddZ'Gdde'Z(dZ)ddZ*ejVdk(rGddZ,Gdde'Z-yy#e$riZYxwxYw)aBasic 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) EALREADY EINPROGRESS EWOULDBLOCK ECONNRESETEINVALENOTCONN ESHUTDOWNEISCONNEBADF ECONNABORTEDEPIPEEAGAIN errorcodec tj|S#tttf$r|t vr t |cYSd|zcYSwxYw)NzUnknown error %s)osstrerror ValueError OverflowError NameErrorr)errs ,/usr/lib/python3.12/test/support/asyncore.py _strerrorrJsI'{{3  y 1' ) S> !!3&&'s%AAAc eZdZy)ExitNowN)__name__ __module__ __qualname__rrrRsrrcf |jy#t$r|jYyxYwN)handle_read_event_reraised_exceptions handle_errorobjs rreadr'W1    0cf |jy#t$r|jYyxYwr!)handle_write_eventr#r$r%s rwriter,_s1    r)cf |jy#t$r|jYyxYwr!)handle_expt_eventr#r$r%s r _exceptionr/gr(r)c6 |tjzr|j|tjzr|j |tj zr|j |tjtjztjzzr|jyy#t$rB}|jtvr|jn|jYd}~yYd}~yd}~wt$r|jYyxYwr!)selectPOLLINr"POLLOUTr+POLLPRIr.POLLHUPPOLLERRPOLLNVAL handle_closeOSErrorerrno _DISCONNECTEDr$r#)r&flagses r readwriter>os 6==  ! ! # 6>> !  " " $ 6>> !  ! ! # FNNV^^3fooE F     G  77- '             sB.B22 D;3C88Dc|t}|rHg}g}g}t|jD]m\}}|j}|j }|r|j ||r|j s|j ||s|s]|j |og|cxk(r |cxk(r|k(rnntj|ytj||||\}}}|D]!}|j|}|t|#|D]!}|j|}|t|#|D]!}|j|}|t|#yyr!) socket_maplistitemsreadablewritableappend acceptingtimesleepr1getr'r,r/) timeoutmaprwr=fdr&is_ris_ws rpollrQsI { BBCIIK( GB<<>D<<>D CMM t    a 1  JJw  --1a11a B''"+C{ I    B''"+C{ #J    B''"+C{ sO  = rc|t}|t|dz}tj}|rt |j D]\}}d}|j r$|tjtjzz}|jr|js|tjz}|sn|j|||j|}|D]%\}}|j|}|t||'yy)Nir)r@intr1rQrArBrCr2r4rDrFr3registerrIr>)rJrKpollsterrNr&r<rLs rpoll2rVs {gdl#{{}H CIIK( -GBE||~77||~cmm'!!"e, - MM' " "IB''"+C{ c5 !  " rc|t}|rttdrt}nt}||r ||||r yy|r|dkDr||||dz }|r|dkDryyyy)NrQr)r@hasattrr1rVrQ)rJuse_pollrKcountpoll_funs rloopr]sh {GFF+ } Wc "eai Wc "AIEeaicicrc"eZdZdZdZdZdZdZdZe dhZ d"dZ dZ d#dZ d#dZej ej"fdZd#d Zd Zd Zd Zd ZdZdZdZdZdZdZdZd$dZdZ dZ!dZ"dZ#dZ$dZ%dZ&dZ'dZ(dZ)d Z*d!Z+y)% dispatcherFNwarningc`| t|_n||_d|_|rA|jd|j ||d|_ |j |_yd|_ y#t$r<}|jttfvrd|_n|j|Yd}~yd}~wwxYw)NFT)r@_map_fileno setblocking set_socket connected getpeernameaddrr9r:rr del_channelsocket)selfsockrKrs r__init__zdispatcher.__init__s ;"DIDI    U # OOD# &!DN  ,,. DK 996 22&+DN $$S) # s A(( B-12B((B-c|jjdz|jjzg}|jr|jr|j dn|j r|j d|j |j d|jzddj|t|fzS#t$r'|j t|jYNwxYw)N. listeningrfz%s:%dz <%s at %#x> ) __class__rrrFrhrErf TypeErrorreprjoinid)rkstatuss r__repr__zdispatcher.__repr__s..++C/0K0KKL >>dii MM+ & ^^ MM+ & 99  / g 12 0"T(;;; / d499o. /sC-C32C3c>| |j}|||j<yr!)rbrc)rkrKs r add_channelzdispatcher.add_channels ;))C DLLrcT|j}| |j}||vr||=d|_yr!)rcrb)rkrKrNs rrizdispatcher.del_channels. \\ ;))C 9B rc||f|_tj||}|jd|j|yNF)family_and_typerjrdre)rkfamilytyperls r create_socketzdispatcher.create_sockets8%t|}}VT*  rc^||_|j|_|j|yr!)rjfilenorcrzrkrlrKs rrezdispatcher.set_socket$s$ {{}  rc |jjtjtj|jj tjtjdzy#t $rYywxYw)NrX)rj setsockopt SOL_SOCKET SO_REUSEADDR getsockoptr9rks rset_reuse_addrzdispatcher.set_reuse_addr)sj  KK " "!!6#6#6 &&v'8'8'-':':<>?@     sA2A55 BBcyNTrrs rrCzdispatcher.readable:rcyrrrs rrDzdispatcher.writable=rrczd|_tjdk(r|dkDrd}|jj |S)NTnt)rFrnamerjlisten)rknums rrzdispatcher.listenDs4 77d?sQwC{{!!#&&rcF||_|jj|Sr!)rhrjbind)rkrhs rrzdispatcher.bindJs {{%%rc,d|_d|_|jj|}|tt t fvs|tk(rtjdk(r||_ y|dtfvr||_ |jyt|t|)NFTrr)rf connectingrj connect_exrrrrrrrhr handle_connect_eventr9r)rkaddressrs rconnectzdispatcher.connectNs~kk$$W- ;+6 6 &=RWW_DI  1g, DI  % % '#y~. .rc |jj\}}||fS#t$rYyt$r(}|jt t tfvrYd}~yd}~wwxYwr!)rjacceptrsr9r:rr r)rkconnrhwhys rrzdispatcher.accept\s_ ++-JD$:   yy[,??  s# AAAAAc |jj|}|S#t$rE}|jtk(rYd}~y|jt vr|j Yd}~yd}~wwxYwNr)rjsendr9r:rr;r8)rkdataresultrs rrzdispatcher.sendjs] [[%%d+FM yyK'm+!!# s  A-A("A('A((A-c |jj|}|s|jy|S#t$r-}|jt vr|jYd}~yd}~wwxYwNr)rjrecvr8r9r:r;)rk buffer_sizerrs rrzdispatcher.recvwsb ;;##K0D!!#  yyM)!!#  s-22 A("A#"A##A(cd|_d|_d|_|j|j |jj yy#t $r#}|jttfvrYd}~yd}~wwxYwr}) rfrFrrirjcloser9r:rr )rkrs rrzdispatcher.closesr  ;; "  !!# # 99Xu$556 sA A;A66A;cZtjjdt|zy)Nzlog: %s )sysstderrr,str)rkmessages rlogzdispatcher.logs s7|34rcB||jvrt|d|yy)Nz: )ignore_log_typesprint)rkrrs rlog_infozdispatcher.log_infos" t,, , dG, - -rc|jr|jy|js-|jr|j |j y|j yr!)rF handle_acceptrfrr handle_readrs rr"zdispatcher.handle_read_eventsI >>    ))+        rc|jjtjtj}|dk7rt |t ||j d|_d|_y)NrTF) rjrrSO_ERRORr9rhandle_connectrfrrkrs rrzdispatcher.handle_connect_eventsTkk$$V%6%6H !8#y~. . rc|jry|js|jr|j|j yr!)rFrfrr handle_writers rr+zdispatcher.handle_write_events4 >> ~~))+ rc|jjtjtj}|dk7r|j y|j yr)rjrrrr8 handle_exptrs rr.zdispatcher.handle_expt_eventsEkk$$V%6%6H !8        rc t\}}}} t|}|jd|d|d|d|d d|j y#dt|z}YBxYw)Nz)<__repr__(self) failed for object at %0x>z-uncaptured python exception, closing channel z (:rq)error)compact_tracebackrtrvrr8)rkniltvtbinfo self_reprs rr$zdispatcher.handle_errorsj-/Q6 OT I       OCbhNIs A Ac(|jddy)Nz!unhandled incoming priority eventr`rrs rrzdispatcher.handle_expts 99Erc(|jddy)Nzunhandled read eventr`rrs rrzdispatcher.handle_reads ,i8rc(|jddy)Nzunhandled write eventr`rrs rrzdispatcher.handle_writes -y9rc(|jddy)Nzunhandled connect eventr`rrs rrzdispatcher.handle_connects /;rcH|j}||j|yyr!)rhandle_accepted)rkpairs rrzdispatcher.handle_accepts({{}   D $ ' rcH|j|jddy)Nzunhandled accepted eventr`)rr)rkrlrhs rrzdispatcher.handle_accepteds  0)>8J8J  "' & /  $  5.    (F9:<( =rr_c,eZdZddZdZdZdZdZy)dispatcher_with_sendNc@tj|||d|_yr)r_rm out_bufferrs rrmzdispatcher_with_send.__init__sD$,rcvd}tj||jdd}|j|d|_y)Nri)r_rr)rknum_sents r initiate_sendz"dispatcher_with_send.initiate_sends4??4%)@A//()4rc$|jyr!)rrs rrz!dispatcher_with_send.handle_write s rcJ|j xst|jSr!)rflenrrs rrDzdispatcher_with_send.writablesNN";s4??';;rc|jr|jdt|z|j|z|_|j y)Nz sending %s)rrrtrr)rkrs rrzdispatcher_with_send.sends: :: MM,d3 4//D0 rr)rrrrmrrrDrrrrrrs5 <rrctj}|j}|s tdg}|rr|j |j j j|j j jt|jf|j}|rr~|d\}}}dj|Dcgc]}d|z c}}|||ft|||fScc}w)Nztraceback does not existrqz [%s|%s|%s])r exception __traceback__AssertionErrorrEtb_framef_code co_filenameco_namer tb_linenotb_nextrur)exctbrfilefunctionlinexrs rrrs --/C  B 788 F  KK   * * KK   & &    ZZ  !":D(D 88v6!\A%6 7D (D !49c4 777s= C"c|t}t|jD]} |j|jy#t$r!}|j t k(rn|sYd}~Ld}~wt$r|sY_xYwr!) r@rAvaluesrr9r:r r#clear)rK ignore_allrs r close_allr0s} { #**,    GGI IIK ww%#   sA  BA..Bposixc@eZdZdZdZdZdZd dZeZeZ dZ dZ y) file_wrapperc8tj||_yr!)rduprNrkrNs rrmzfile_wrapper.__init__UsffRjDGrc|jdk\rtjd|zt||j y)Nrzunclosed file %r)source)rNwarningswarnResourceWarningrrs r__del__zfile_wrapper.__del__Xs/ww!| 047%)+ JJLrcBtj|jg|Sr!)rr'rNrkargss rrzfile_wrapper.recv^s77477*T* *rcBtj|jg|Sr!)rr,rNr s rrzfile_wrapper.sendas88DGG+d+ +rNcj|tjk(r|tjk(r|sytd)Nrz-Only asyncore specific behaviour implemented.)rjrrNotImplementedError)rkleveloptnamebuflens rrzfile_wrapper.getsockoptds4***6??*%'56 6rct|jdkry|j}d|_tj|y)Nrr)rNrrrs rrzfile_wrapper.closeos+ww{BDG HHRLrc|jSr!)rNrs rrzfile_wrapper.filenovs 77Nrr!) rrrrmr rrrr'r,rrrrrrrPs1  !   + , 6  rrceZdZddZdZy)file_dispatcherNctj|d|d|_ |j}|j |t j|dy#t$rY3wxYw)NTF)r_rmrfrAttributeErrorset_filer set_blocking)rkrNrKs rrmzfile_dispatcher.__init__{s[   dC 0!DN YY[ MM"  OOB & "  sA A$#A$ct||_|jj|_|j yr!)rrjrrcrzrs rrzfile_dispatcher.set_files.&r*DK;;--/DL    rr!)rrrrmrrrrrrys  ' rr)gN)g>@FNNr}).__doc__r1rjrrGrrr:rrrrrrr r r r r rrrr;r@rr ExceptionrKeyboardInterrupt SystemExitr#r'r,r/r>rQrVpoll3r]r_rrrrrrrrrr!s B*   :xL% "# ' i  !2J?(%N"4 $aaP :68*>77g''R*UQJsB99CC