o ¯b{+ã@s®dZddlZddlZddlmZmZddlmZmZm Z ddl m Z ddl m Z ddlmZddlmZmZmZdd lmZdd lmZe eƒGd d „d eƒƒZdd d„ZdS)z' asyncio-based reactor implementation. éN)ÚAbstractEventLoopÚget_event_loop)ÚDictÚOptionalÚType)Ú implementer)ÚFileDescriptor)Ú IReactorFDSet)Ú _NO_FILEDESCÚPosixReactorBaseÚ_ContinuousPolling)ÚLogger)ÚcallWithLoggercsÐeZdZdZdZeƒZd,deef‡fdd„ Z dd„Z d d „Z d d „Z d d„Z dd„Zdd„Zdd„Zdd„Zdd„Zdd„Zd-dd„Z‡fdd„Z‡fd d!„Zd"d#„Zd$d%„Zd&d'„Zd(d)„Zd*d+„Z‡ZS).ÚAsyncioSelectorReactora† Reactor running on top of L{asyncio.SelectorEventLoop}. On POSIX platforms, the default event loop is L{asyncio.SelectorEventLoop}. On Windows, the default event loop on Python 3.7 and older is C{asyncio.WindowsSelectorEventLoop}, but on Python 3.8 and newer the default event loop is C{asyncio.WindowsProactorEventLoop} which is incompatible with L{AsyncioSelectorReactor}. Applications that use L{AsyncioSelectorReactor} on Windows with Python 3.8+ must call C{asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())} before instantiating and running L{AsyncioSelectorReactor}. FNÚ eventloopcsx|durtƒ}n|}tjdkr!ddlm}t||ƒr!td|›ƒ‚||_i|_i|_ t |ƒ|_ d|_ d|_ tƒ ¡dS)NÚwin32r)ÚProactorEventLoopz)ProactorEventLoop is not supported, got: )rÚsysÚplatformÚasynciorÚ isinstanceÚ TypeErrorÚ_asyncioEventloopÚ_writersÚ_readersr Ú_continuousPollingÚ _scheduledAtÚ _timerHandleÚsuperÚ__init__)ÚselfrÚ _eventloopr©Ú __class__©úA/usr/lib/python3/dist-packages/twisted/internet/asyncioreactor.pyr0s    ÿ zAsyncioSelectorReactor.__init__cCs*z |jj |¡WdStyYdSw)am Compensate for a bug in asyncio where it will not unregister a FD that it cannot handle in the epoll loop. It touches internal asyncio code. A description of the bug by markrwilliams: The C{add_writer} method of asyncio event loops isn't atomic because all the Selector classes in the selector module internally record a file object before passing it to the platform's selector implementation. If the platform's selector decides the file object isn't acceptable, the resulting exception doesn't cause the Selector to un-track the file object. The failing/hanging stdio test goes through the following sequence of events (roughly): * The first C{connection.write(intToByte(value))} call hits the asyncio reactor's C{addWriter} method. * C{addWriter} calls the asyncio loop's C{add_writer} method, which happens to live on C{_BaseSelectorEventLoop}. * The asyncio loop's C{add_writer} method checks if the file object has been registered before via the selector's C{get_key} method. * It hasn't, so the KeyError block runs and calls the selector's register method * Code examples that follow use EpollSelector, but the code flow holds true for any other selector implementation. The selector's register method first calls through to the next register method in the MRO * That next method is always C{_BaseSelectorImpl.register} which creates a C{SelectorKey} instance for the file object, stores it under the file object's file descriptor, and then returns it. * Control returns to the concrete selector implementation, which asks the operating system to track the file descriptor using the right API. * The operating system refuses! An exception is raised that, in this case, the asyncio reactor handles by creating a C{_ContinuousPolling} object to watch the file descriptor. * The second C{connection.write(intToByte(value))} call hits the asyncio reactor's C{addWriter} method, which hits the C{add_writer} method. But the loop's selector's get_key method now returns a C{SelectorKey}! Now the asyncio reactor's C{addWriter} method thinks the asyncio loop will watch the file descriptor, even though it won't. N)rÚ _selectorÚ unregisterÚ BaseException)r Úfdr$r$r%Ú_unregisterFDInAsyncioKs 2 ÿz-AsyncioSelectorReactor._unregisterFDInAsyncioc Csˆ|r|jn|j}| ¡dkr| |t|¡dSz|ƒ}Wnty6}z|}|j d¡WYd}~nd}~ww|rB| |||¡dSdS©Néÿÿÿÿ)ÚdoReadÚdoWriteÚfilenoÚ_disconnectSelectabler Ú ExceptionÚ_logÚfailure)r Ú selectableÚreadÚmethodÚwhyÚer$r$r%Ú _readOrWrite‚s  €þÿz#AsyncioSelectorReactor._readOrWritec Csœ||j ¡vs ||jjvrdS| ¡}z|j |t||j|d¡||j|<WdStyM}z|  |¡|j t j krA|j  |¡n‚WYd}~dSd}~ww)NT) rÚkeysrr/rÚ add_readerrr9ÚOSErrorr*ÚerrnoÚEPERMÚ addReader)r Úreaderr)r8r$r$r%r?‘s ÿ  þ€úz AsyncioSelectorReactor.addReadercCs®||j ¡vs ||jjvrdS| ¡}z|j |t||j|d¡||j|<WdSty<|  |¡|j  |¡YdSt yJ|  |¡YdSt yV|  |¡‚w)NF) rr:rr/rÚ add_writerrr9ÚPermissionErrorr*Ú addWriterÚBrokenPipeErrorr(©r Úwriterr)r$r$r%rC¥s"ÿ     þz AsyncioSelectorReactor.addWritercCót||j ¡vs|j |¡sdS|j |¡r|j |¡dS| ¡}|dkr,|j |¡}n|j |¡|j |¡dSr+) rr:rÚ isReadingÚ removeReaderr/ÚpoprÚ remove_reader)r r@r)r$r$r%rI¼s   z#AsyncioSelectorReactor.removeReadercCrGr+) rr:rÚ isWritingÚ removeWriterr/rJrÚ remove_writerrEr$r$r%rMÔs   z#AsyncioSelectorReactor.removeWritercCs"| |j ¡|j ¡¡|j ¡S©N)Ú _removeAllrr:rrÚ removeAll©r r$r$r%rQísÿÿz AsyncioSelectorReactor.removeAllcCót|j ¡ƒ|j ¡SrO)Úlistrr:rÚ getReadersrRr$r$r%rUóóz!AsyncioSelectorReactor.getReaderscCrSrO)rTrr:rÚ getWritersrRr$r$r%rWörVz!AsyncioSelectorReactor.getWriterscCs$|j |d|jj¡|j ¡dS)Ng{®Gáz„?)rÚ call_laterÚstopÚ run_forever)r Útimeoutr$r$r%ÚiterateùszAsyncioSelectorReactor.iterateTcCs*|j|d|j ¡|jrd|_dSdS)N)ÚinstallSignalHandlersF)Ú startRunningrrZÚ _justStopped)r r]r$r$r%Úrunýs   ÿzAsyncioSelectorReactor.runcstƒ ¡| ddd„¡dS)NrcSsdSrOr$r$r$r$r%Úsz-AsyncioSelectorReactor.stop..)rrYÚ callLaterrRr"r$r%rYs zAsyncioSelectorReactor.stopcstƒ ¡|j ¡dSrO)rÚcrashrrYrRr"r$r%rc s zAsyncioSelectorReactor.crashcCsd|_| ¡| ¡dSrO)rÚrunUntilCurrentÚ _reschedulerRr$r$r%Ú_onTimer s zAsyncioSelectorReactor._onTimercCsR| ¡}|dur'|j ¡|}||_|jdur|j ¡|j ||j¡|_dSdSrO)r[rÚtimerrÚcancelÚcall_atrf)r r[Úabs_timer$r$r%res  ûz"AsyncioSelectorReactor._reschedulecCst ||¡| ¡dSrO)r Ú_moveCallLaterSoonerre)r Útpler$r$r%rks  z+AsyncioSelectorReactor._moveCallLaterSoonercOsNtj|||g|¢Ri|¤Ž}|j ¡| ¡}|jdus!||jkr%| ¡|SrO)r rbrrgr[rre)r ÚsecondsÚfÚargsÚkwargsÚdcrjr$r$r%rbs z AsyncioSelectorReactor.callLatercs"‡‡‡‡fdd„}ˆj |¡dS)Ncsˆjdˆgˆ¢Riˆ¤ŽS)Nr)rbr$©rornrpr r$r%ra'sz7AsyncioSelectorReactor.callFromThread..)rÚcall_soon_threadsafe)r rnrorpÚgr$rrr%ÚcallFromThread&sz%AsyncioSelectorReactor.callFromThreadrO)T)Ú__name__Ú __module__Ú __qualname__Ú__doc__Ú _asyncClosedr r2rrrr*r9r?rCrIrMrQrUrWr\r`rYrcrfrerkrbruÚ __classcell__r$r$r"r%rs.7    rcCs t|ƒ}ddlm}||ƒdS)z‘ Install an asyncio-based reactor. @param eventloop: The asyncio eventloop to wrap. If default, the global one is selected. r)ÚinstallReactorN)rÚtwisted.internet.mainr|)rÚreactorr|r$r$r%Úinstall+s  rrO)ryr=rrrrÚtypingrrrÚzope.interfacerÚtwisted.internet.abstractrÚtwisted.internet.interfacesr Útwisted.internet.posixbaser r r Útwisted.loggerr Útwisted.python.logrrrr$r$r$r%Ús