grthtrhthjhtyjytjytkergtrhtrjytjerhrfh4:24 29/09/2026î ¤ fRã@sdZddlZddlZddlZddlZddlZddlZddlmZm Z m Z m Z m Z m Z mZmZmZmZmZmZmZee e eeeefƒZyeWnek ræiZYnXdd„ZGdd„deƒZeeefZdd „Zd d „Z d d „Z!dd„Z"dddd„Z#dddd„Z$e$Z%dddddd„Z&Gdd„dƒZ'Gdd„de'ƒZ(dd„Z)dddd „Z*ej+d!krddl,Z,Gd"d#„d#ƒZ-Gd$d%„d%e'ƒZ.ndS)&a 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) ÚEALREADYÚ EINPROGRESSÚ EWOULDBLOCKÚ ECONNRESETÚEINVALÚENOTCONNÚ ESHUTDOWNÚEISCONNÚEBADFÚ ECONNABORTEDÚEPIPEÚEAGAINÚ errorcodec CsOytj|ƒSWn7tttfk rJ|tkr>t|Sd|SYnXdS)NzUnknown error %s)ÚosÚstrerrorÚ ValueErrorÚ OverflowErrorÚ NameErrorr)Úerr©rú-/opt/alt/python34/lib64/python3.4/asyncore.pyÚ _strerrorDs  rc@seZdZdS)ÚExitNowN)Ú__name__Ú __module__Ú __qualname__rrrrrLs rc Cs;y|jƒWn&tk r%‚Yn|jƒYnXdS)N)Úhandle_read_eventÚ_reraised_exceptionsÚ handle_error)ÚobjrrrÚreadQs  r c Cs;y|jƒWn&tk r%‚Yn|jƒYnXdS)N)Úhandle_write_eventrr)rrrrÚwriteYs  r"c Cs;y|jƒWn&tk r%‚Yn|jƒYnXdS)N)Úhandle_expt_eventrr)rrrrÚ _exceptionas  r$cCsõyz|tj@r|jƒn|tj@r7|jƒn|tj@rQ|jƒn|tjtjBtj B@ry|j ƒnWntt k rË}z/|j dt kr¯|jƒn |j ƒWYdd}~Xn&tk rß‚Yn|jƒYnXdS)Nr)ÚselectÚPOLLINrÚPOLLOUTr!ÚPOLLPRIr#ZPOLLHUPZPOLLERRZPOLLNVALÚ handle_closeÚOSErrorÚargsÚ _DISCONNECTEDrr)rÚflagsÚerrrÚ readwriteis"        r/gc Csë|dkrt}n|rçg}g}g}xŠt|jƒƒD]v\}}|jƒ}|jƒ}|rz|j|ƒn|rš|j rš|j|ƒn|s¦|r@|j|ƒq@q@Wg|koÜ|koÜ|knròtj|ƒdSy%t j ||||ƒ\}}}Wnt k r/dSYnXx9|D]1}|j |ƒ}|dkr^q7nt |ƒq7Wx9|D]1}|j |ƒ}|dkršqsnt |ƒqsWx<|D]1}|j |ƒ}|dkrÖq¯nt|ƒq¯WndS)N)Ú socket_mapÚlistÚitemsÚreadableÚwritableÚappendÚ acceptingÚtimeZsleepr%ÚInterruptedErrorÚgetr r"r$) ÚtimeoutÚmapÚrÚwr.ÚfdrZis_rZis_wrrrÚpoll}sJ     ' %        r?c CsK|dkrt}n|dk r4t|dƒ}ntjƒ}|rGxˆt|jƒƒD]t\}}d}|jƒrŽ|tjtjBO}n|j ƒr´|j r´|tj O}n|rY|j ||ƒqYqYWy|j|ƒ}Wnt k rþg}YnXxE|D]:\}}|j|ƒ}|dkr3qnt||ƒqWndS)Nièr)r0Úintr%r?r1r2r3r&r(r4r6r'Úregisterr8r9r/)r:r;Zpollsterr>rr-r<rrrÚpoll2§s.        rBg>@FcCs–|dkrt}n|r3ttdƒr3t}nt}|dkrbxJ|r^|||ƒqHWn0x-|r‘|dkr‘|||ƒ|d}qeWdS)Nr?ré)r0Úhasattrr%rBr?)r:Zuse_pollr;ÚcountZpoll_funrrrÚloopÅs      rFc@sÛeZdZdZdZdZdZdZdZe dgƒZ dddd„Z dd„Z e Z ddd „Zdd d „Zejejd d „Zddd„Zdd„Zdd„Zdd„Zdd„Zdd„Zdd„Zdd„Zdd„Zd d!„Zd"d#„Zd$d%„Zd&d'„Z d(d)d*„Z!d+d,„Z"d-d.„Z#d/d0„Z$d1d2„Z%d3d4„Z&d5d6„Z'd7d8„Z(d9d:„Z)d;d<„Z*d=d>„Z+d?d@„Z,dAdB„Z-dS)CÚ dispatcherFNÚwarningcCsÖ|dkrt|_n ||_d|_|rÉ|jdƒ|j||ƒd|_y|jƒ|_WqÒtk rÅ}z:|j dt t fkr£d|_n|j |ƒ‚WYdd}~XqÒXn d|_ dS)NrTF)r0Ú_mapÚ_filenoÚ setblockingÚ set_socketÚ connectedZ getpeernameÚaddrr*r+rrÚ del_channelÚsocket)ÚselfÚsockr;rrrrÚ__init__ás         zdispatcher.__init__c CsÊ|jjd|jjg}|jr?|jr?|jdƒn|jrX|jdƒn|jdk r­y|jd|jƒWq­tk r©|jt|jƒƒYq­Xnddj |ƒt |ƒfS)NÚ.Z listeningrMz%s:%dz <%s at %#x>ú ) Ú __class__rrr6rNr5rMÚ TypeErrorÚreprÚjoinÚid)rQZstatusrrrÚ__repr__s  zdispatcher.__repr__cCs)|dkr|j}n|||jrrrrOs      zdispatcher.del_channelcCs?||f|_tj||ƒ}|jdƒ|j|ƒdS)Nr)Zfamily_and_typerPrKrL)rQZfamilyÚtyperRrrrÚ create_sockets zdispatcher.create_socketcCs)||_|jƒ|_|j|ƒdS)N)rPÚfilenorJr\)rQrRr;rrrrL%s zdispatcher.set_socketc CsRy9|jjtjtj|jjtjtjƒdBƒWntk rMYnXdS)NrC)rPZ setsockoptÚ SOL_SOCKETZ SO_REUSEADDRÚ getsockoptr*)rQrrrÚset_reuse_addr+s   zdispatcher.set_reuse_addrcCsdS)NTr)rQrrrr3<szdispatcher.readablecCsdS)NTr)rQrrrr4?szdispatcher.writablecCs=d|_tjdkr-|dkr-d}n|jj|ƒS)NTÚnté)r6rÚnamerPÚlisten)rQZnumrrrrfFs  zdispatcher.listencCs||_|jj|ƒS)N)rNrPÚbind)rQrNrrrrgLs zdispatcher.bindcCs d|_d|_|jj|ƒ}|tttfksT|tkratj dkra||_ dS|dt fkr‰||_ |j ƒnt |t|ƒ‚dS)NFTrcÚcer)zntrh)rMÚ connectingrPZ connect_exrrrrrrerNr Úhandle_connect_eventr*r)rQZaddressrrrrÚconnectPs     zdispatcher.connectcCs‡y|jjƒ\}}Wn]tk r1dSYnRtk rx}z(|jdtttfkrcdS‚WYdd}~Xn X||fSdS)Nr)rPÚacceptrWr*r+rr r )rQZconnrNÚwhyrrrrl^s zdispatcher.acceptcCs�y|jj|ƒ}|SWn`tk r|}z@|jdtkrFdS|jdtkrg|jƒdS‚WYdd}~XnXdS)Nr)rPÚsendr*r+rr,r))rQÚdataÚresultrmrrrrnls zdispatcher.sendcCs~y.|jj|ƒ}|s)|jƒdS|SWnItk ry}z)|jdtkrd|jƒdS‚WYdd}~XnXdS)Nór)rPÚrecvr)r*r+r,)rQZ buffer_sizerormrrrrrys  zdispatcher.recvcCs“d|_d|_d|_|jƒ|jdk r�y|jjƒWq�tk r‹}z$|jdtt fkry‚nWYdd}~Xq�XndS)NFr) rMr6rirOrPÚcloser*r+rr )rQrmrrrrs‹s    zdispatcher.closec Csƒyt|j|ƒ}Wn.tk rFtd|jj|fƒ‚Yn9Xdi|jjd6|d6}tj|tddƒ|SdS)Nz!%s instance has no attribute '%s'zA%(me)s.%(attr)s is deprecated; use %(me)s.socket.%(attr)s insteadÚmeÚattrÚ stacklevelé)ÚgetattrrPÚAttributeErrorrVrÚwarningsÚwarnÚDeprecationWarning)rQruZretattrÚmsgrrrÚ __getattr__™s zdispatcher.__getattr__cCstjjdt|ƒƒdS)Nzlog: %s )ÚsysÚstderrr"Ústr)rQÚmessagerrrÚlog©szdispatcher.logÚinfocCs*||jkr&td||fƒndS)Nz%s: %s)Úignore_log_typesÚprint)rQr‚r]rrrÚlog_info¬szdispatcher.log_infocCsP|jr|jƒn6|jsB|jr5|jƒn|jƒn |jƒdS)N)r6Ú handle_acceptrMrirjÚ handle_read)rQrrrr°s      zdispatcher.handle_read_eventcCs_|jjtjtjƒ}|dkr?t|t|ƒƒ‚n|jƒd|_d|_dS)NrTF) rPrar`ÚSO_ERRORr*rÚhandle_connectrMri)rQrrrrrj¼s    zdispatcher.handle_connect_eventcCs=|jr dS|js/|jr/|jƒq/n|jƒdS)N)r6rMrirjÚ handle_write)rQrrrr!Äs    zdispatcher.handle_write_eventcCsB|jjtjtjƒ}|dkr4|jƒn |jƒdS)Nr)rPrar`rŠr)Ú handle_expt)rQrrrrr#Ïs  zdispatcher.handle_expt_eventc Csntƒ\}}}}yt|ƒ}Wndt|ƒ}YnX|jd||||fdƒ|jƒdS)Nz)<__repr__(self) failed for object at %0x>z:uncaptured python exception, closing channel %s (%s:%s %s)Úerror)Úcompact_tracebackrXrZr‡r))rQZnilÚtÚvÚtbinfoZ self_reprrrrrÞszdispatcher.handle_errorcCs|jddƒdS)Nz!unhandled incoming priority eventrH)r‡)rQrrrr�òszdispatcher.handle_exptcCs|jddƒdS)Nzunhandled read eventrH)r‡)rQrrrr‰õszdispatcher.handle_readcCs|jddƒdS)Nzunhandled write eventrH)r‡)rQrrrrŒøszdispatcher.handle_writecCs|jddƒdS)Nzunhandled connect eventrH)r‡)rQrrrr‹ûszdispatcher.handle_connectcCs,|jƒ}|dk r(|j|ŒndS)N)rlÚhandle_accepted)rQZpairrrrrˆþs  zdispatcher.handle_acceptcCs|jƒ|jddƒdS)Nzunhandled accepted eventrH)rsr‡)rQrRrNrrrr“s zdispatcher.handle_acceptedcCs|jddƒ|jƒdS)Nzunhandled close eventrH)r‡rs)rQrrrr)szdispatcher.handle_close).rrrÚdebugrMr6riÚclosingrNÚ frozensetr…rSr[Ú__str__r\rOrPZAF_INETZ SOCK_STREAMr^rLrbr3r4rfrgrkrlrnrrrsr~rƒr‡rrjr!r#rr�r‰rŒr‹rˆr“r)rrrrrG×sN                       rGc@sReZdZdddd„Zdd„Zdd„Zdd „Zd d „ZdS) Údispatcher_with_sendNcCs tj|||ƒd|_dS)Nrq)rGrSÚ out_buffer)rQrRr;rrrrSszdispatcher_with_send.__init__cCs?d}tj||jdd…ƒ}|j|d…|_dS)Nri)rGrnr™)rQZnum_sentrrrÚ initiate_sendsz"dispatcher_with_send.initiate_sendcCs|jƒdS)N)rš)rQrrrrŒsz!dispatcher_with_send.handle_writecCs|j pt|jƒS)N)rMÚlenr™)rQrrrr4szdispatcher_with_send.writablecCsA|jr#|jdt|ƒƒn|j||_|jƒdS)Nz sending %s)r”r‡rXr™rš)rQrorrrrn!s zdispatcher_with_send.send)rrrrSršrŒr4rnrrrrr˜s    r˜cCsÂtjƒ\}}}g}|s0tdƒ‚nxD|rv|j|jjj|jjjt|j ƒfƒ|j }q3W~|d\}}}dj dd„|Dƒƒ}|||f|||fS)Nztraceback does not existrCrUcSsg|]}d|‘qS)z [%s|%s|%s]r)Ú.0Úxrrrú <s z%compact_traceback..éÿÿÿÿ) rÚexc_infoÚAssertionErrorr5Útb_frameÚf_codeÚ co_filenameÚco_namer�Ú tb_linenoÚtb_nextrY)r�r‘Útbr’ÚfileZfunctionÚliner„rrrr�+s    r�cCs¿|dkrt}nx™t|jƒƒD]…}y|jƒWq(tk r…}z'|jdtkrgn |ss‚nWYdd}~Xq(tk r™‚Yq(|s¨‚nYq(Xq(W|jƒdS)Nr) r0r1Úvaluesrsr*r+r rÚclear)r;Z ignore_allr�rrrÚ close_all?s    r­Úposixc@sseZdZdd„Zdd„Zdd„Zdd„Zd d d „ZeZeZ d d „Z dd„Z d S)Ú file_wrappercCstj|ƒ|_dS)N)rÚdupr>)rQr>rrrrSfszfile_wrapper.__init__cCs4|jdkr&tjd|tƒn|jƒdS)Nrzunclosed file %r)r>rzr{ÚResourceWarningrs)rQrrrÚ__del__iszfile_wrapper.__del__cGstj|j|ŒS)N)rr r>)rQr+rrrrrnszfile_wrapper.recvcGstj|j|ŒS)N)rr"r>)rQr+rrrrnqszfile_wrapper.sendNcCs9|tjkr)|tjkr)| r)dStdƒ‚dS)Nrz-Only asyncore specific behaviour implemented.)rPr`rŠÚNotImplementedError)rQÚlevelZoptnameZbuflenrrrrats zfile_wrapper.getsockoptcCs0|jdkrdStj|jƒd|_dS)NrrCrŸ)r>rrs)rQrrrrsszfile_wrapper.closecCs|jS)N)r>)rQrrrr_…szfile_wrapper.fileno) rrrrSr²rrrnrar r"rsr_rrrrr¯as      r¯c@s+eZdZddd„Zdd„ZdS)Úfile_dispatcherNc Cs�tj|d|ƒd|_y|jƒ}Wntk r@YnX|j|ƒtj|tjdƒ}|tj B}tj|tj |ƒdS)NTr) rGrSrMr_ryÚset_fileÚfcntlZF_GETFLrÚ O_NONBLOCKZF_SETFL)rQr>r;r-rrrrSŠs    zfile_dispatcher.__init__cCs/t|ƒ|_|jjƒ|_|jƒdS)N)r¯rPr_rJr\)rQr>rrrr¶—szfile_dispatcher.set_file)rrrrSr¶rrrrrµˆs  rµ)/Ú__doc__r%rPrr7rzrÚerrnorrrrrrrr r r r r rr–r,r0rrÚ ExceptionrÚKeyboardInterruptÚ SystemExitrr r"r$r/r?rBZpoll3rFrGr˜r�r­rer·r¯rµrrrrÚ/sB      X        *ÿ:  '