o [d,O@sxdZddlZddlZddlZddlZddlZddlZddlmZm Z m Z m Z m Z m Z mZmZmZmZmZmZmZejdeddee e eeeehZzeWn eyYiZYnwddZGd d d eZeeefZ d d Z!d dZ"ddZ#ddZ$d)ddZ%d)ddZ&e&Z'd*ddZ(GdddZ)Gddde)Z*d d!Z+d+d"d#Z,ej-d$krGd%d&d&Z.Gd'd(d(e)Z/dSdS),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 errorcodezlThe asyncore module is deprecated and will be removed in Python 3.12. The recommended replacement is asyncio) stacklevelc CsBzt|WStttfy |tvrt|YSd|YSw)NzUnknown error %s)osstrerror ValueError OverflowError NameErrorr)errr/usr/lib/python3.10/asyncore.py _strerrorKs   rc@s eZdZdS)ExitNowN)__name__ __module__ __qualname__rrrrrSsrcC2z|WdSty|YdSN)handle_read_event_reraised_exceptions handle_errorobjrrrreadX  r%cCrr)handle_write_eventr!r"r#rrrwrite`r&r(cCrr)handle_expt_eventr!r"r#rrr _exceptionhr&r*c Csz0|tj@r ||tj@r||tj@r||tjtjBtj B@r.| WdSWdSt yX}z|j t vrB|n | WYd}~dSWYd}~dSd}~wty_|YdSr)selectPOLLINr POLLOUTr'POLLPRIr)POLLHUPPOLLERRPOLLNVAL handle_closeOSErrorerrno _DISCONNECTEDr"r!)r$flagserrr readwriteps(      r8c Cs6|durt}|rg}g}g}t|D]&\}}|}|}|r'|||r1|js1|||s5|r:||qg|krJ|krJ|krSnnt|dSt ||||\}}}|D]}| |}|durlq`t |q`|D]}| |}|durqst |qs|D]}| |}|durqt |qdSdSr) socket_maplistitemsreadablewritableappend acceptingtimesleepr+getr%r(r*) timeoutmaprwr7fdr$is_ris_wrrrpollsF     "       rKcCs|durt}|durt|d}t}|r^t|D]&\}}d}|r.|tjtjBO}| r:|j s:|tj O}|rB| ||q||}|D]\}}| |}|durXqJt||qJdSdS)Nir)r:intr+rKr;r<r=r,r.r>r@r-registerrCr8)rDrEpollsterrHr$r6rFrrrpoll2s.        rO>@FcCs|durt}|rttdrt}nt}|dur#|r!||||sdSdS|r:|dkr>||||d}|r<|dks)dSdSdSdS)NrKr)r:hasattrr+rOrK)rDuse_pollrEcountpoll_funrrrloops   rVc@s2eZdZdZdZdZdZdZdZe dhZ dAddZ ddZ dBdd Z dBd d Zejejfd d ZdBddZddZddZddZddZddZddZddZddZd d!Zd"d#Zd$d%ZdCd'd(Zd)d*Z d+d,Z!d-d.Z"d/d0Z#d1d2Z$d3d4Z%d5d6Z&d7d8Z'd9d:Z(d;d<Z)d=d>Z*d?d@Z+dS)D dispatcherFNwarningc Cs|durt|_n||_d|_|rK|d|||d|_z||_WdStyJ}z|j t t fvr9d|_n| |WYd}~dSd}~wwd|_ dS)NFT)r:_map_fileno setblocking set_socket connected getpeernameaddrr3r4rr del_channelsocket)selfsockrErrrr__init__s&    zdispatcher.__init__cCs|jjd|jjg}|jr|jr|dn|jr|d|jdur@z |d|jWnty?|t|jYnwdd |t |fS)N. listeningr]z%s:%dz <%s at %#x> ) __class__rrr@r_r?r] TypeErrorreprjoinid)rbstatusrrr__repr__s     zdispatcher.__repr__cCs|dur|j}|||j<dSr)rYrZ)rbrErrr add_channelszdispatcher.add_channelcCs,|j}|dur |j}||vr||=d|_dSr)rZrY)rbrErHrrrr`s  zdispatcher.del_channelcCs.||f|_t||}|d||dSNF)family_and_typerar[r\)rbfamilytypercrrr create_sockets   zdispatcher.create_socketcCs||_||_||dSr)rafilenorZrorbrcrErrrr\%s zdispatcher.set_socketcCsBz|jtjtj|jtjtjdBWdSty YdSw)NrQ)ra setsockopt SOL_SOCKET SO_REUSEADDR getsockoptr3rbrrrset_reuse_addr*s  zdispatcher.set_reuse_addrcCdSNTrr{rrrr=;zdispatcher.readablecCr}r~rr{rrrr>>rzdispatcher.writablecCs(d|_tjdkr|dkrd}|j|S)NTnt)r@rnameralisten)rbnumrrrrEs zdispatcher.listencCs||_|j|Sr)r_rabind)rbr_rrrrKs zdispatcher.bindcCsnd|_d|_|j|}|tttfvs|tkr!tj dkr!||_ dS|dt fvr0||_ | dSt |t|)NFTrr)r] connectingra connect_exrrrrrrr_r handle_connect_eventr3r)rbaddressrrrrconnectOs   zdispatcher.connectc Csdz |j\}}W||fStyYdSty1}z|jtttfvr,WYd}~dSd}~wwr)raacceptrir3r4rr r )rbconnr_whyrrrr]s zdispatcher.acceptc Csfz |j|}|WSty2}z|jtkrWYd}~dS|jtvr-|WYd}~dSd}~wwNr)rasendr3r4rr5r2)rbdataresultrrrrrks   zdispatcher.sendc Cs`z|j|}|s|WdS|WSty/}z|jtvr*|WYd}~dSd}~wwN)rarecvr2r3r4r5)rb buffer_sizerrrrrrxs  zdispatcher.recvc Cspd|_d|_d|_||jdur6z|jWdSty5}z|jtt fvr*WYd}~dSd}~wwdSrp) r]r@rr`racloser3r4rr )rbrrrrrs zdispatcher.closecCstjdt|dS)Nzlog: %s )sysstderrr(str)rbmessagerrrlogszdispatcher.loginfocCs"||jvrtd||fdSdS)Nz%s: %s)ignore_log_typesprint)rbrrsrrrlog_infos zdispatcher.log_infocCs>|jr |dS|js|jr||dS|dSr)r@ handle_acceptr]rr handle_readr{rrrr s   zdispatcher.handle_read_eventcCs@|jtjtj}|dkrt|t||d|_d|_dS)NrTF) rarzrxSO_ERRORr3rhandle_connectr]rrbrrrrrs  zdispatcher.handle_connect_eventcCs*|jrdS|js|jr||dSr)r@r]rr handle_writer{rrrr's  zdispatcher.handle_write_eventcCs2|jtjtj}|dkr|dS|dSr)rarzrxrr2 handle_exptrrrrr)s  zdispatcher.handle_expt_eventcCsTt\}}}}zt|}Wn dt|}Y|d||||fd|dS)Nz)<__repr__(self) failed for object at %0x>z:uncaptured python exception, closing channel %s (%s:%s %s)error)compact_tracebackrjrlrr2)rbniltvtbinfo self_reprrrrr"s  zdispatcher.handle_errorcC|dddS)Nz!unhandled incoming priority eventrXrr{rrrrzdispatcher.handle_exptcCr)Nzunhandled read eventrXrr{rrrrrzdispatcher.handle_readcCr)Nzunhandled write eventrXrr{rrrrrzdispatcher.handle_writecCr)Nzunhandled connect eventrXrr{rrrrrzdispatcher.handle_connectcCs"|}|dur|j|dSdSr)rhandle_accepted)rbpairrrrrszdispatcher.handle_acceptcCs||dddS)Nzunhandled accepted eventrX)rr)rbrcr_rrrrszdispatcher.handle_acceptedcCs|dd|dS)Nzunhandled close eventrX)rrr{rrrr2s  zdispatcher.handle_closeNNr)r),rrrdebugr]r@rclosingr_ frozensetrrdrnror`raAF_INET SOCK_STREAMrtr\r|r=r>rrrrrrrrrr rr'r)r"rrrrrrr2rrrrrWsL         rWc@s6eZdZd ddZddZddZdd Zd d ZdS) dispatcher_with_sendNcCst|||d|_dSr)rWrd out_bufferrvrrrrds zdispatcher_with_send.__init__cCs.d}t||jdd}|j|d|_dS)Nri)rWrr)rbnum_sentrrr initiate_sendsz"dispatcher_with_send.initiate_sendcCs |dSr)rr{rrrr s z!dispatcher_with_send.handle_writecCs|j pt|jSr)r]lenrr{rrrr>szdispatcher_with_send.writablecCs0|jr |dt||j||_|dS)Nz sending %s)rrrjrr)rbrrrrrs  zdispatcher_with_send.sendr)rrrrdrrr>rrrrrrs   rcCst\}}}g}|std|r'||jjj|jjjt|j f|j }|s~|d\}}}d dd|D}|||f|||fS)Nztraceback does not existrgcSsg|]}d|qS)z [%s|%s|%s]r).0xrrr -sz%compact_traceback..) rexc_infoAssertionErrorr?tb_framef_code co_filenameco_namer tb_linenotb_nextrk)rrtbrfilefunctionlinerrrrrs  rc Cs|durt}t|D]1}z|Wq ty/}z|jtkr"n|s%WYd}~q d}~wty6|s<Yq |dSr) r:r;valuesrr3r4r r!clear)rE ignore_allrrrr close_all0s$    rposixc@sNeZdZddZddZddZddZdd d ZeZeZ d d Z ddZ d S) file_wrappercCst||_dSr)rduprHrbrHrrrrdUrzfile_wrapper.__init__cCs*|jdkrtjd|t|d|dS)Nrzunclosed file %r)source)rHwarningswarnResourceWarningrr{rrr__del__Xs   zfile_wrapper.__del__cGtj|jg|RSr)rr%rHrbargsrrrr^zfile_wrapper.recvcGrr)rr(rHrrrrrarzfile_wrapper.sendNcCs$|tjkr|tjkr|sdStd)Nrz-Only asyncore specific behaviour implemented.)rarxrNotImplementedError)rbleveloptnamebuflenrrrrzds  zfile_wrapper.getsockoptcCs(|jdkrdS|j}d|_t|dS)Nrr)rHrrrrrrros zfile_wrapper.closecCs|jSr)rHr{rrrruvszfile_wrapper.filenor) rrrrdrrrrzr%r(rrurrrrrPs  rc@seZdZdddZddZdS)file_dispatcherNcCsNt|d|d|_z|}Wn tyYnw||t|ddS)NTF)rWrdr]ruAttributeErrorset_filer set_blocking)rbrHrErrrrd{s   zfile_dispatcher.__init__cCs"t||_|j|_|dSr)rrarurZrorrrrrs   zfile_dispatcher.set_filer)rrrrdrrrrrrys  r)r9N)rPFNNrp)0__doc__r+rarrArrr4rrrrrrrr r r r r rrDeprecationWarningrr5r:rr ExceptionrKeyboardInterrupt SystemExitr!r%r(r*r8rKrOpoll3rVrWrrrrrrrrrrsT<     ' *  )