YfN@s dZddlZddlZddlZddlZddlZddlZddlmZm Z m Z m Z m Z m Z mZmZmZmZmZmZmZee e eeeehZyeWnek riZYnXddZGdddeZeeefZdd Zd d Z d d Z!ddZ"ddddZ#ddddZ$e$Z%ddddddZ&GdddZ'Gddde'Z(ddZ)dddd Z*ej+d!kr Gd"d#d#Z,Gd$d%d%e'Z-dS)&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 CsOytj|SWn7tttfk rJ|tkr>t|Sd|SYnXdS)NzUnknown error %s)osstrerror ValueError OverflowError NameErrorr)errr-/opt/alt/python35/lib64/python3.5/asyncore.py _strerrorDs  rc@seZdZdS)ExitNowN)__name__ __module__ __qualname__rrrrrLs rc Cs;y|jWn&tk r%Yn|jYnXdS)N)handle_read_event_reraised_exceptions handle_error)objrrrreadQs  r c Cs;y|jWn&tk r%Yn|jYnXdS)N)handle_write_eventrr)rrrrwriteYs  r"c Cs;y|jWn&tk r%Yn|jYnXdS)N)handle_expt_eventrr)rrrr _exceptionas  r$cCsyn|tj@r|j|tj@r1|j|tj@rH|j|tjtjBtj B@rm|j Wntt k r}z/|j dt kr|jn |j WYdd}~Xn&tk rYn|jYnXdS)Nr)selectPOLLINrPOLLOUTr!POLLPRIr#ZPOLLHUPZPOLLERRZPOLLNVAL handle_closeOSErrorargs _DISCONNECTEDrr)rflagserrr readwriteis"        r/gc Cs|dkrt}|rg}g}g}xt|jD]m\}}|j}|j}|rt|j||r|j r|j||s|r=|j|q=Wg|ko|ko|knrtj|dSt j ||||\}}}x6|D].}|j |}|dkr2qt |qWx6|D].}|j |}|dkrkqGt |qGWx6|D].}|j |}|dkrqt |qWdS)N) socket_maplistitemsreadablewritableappend acceptingtimeZsleepr%getr r"r$) timeoutmaprwr.fdrZis_rZis_wrrrpoll}sD      ' !      r>cCs|dkrt}|dk r.t|d}tj}|rxt|jD]k\}}d}|jr|tjtjBO}|j r|j r|tj O}|rS|j ||qSW|j|}x?|D]7\}}|j |}|dkrqt||qWdS)Nir)r0intr%r>r1r2r3r&r(r4r6r'registerr8r/)r9r:Zpollsterr=rr-r;rrrpoll2s(      rAg>@FcCs|dkrt}|r0ttdr0t}nt}|dkr_xJ|r[|||qEWn0x-|r|dkr||||d}qbWdS)Nr>r)r0hasattrr%rAr>)r9Zuse_pollr:countZpoll_funrrrloops     rEc@seZdZdZdZdZdZdZdZe dhZ ddddZ ddZ e Z ddd Zdd d Zejejd d ZdddZddZddZddZddZddZddZddZddZd d!Zd"d#Zd$d%Zd&d'd(Z d)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)A dispatcherFNwarningcCs|dkrt|_n ||_d|_|r|jd|j||d|_y|j|_Wqtk r}z:|j dt t fkrd|_n|j |WYdd}~XqXn d|_ dS)NrTF)r0_map_fileno setblocking set_socket connectedZ getpeernameaddrr*r+rr del_channelsocket)selfsockr:rrrr__init__s         zdispatcher.__init__c Cs|jjd|jjg}|jr?|jr?|jdn|jrU|jd|jdk ry|jd|jWn(tk r|jt|jYnXddj |t |fS)N.Z listeningrLz%s:%dz <%s at %#x> ) __class__rrr6rMr5rL TypeErrorreprjoinid)rPZstatusrrr__repr__s   zdispatcher.__repr__cCs&|dkr|j}|||j|j}|dkr|j}||kr1||=d|_dS)N)rIrH)rPr:r=rrrrNs     zdispatcher.del_channelcCs?||f|_tj||}|jd|j|dS)Nr)Zfamily_and_typerOrJrK)rPZfamilytyperQrrr create_sockets zdispatcher.create_socketcCs)||_|j|_|j|dS)N)rOfilenorIr[)rPrQr:rrrrK s zdispatcher.set_socketc CsRy9|jjtjtj|jjtjtjdBWntk rMYnXdS)NrB)rOZ setsockopt SOL_SOCKETZ SO_REUSEADDR getsockoptr*)rPrrrset_reuse_addr&s   zdispatcher.set_reuse_addrcCsdS)NTr)rPrrrr37szdispatcher.readablecCsdS)NTr)rPrrrr4:szdispatcher.writablecCs:d|_tjdkr*|dkr*d}|jj|S)NTnt)r6rnamerOlisten)rPZnumrrrreAs zdispatcher.listencCs||_|jj|S)N)rMrObind)rPrMrrrrfGs zdispatcher.bindcCsd|_d|_|jj|}|tttfksT|tkratj dkra||_ dS|dt fkr||_ |j nt |t|dS)NFTrbcer)rbrg)rL connectingrOZ connect_exrrrrrrdrMr handle_connect_eventr*r)rPZaddressrrrrconnectKs     zdispatcher.connectcCsy|jj\}}Wn]tk r1dSYnRtk rx}z(|jdtttfkrcdSWYdd}~Xn X||fSdS)Nr)rOacceptrVr*r+rr r )rPZconnrMwhyrrrrkYs zdispatcher.acceptcCsy|jj|}|SWn`tk r|}z@|jdtkrFdS|jdtkrg|jdSWYdd}~XnXdS)Nr)rOsendr*r+rr,r))rPdataresultrlrrrrmgs zdispatcher.sendcCs~y.|jj|}|s)|jdS|SWnItk ry}z)|jdtkrd|jdSWYdd}~XnXdS)Nr)rOrecvr)r*r+r,)rPZ buffer_sizernrlrrrrqts  zdispatcher.recvcCsd|_d|_d|_|j|jdk ry|jjWnAtk r}z!|jdtt fkrvWYdd}~XnXdS)NFr) rLr6rhrNrOcloser*r+rr )rPrlrrrrrs    zdispatcher.closecCstjjdt|dS)Nzlog: %s )sysstderrr"str)rPmessagerrrlogszdispatcher.loginfocCs'||jkr#td||fdS)Nz%s: %s)ignore_log_typesprint)rPrvr\rrrlog_infoszdispatcher.log_infocCsM|jr|jn3|js?|jr2|j|jn |jdS)N)r6 handle_acceptrLrhri handle_read)rPrrrrs      zdispatcher.handle_read_eventcCs\|jjtjtj}|dkr<t|t||jd|_d|_dS)NrTF) rOr`r_SO_ERRORr*rhandle_connectrLrh)rPrrrrris    zdispatcher.handle_connect_eventcCs7|jr dS|js)|jr)|j|jdS)N)r6rLrhri handle_write)rPrrrr!s     zdispatcher.handle_write_eventcCsB|jjtjtj}|dkr4|jn |jdS)Nr)rOr`r_r~r) handle_expt)rPrrrrr#s  zdispatcher.handle_expt_eventc Csnt\}}}}yt|}Wndt|}YnX|jd||||fd|jdS)Nz)<__repr__(self) failed for object at %0x>z:uncaptured python exception, closing channel %s (%s:%s %s)error)compact_tracebackrWrYr{r))rPZniltvtbinfoZ self_reprrrrrszdispatcher.handle_errorcCs|jdddS)Nz!unhandled incoming priority eventrG)r{)rPrrrrszdispatcher.handle_exptcCs|jdddS)Nzunhandled read eventrG)r{)rPrrrr}szdispatcher.handle_readcCs|jdddS)Nzunhandled write eventrG)r{)rPrrrrszdispatcher.handle_writecCs|jdddS)Nzunhandled connect eventrG)r{)rPrrrrszdispatcher.handle_connectcCs)|j}|dk r%|j|dS)N)rkhandle_accepted)rPZpairrrrr|s  zdispatcher.handle_acceptcCs|j|jdddS)Nzunhandled accepted eventrG)rrr{)rPrQrMrrrrs zdispatcher.handle_acceptedcCs|jdd|jdS)Nzunhandled close eventrG)r{rr)rPrrrr)szdispatcher.handle_close)-rrrdebugrLr6rhclosingrM frozensetryrRrZ__str__r[rNrOZAF_INETZ SOCK_STREAMr]rKrar3r4rerfrjrkrmrqrrrwr{rrir!r#rrr}rrr|rr)rrrrrFsL                      rFc@sReZdZddddZddZddZdd Zd d ZdS) dispatcher_with_sendNcCs tj|||d|_dS)Nrp)rFrR out_buffer)rPrQr:rrrrRszdispatcher_with_send.__init__cCs?d}tj||jdd}|j|d|_dS)Nri)rFrmr)rPZnum_sentrrr initiate_sendsz"dispatcher_with_send.initiate_sendcCs|jdS)N)r)rPrrrrsz!dispatcher_with_send.handle_writecCs|j pt|jS)N)rLlenr)rPrrrr4 szdispatcher_with_send.writablecCs>|jr |jdt||j||_|jdS)Nz sending %s)rr{rWrr)rPrnrrrrms zdispatcher_with_send.send)rrrrRrrr4rmrrrrrs    rcCstj\}}}g}|s-tdxD|rs|j|jjj|jjjt|j f|j }q0W~|d\}}}dj dd|D}|||f|||fS)Nztraceback does not existrBrTcSsg|]}d|qS)z [%s|%s|%s]r).0xrrr )s z%compact_traceback..) rsexc_infoAssertionErrorr5tb_framef_code co_filenameco_nameru tb_linenotb_nextrX)rrtbrfileZfunctionlinerxrrrrs     rcCs|dkrt}xt|jD]}y|jWq%tk r}z$|jdtkrdn |smWYdd}~Xq%tk rYq%|sYq%Xq%W|jdS)Nr) r0r1valuesrrr*r+r rclear)r:Z ignore_allrrrr close_all,s    rposixc@sseZdZddZddZddZddZd d d ZeZeZ d d Z ddZ d S) file_wrappercCstj||_dS)N)rdupr=)rPr=rrrrRQszfile_wrapper.__init__cCs1|jdkr#tjd|t|jdS)Nrzunclosed file %r)r=warningswarnResourceWarningrr)rPrrr__del__Tszfile_wrapper.__del__cGstj|j|S)N)rr r=)rPr+rrrrqYszfile_wrapper.recvcGstj|j|S)N)rr"r=)rPr+rrrrm\szfile_wrapper.sendNcCs9|tjkr)|tjkr)| r)dStddS)Nrz-Only asyncore specific behaviour implemented.)rOr_r~NotImplementedError)rPlevelZoptnameZbuflenrrrr`_s zfile_wrapper.getsockoptcCs0|jdkrdStj|jd|_dS)NrrBr)r=rrr)rPrrrrrjszfile_wrapper.closecCs|jS)N)r=)rPrrrr^pszfile_wrapper.fileno) rrrrRrrqrmr`r r"rrr^rrrrrLs      rc@s+eZdZdddZddZdS)file_dispatcherNc Csbtj|d|d|_y|j}Wntk r@YnX|j|tj|ddS)NTF)rFrRrLr^AttributeErrorset_filer set_blocking)rPr=r:rrrrRus   zfile_dispatcher.__init__cCs/t||_|jj|_|jdS)N)rrOr^rIr[)rPr=rrrrszfile_dispatcher.set_file)rrrrRrrrrrrss  r).__doc__r%rOrsr7rrerrnorrrrrrrr r r r r rrr,r0rr ExceptionrKeyboardInterrupt SystemExitrr r"r$r/r>rAZpoll3rErFrrrrdrrrrrr/s@      X        ', '