o ?Og,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//opt/alt/python310/lib64/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)ZPOLLHUPZPOLLERRZPOLLNVAL handle_closeOSErrorerrno _DISCONNECTEDr"r!)r$flagserrr readwriteps(      r5c 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*) timeoutmaprwr4fdr$Zis_rZis_wrrrpollsF     "       rFcCs|durt}|durt|d}t}|r^t|D]&\}}d}|r.|tjtjBO}| r:|j s:|tj O}|rB| ||q||}|D]\}}| |}|durXqJt||qJdSdS)Nir)r7intr+rFr8r9r:r,r.r;r=r-registerr@r5)rArBZpollsterrEr$r3rCrrrpoll2s.        rI>@FcCs|durt}|rttdrt}nt}|dur#|r!||||sdSdS|r:|dkr>||||d}|r<|dks)dSdSdSdS)NrFr)r7hasattrr+rIrF)rAZuse_pollrBcountZpoll_funrrrloops   rNc@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)r7_map_fileno setblocking set_socket connectedZ getpeernameaddrr0r1rr del_channelsocket)selfsockrBrrrr__init__s&    zdispatcher.__init__cCs|jjd|jjg}|jr|jr|dn|jr|d|jdur@z |d|jWnty?|t|jYnwdd |t |fS)N.Z listeningrUz%s:%dz <%s at %#x> ) __class__rrr=rVr<rU TypeErrorreprjoinid)rYZstatusrrr__repr__s     zdispatcher.__repr__cCs|dur|j}|||j<dSr)rQrR)rYrBrrr add_channelszdispatcher.add_channelcCs,|j}|dur |j}||vr||=d|_dSr)rRrQ)rYrBrErrrrWs  zdispatcher.del_channelcCs.||f|_t||}|d||dSNF)Zfamily_and_typerXrSrT)rYZfamilytyperZrrr create_sockets   zdispatcher.create_socketcCs||_||_||dSr)rXfilenorRrdrYrZrBrrrrT%s zdispatcher.set_socketcCsBz|jtjtj|jtjtjdBWdSty YdSw)NrK)rXZ setsockopt SOL_SOCKETZ SO_REUSEADDR getsockoptr0rYrrrset_reuse_addr*s  zdispatcher.set_reuse_addrcCdSNTrrlrrrr:;zdispatcher.readablecCrnrorrlrrrr;>rpzdispatcher.writablecCs(d|_tjdkr|dkrd}|j|S)NTnt)r=rnamerXlisten)rYZnumrrrrtEs zdispatcher.listencCs||_|j|Sr)rVrXbind)rYrVrrrruKs zdispatcher.bindcCsnd|_d|_|j|}|tttfvs|tkr!tj dkr!||_ dS|dt fvr0||_ | dSt |t|)NFTrqr)rU connectingrXZ connect_exrrrrrrsrVr handle_connect_eventr0r)rYZaddressrrrrconnectOs   zdispatcher.connectc Csdz |j\}}W||fStyYdSty1}z|jtttfvr,WYd}~dSd}~wwr)rXacceptr_r0r1rr r )rYZconnrVwhyrrrry]s zdispatcher.acceptc Csfz |j|}|WSty2}z|jtkrWYd}~dS|jtvr-|WYd}~dSd}~wwNr)rXsendr0r1rr2r/)rYdataresultrzrrrr|ks   zdispatcher.sendc Cs`z|j|}|s|WdS|WSty/}z|jtvr*|WYd}~dSd}~wwN)rXrecvr/r0r1r2)rYZ buffer_sizer}rzrrrrxs  zdispatcher.recvc Cspd|_d|_d|_||jdur6z|jWdSty5}z|jtt fvr*WYd}~dSd}~wwdSre) rUr=rvrWrXcloser0r1rr )rYrzrrrrs zdispatcher.closecCstjdt|dS)Nzlog: %s )sysstderrr(str)rYmessagerrrlogszdispatcher.loginfocCs"||jvrtd||fdSdS)Nz%s: %s)ignore_log_typesprint)rYrrfrrrlog_infos zdispatcher.log_infocCs>|jr |dS|js|jr||dS|dSr)r= handle_acceptrUrvrw handle_readrlrrrr s   zdispatcher.handle_read_eventcCs@|jtjtj}|dkrt|t||d|_d|_dS)NrTF) rXrkrjSO_ERRORr0rhandle_connectrUrvrYrrrrrws  zdispatcher.handle_connect_eventcCs*|jrdS|js|jr||dSr)r=rUrvrw handle_writerlrrrr's  zdispatcher.handle_write_eventcCs2|jtjtj}|dkr|dS|dSr{)rXrkrjrr/ 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_tracebackr`rbrr/)rYZniltvtbinfoZ self_reprrrrr"s  zdispatcher.handle_errorcC|dddS)Nz!unhandled incoming priority eventrPrrlrrrrzdispatcher.handle_exptcCr)Nzunhandled read eventrPrrlrrrrrzdispatcher.handle_readcCr)Nzunhandled write eventrPrrlrrrrrzdispatcher.handle_writecCr)Nzunhandled connect eventrPrrlrrrrrzdispatcher.handle_connectcCs"|}|dur|j|dSdSr)ryhandle_accepted)rYZpairrrrrszdispatcher.handle_acceptcCs||dddS)Nzunhandled accepted eventrP)rr)rYrZrVrrrrszdispatcher.handle_acceptedcCs|dd|dS)Nzunhandled close eventrP)rrrlrrrr/s  zdispatcher.handle_closeNNr)r),rrrdebugrUr=rvclosingrV frozensetrr[rcrdrWrXZAF_INETZ SOCK_STREAMrgrTrmr:r;rtrurxryr|rrrrr rwr'r)r"rrrrrrr/rrrrrOsL         rOc@s6eZdZd ddZddZddZdd Zd d ZdS) dispatcher_with_sendNcCst|||d|_dSr)rOr[ out_bufferrirrrr[s zdispatcher_with_send.__init__cCs.d}t||jdd}|j|d|_dS)Nri)rOr|r)rYZnum_sentrrr initiate_sendsz"dispatcher_with_send.initiate_sendcCs |dSr)rrlrrrr s z!dispatcher_with_send.handle_writecCs|j pt|jSr)rUlenrrlrrrr;szdispatcher_with_send.writablecCs0|jr |dt||j||_|dS)Nz sending %s)rrr`rr)rYr}rrrr|s  zdispatcher_with_send.sendr)rrrr[rrr;r|rrrrrs   rcCst\}}}g}|std|r'||jjj|jjjt|j f|j }|s~|d\}}}d dd|D}|||f|||fS)Nztraceback does not existr]cSsg|]}d|qS)z [%s|%s|%s]r).0xrrr -sz%compact_traceback..) rexc_infoAssertionErrorr<tb_framef_code co_filenameco_namer tb_linenotb_nextra)rrtbrfileZfunctionlinerrrrrs  rc Cs|durt}t|D]1}z|Wq ty/}z|jtkr"n|s%WYd}~q d}~wty6|s<Yq |dSr) r7r8valuesrr0r1r r!clear)rBZ ignore_allrrrr close_all0s$    rposixc@sNeZdZddZddZddZddZdd d ZeZeZ d d Z ddZ d S) file_wrappercCst||_dSr)rduprErYrErrrr[Urzfile_wrapper.__init__cCs*|jdkrtjd|t|d|dS)Nrzunclosed file %r)source)rEwarningswarnResourceWarningrrlrrr__del__Xs   zfile_wrapper.__del__cGtj|jg|RSr)rr%rErYargsrrrr^zfile_wrapper.recvcGrr)rr(rErrrrr|arzfile_wrapper.sendNcCs$|tjkr|tjkr|sdStd)Nrz-Only asyncore specific behaviour implemented.)rXrjrNotImplementedError)rYlevelZoptnameZbuflenrrrrkds  zfile_wrapper.getsockoptcCs(|jdkrdS|j}d|_t|dS)Nrr)rErrrrrrros zfile_wrapper.closecCs|jSr)rErlrrrrhvszfile_wrapper.filenor) rrrr[rrr|rkr%r(rrhrrrrrPs  rc@seZdZdddZddZdS)file_dispatcherNcCsNt|d|d|_z|}Wn tyYnw||t|ddS)NTF)rOr[rUrhAttributeErrorset_filer set_blocking)rYrErBrrrr[{s   zfile_dispatcher.__init__cCs"t||_|j|_|dSr)rrXrhrRrdrrrrrs   zfile_dispatcher.set_filer)rrrr[rrrrrrys  r)r6N)rJFNNre)0__doc__r+rXrr>rrr1rrrrrrrr r r r r rrDeprecationWarningrr2r7rr ExceptionrKeyboardInterrupt SystemExitr!r%r(r*r5rFrIZpoll3rNrOrrrrsrrrrrrsT<     ' *  )