3 \[@sdZddddddddd d d d d dgZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl m Z ddl m Z ddZddZd3ddZddZd4ddZGdddZGd ddeZGd!ddZGd"ddZGd#ddZGd$d%d%eZdae jZGd&d'd'e jZeZd(dZd)d Z d*d+Z!d,dZ"d-dZ#d.dZ$d/d Z%d0d Z&d1d Z'd2d Z(dS)5z!Event loop and event loop policy.AbstractEventLoopPolicyAbstractEventLoopAbstractServerHandle TimerHandleget_event_loop_policyset_event_loop_policyget_event_loopset_event_loopnew_event_loopget_child_watcherset_child_watcher_set_running_loop_get_running_loopN)compat) constantscCsttjrtj|}nt|dr"|j}tj|r>|j}|j|j fSt |t j rTt |jStjrpt |t jrpt |jSdS)N __wrapped__)rZPY34inspectZunwraphasattrrZ isfunction__code__ co_filenameco_firstlineno isinstance functoolspartial_get_function_sourcefunc partialmethod)rcoder /usr/lib64/python3.6/events.pyrs       rcCsJg}|r|jdd|D|r8|jdd|jDddj|dS)zFormat function arguments and keyword arguments. Special case for a single parameter: ('hello',) is formatted as ('hello'). css|]}tj|VqdS)N)reprlibrepr).0argr r r! 1sz*_format_args_and_kwargs..css$|]\}}dj|tj|VqdS)z{}={}N)formatr"r#)r$kvr r r!r&3s(z, ))extenditemsjoin)argskwargsr-r r r!_format_args_and_kwargs)s r1cCst|tjr.t|||}t|j|j|j|St|drF|j rF|j }n t|dr^|j r^|j }nt |}|t||7}|r||7}|S)N __qualname____name__) rrrr1_format_callbackrr/keywordsrr3r4r#)rr/r0suffix func_reprr r r!r58s r5cCs(t||d}t|}|r$|d|7}|S)Nz at %s:%s)r5r)rr/r8sourcer r r!_format_callback_sourceIs   r:cCsD|dkrtjj}|dkr tj}tjjtj||dd}|j |S)zlReplacement for traceback.extract_stack() that only does the necessary work for asyncio debug mode. NF)limit lookup_lines) sys _getframef_backrZDEBUG_STACK_DEPTH traceback StackSummaryextract walk_stackreverse)fr;stackr r r! extract_stackQs rGc@s<eZdZdZdZd d Zd d Zd dZddZddZ dS)rz1Object returned by callback registration methods. _callback_args _cancelled_loop_source_traceback_repr __weakref__cCsD||_||_||_d|_d|_|jjr:ttjd|_ nd|_ dS)NFr) rKrHrIrJrM get_debugrGr=r>rL)selfcallbackr/loopr r r!__init__hs zHandle.__init__cCsf|jjg}|jr|jd|jdk r8|jt|j|j|jrb|jd}|jd|d|df|S)NZ cancelledrzcreated at %s:%sr) __class__r4rJappendrHr:rIrL)rPinfoframer r r! _repr_infoss    zHandle._repr_infocCs&|jdk r|jS|j}ddj|S)Nz<%s> )rMrYr.)rPrWr r r!__repr__~s zHandle.__repr__cCs0|js,d|_|jjr t||_d|_d|_dS)NT)rJrKrOr#rMrHrI)rPr r r!cancels   z Handle.cancelcCs|y|j|jWnbtk rr}zFt|j|j}dj|}|||d}|jrV|j|d<|jj|WYdd}~XnXd}dS)NzException in callback {})messageZ exceptionhandleZsource_traceback)rHrI Exceptionr:r'rLrKcall_exception_handler)rPexccbmsgcontextr r r!_runs  z Handle._runN)rHrIrJrKrLrMrN) r4 __module__r3__doc__ __slots__rSrYr[r\rer r r r!rbs   csxeZdZdZddgZfddZfddZdd Zd d Zd d Z ddZ ddZ ddZ ddZ fddZZS)rz7Object returned by timed callback registration methods. _scheduled_whencs:|dk s ttj||||jr*|jd=||_d|_dS)NrFrT)AssertionErrorsuperrSrLrjri)rPwhenrQr/rR)rUr r!rSs  zTimerHandle.__init__cs.tj}|jrdnd}|j|d|j|S)Nrzwhen=%s)rlrYrJinsertrj)rPrWpos)rUr r!rYs zTimerHandle._repr_infocCs t|jS)N)hashrj)rPr r r!__hash__szTimerHandle.__hash__cCs |j|jkS)N)rj)rPotherr r r!__lt__szTimerHandle.__lt__cCs|j|jkrdS|j|S)NT)rj__eq__)rPrsr r r!__le__s zTimerHandle.__le__cCs |j|jkS)N)rj)rPrsr r r!__gt__szTimerHandle.__gt__cCs|j|jkrdS|j|S)NT)rjru)rPrsr r r!__ge__s zTimerHandle.__ge__cCs>t|tr:|j|jko8|j|jko8|j|jko8|j|jkStS)N)rrrjrHrIrJNotImplemented)rPrsr r r!rus      zTimerHandle.__eq__cCs|j|}|tkrtS| S)N)rury)rPrsZequalr r r!__ne__s zTimerHandle.__ne__cs |js|jj|tjdS)N)rJrK_timer_handle_cancelledrlr\)rP)rUr r!r\s zTimerHandle.cancel)r4rfr3rgrhrSrYrrrtrvrwrxrurzr\ __classcell__r r )rUr!rs  c@s eZdZdZddZddZdS)rz,Abstract server returned by create_server().cCstS)z5Stop serving. This leaves existing connections open.)ry)rPr r r!closeszAbstractServer.closecCstS)z*Coroutine to wait until service is closed.)ry)rPr r r! wait_closedszAbstractServer.wait_closedN)r4rfr3rgr}r~r r r r!rsc @seZdZdZddZddZddZdd Zd d Zd d Z ddZ ddZ ddZ ddZ ddZddZddZddZddZd d!Zd"d#Zd$d$d$d$d%d&d'Zdhd(d)Zdid*d$d$d$d*d*d*d+d,d-Zdjejejd*d.d*d*d*d/d0d1Zd*d*d*d2d3d4Zd*d.d*d5d6d7Zdkd$d$d$d*d*d*d*d8d9d:Zd;d<Zd=d>Z e!j"e!j"e!j"d?d@dAZ#e!j"e!j"e!j"d?dBdCZ$dDdEZ%dFdGZ&dHdIZ'dJdKZ(dLdMZ)dNdOZ*dPdQZ+dRdSZ,dTdUZ-dVdWZ.dXdYZ/dZd[Z0d\d]Z1d^d_Z2d`daZ3dbdcZ4dddeZ5dfdgZ6d*S)lrzAbstract event loop.cCstdS)z*Run the event loop until stop() is called.N)NotImplementedError)rPr r r! run_foreverszAbstractEventLoop.run_forevercCstdS)zpRun the event loop until a Future is done. Return the Future's result, or raise its exception. N)r)rPZfuturer r r!run_until_completesz$AbstractEventLoop.run_until_completecCstdS)zStop the event loop as soon as reasonable. Exactly how soon that is may depend on the implementation, but no more I/O callbacks should be scheduled. N)r)rPr r r!stopszAbstractEventLoop.stopcCstdS)z3Return whether the event loop is currently running.N)r)rPr r r! is_runningszAbstractEventLoop.is_runningcCstdS)z*Returns True if the event loop was closed.N)r)rPr r r! is_closedszAbstractEventLoop.is_closedcCstdS)zClose the loop. The loop should not be running. This is idempotent and irreversible. No other methods should be called after this one. N)r)rPr r r!r}s zAbstractEventLoop.closecCstdS)z,Shutdown all active asynchronous generators.N)r)rPr r r!shutdown_asyncgenssz$AbstractEventLoop.shutdown_asyncgenscCstdS)z3Notification that a TimerHandle has been cancelled.N)r)rPr^r r r!r{sz)AbstractEventLoop._timer_handle_cancelledcGs|jd|f|S)Nr) call_later)rPrQr/r r r! call_soonszAbstractEventLoop.call_sooncGstdS)N)r)rPZdelayrQr/r r r!rszAbstractEventLoop.call_latercGstdS)N)r)rPrmrQr/r r r!call_atszAbstractEventLoop.call_atcCstdS)N)r)rPr r r!time"szAbstractEventLoop.timecCstdS)N)r)rPr r r! create_future%szAbstractEventLoop.create_futurecCstdS)N)r)rPcoror r r! create_task*szAbstractEventLoop.create_taskcGstdS)N)r)rPrQr/r r r!call_soon_threadsafe/sz&AbstractEventLoop.call_soon_threadsafecGstdS)N)r)rPexecutorrr/r r r!run_in_executor2sz!AbstractEventLoop.run_in_executorcCstdS)N)r)rPrr r r!set_default_executor5sz&AbstractEventLoop.set_default_executorr)familytypeprotoflagscCstdS)N)r)rPhostportrrrrr r r! getaddrinfo:szAbstractEventLoop.getaddrinfocCstdS)N)r)rPZsockaddrrr r r! getnameinfo=szAbstractEventLoop.getnameinfoN)sslrrrsock local_addrserver_hostnamec CstdS)N)r) rPprotocol_factoryrrrrrrrrrr r r!create_connection@sz#AbstractEventLoop.create_connectiond)rrrbacklogr reuse_address reuse_portc CstdS)aA coroutine which creates a TCP server bound to host and port. The return value is a Server object which can be used to stop the service. If host is an empty string or None all interfaces are assumed and a list of multiple sockets will be returned (most likely one for IPv4 and another one for IPv6). The host parameter can also be a sequence (e.g. list) of hosts to bind to. family can be set to either AF_INET or AF_INET6 to force the socket to use IPv4 or IPv6. If not set it will be determined from host (defaults to AF_UNSPEC). flags is a bitmask for getaddrinfo(). sock can optionally be specified in order to use a preexisting socket object. backlog is the maximum number of queued connections passed to listen() (defaults to 100). ssl can be set to an SSLContext to enable SSL over the accepted connections. reuse_address tells the kernel to reuse a local socket in TIME_WAIT state, without waiting for its natural timeout to expire. If not specified will automatically be set to True on UNIX. reuse_port tells the kernel to allow this endpoint to be bound to the same port as other existing endpoints are bound to, so long as they all set this flag when being created. This option is not supported on Windows. N)r) rPrrrrrrrrrrr r r! create_serverEs'zAbstractEventLoop.create_server)rrrcCstdS)N)r)rPrpathrrrr r r!create_unix_connectionnsz(AbstractEventLoop.create_unix_connection)rrrcCstdS)a#A coroutine which creates a UNIX Domain Socket server. The return value is a Server object, which can be used to stop the service. path is a str, representing a file systsem path to bind the server socket to. sock can optionally be specified in order to use a preexisting socket object. backlog is the maximum number of queued connections passed to listen() (defaults to 100). ssl can be set to an SSLContext to enable SSL over the accepted connections. N)r)rPrrrrrr r r!create_unix_serverssz$AbstractEventLoop.create_unix_server)rrrrrallow_broadcastrc CstdS)aA coroutine which creates a datagram endpoint. This method will try to establish the endpoint in the background. When successful, the coroutine returns a (transport, protocol) pair. protocol_factory must be a callable returning a protocol instance. socket family AF_INET or socket.AF_INET6 depending on host (or family if specified), socket type SOCK_DGRAM. reuse_address tells the kernel to reuse a local socket in TIME_WAIT state, without waiting for its natural timeout to expire. If not specified it will automatically be set to True on UNIX. reuse_port tells the kernel to allow this endpoint to be bound to the same port as other existing endpoints are bound to, so long as they all set this flag when being created. This option is not supported on Windows and some UNIX's. If the :py:data:`~socket.SO_REUSEPORT` constant is not defined then this capability is unsupported. allow_broadcast tells the kernel to allow this endpoint to send messages to the broadcast address. sock can optionally be specified in order to use a preexisting socket object. N)r) rPrrZ remote_addrrrrrrrrr r r!create_datagram_endpoints!z*AbstractEventLoop.create_datagram_endpointcCstdS)aRegister read pipe in event loop. Set the pipe to non-blocking mode. protocol_factory should instantiate object with Protocol interface. pipe is a file-like object. Return pair (transport, protocol), where transport supports the ReadTransport interface.N)r)rPrpiper r r!connect_read_pipes z#AbstractEventLoop.connect_read_pipecCstdS)aRegister write pipe in event loop. protocol_factory should instantiate object with BaseProtocol interface. Pipe is file-like object already switched to nonblocking. Return pair (transport, protocol), where transport support WriteTransport interface.N)r)rPrrr r r!connect_write_pipes z$AbstractEventLoop.connect_write_pipe)stdinstdoutstderrcKstdS)N)r)rPrcmdrrrr0r r r!subprocess_shellsz"AbstractEventLoop.subprocess_shellcOstdS)N)r)rPrrrrr/r0r r r!subprocess_execsz!AbstractEventLoop.subprocess_execcGstdS)N)r)rPfdrQr/r r r! add_readerszAbstractEventLoop.add_readercCstdS)N)r)rPrr r r! remove_readerszAbstractEventLoop.remove_readercGstdS)N)r)rPrrQr/r r r! add_writerszAbstractEventLoop.add_writercCstdS)N)r)rPrr r r! remove_writerszAbstractEventLoop.remove_writercCstdS)N)r)rPrnbytesr r r! sock_recvszAbstractEventLoop.sock_recvcCstdS)N)r)rPrdatar r r! sock_sendallszAbstractEventLoop.sock_sendallcCstdS)N)r)rPrZaddressr r r! sock_connectszAbstractEventLoop.sock_connectcCstdS)N)r)rPrr r r! sock_acceptszAbstractEventLoop.sock_acceptcGstdS)N)r)rPsigrQr/r r r!add_signal_handlersz$AbstractEventLoop.add_signal_handlercCstdS)N)r)rPrr r r!remove_signal_handlersz'AbstractEventLoop.remove_signal_handlercCstdS)N)r)rPfactoryr r r!set_task_factorysz"AbstractEventLoop.set_task_factorycCstdS)N)r)rPr r r!get_task_factorysz"AbstractEventLoop.get_task_factorycCstdS)N)r)rPr r r!get_exception_handlersz'AbstractEventLoop.get_exception_handlercCstdS)N)r)rPZhandlerr r r!set_exception_handlersz'AbstractEventLoop.set_exception_handlercCstdS)N)r)rPrdr r r!default_exception_handlersz+AbstractEventLoop.default_exception_handlercCstdS)N)r)rPrdr r r!r` sz(AbstractEventLoop.call_exception_handlercCstdS)N)r)rPr r r!rOszAbstractEventLoop.get_debugcCstdS)N)r)rPZenabledr r r! set_debugszAbstractEventLoop.set_debug)r)NN)NN)NN)7r4rfr3rgrrrrrr}rr{rrrrrrrrrrrrsocketZ AF_UNSPECZ AI_PASSIVErrrrrr subprocessPIPErrrrrrrrrrrrrrrrrr`rOrr r r r!rst   '!   c@s8eZdZdZddZddZddZdd Zd d Zd S) rz-Abstract policy for accessing the event loop.cCstdS)a:Get the event loop for the current context. Returns an event loop object implementing the BaseEventLoop interface, or raises an exception in case no event loop has been set for the current context and the current policy does not specify to create one. It should never return None.N)r)rPr r r!rsz&AbstractEventLoopPolicy.get_event_loopcCstdS)z3Set the event loop for the current context to loop.N)r)rPrRr r r!r $sz&AbstractEventLoopPolicy.set_event_loopcCstdS)zCreate and return a new event loop object according to this policy's rules. If there's need to set this loop as the event loop for the current context, set_event_loop must be called explicitly.N)r)rPr r r!r (sz&AbstractEventLoopPolicy.new_event_loopcCstdS)z$Get the watcher for child processes.N)r)rPr r r!r 0sz)AbstractEventLoopPolicy.get_child_watchercCstdS)z$Set the watcher for child processes.N)r)rPwatcherr r r!r 4sz)AbstractEventLoopPolicy.set_child_watcherN) r4rfr3rgrr r r r r r r r!rs  c@sFeZdZdZdZGdddejZddZddZ d d Z d d Z dS) BaseDefaultEventLoopPolicyaDefault policy implementation for accessing the event loop. In this policy, each thread has its own event loop. However, we only automatically create an event loop by default for the main thread; other threads by default have no event loop. Other policies may have different rules (e.g. a single global event loop, or automatically creating an event loop per thread, or using some other notion of context to which an event loop is associated). Nc@seZdZdZdZdS)z!BaseDefaultEventLoopPolicy._LocalNF)r4rfr3rK _set_calledr r r r!_LocalHsrcCs|j|_dS)N)r_local)rPr r r!rSLsz#BaseDefaultEventLoopPolicy.__init__cCsZ|jjdkr4|jj r4ttjtjr4|j|j|jjdkrRt dtjj |jjS)zSGet the event loop. This may be None or an instance of EventLoop. Nz,There is no current event loop in thread %r.) rrKrr threadingZcurrent_threadZ _MainThreadr r RuntimeErrorname)rPr r r!rOs   z)BaseDefaultEventLoopPolicy.get_event_loopcCs*d|j_|dkst|tst||j_dS)zSet the event loop.TN)rrrrrkrK)rPrRr r r!r ]sz)BaseDefaultEventLoopPolicy.set_event_loopcCs|jS)zvCreate a new event loop. You must call set_event_loop() to make this the current event loop. ) _loop_factory)rPr r r!r csz)BaseDefaultEventLoopPolicy.new_event_loop) r4rfr3rgrrlocalrrSrr r r r r r!r9s rc@seZdZdZdS) _RunningLoopN)NN)r4rfr3loop_pidr r r r!rwsrcCs&tj\}}|dk r"|tjkr"|SdS)zReturn the running event loop or None. This is a low-level function intended to be used by event loops. This function is thread-specific. N) _running_looprosgetpid)Z running_looppidr r r!r~s cCs|tjft_dS)zSet the running event loop. This is a low-level function intended to be used by event loops. This function is thread-specific. N)rrrr)rRr r r!r sc Cs.t tdkr ddlm}|aWdQRXdS)Nr)DefaultEventLoopPolicy)_lock_event_loop_policyr2r)rr r r!_init_event_loop_policys rcCstdkrttS)z"Get the current event loop policy.N)rrr r r r!rscCs|dkst|tst|adS)zZSet the current event loop policy. If policy is None, the default policy is restored.N)rrrkr)Zpolicyr r r!rscCst}|dk r|StjS)aGReturn an asyncio event loop. When called from a coroutine or a callback (e.g. scheduled with call_soon or similar API), this function will always return the running event loop. If there is no running event loop set, the function will return the result of `get_event_loop_policy().get_event_loop()` call. N)rrr)Z current_loopr r r!rs cCstj|dS)zCEquivalent to calling get_event_loop_policy().set_event_loop(loop).N)rr )rRr r r!r scCs tjS)z?Equivalent to calling get_event_loop_policy().new_event_loop().)rr r r r r!r scCs tjS)zBEquivalent to calling get_event_loop_policy().get_child_watcher().)rr r r r r!r scCs tj|S)zMEquivalent to calling get_event_loop_policy().set_child_watcher(watcher).)rr )rr r r!r s)r2)NN))rg__all__rrrr"rrr=rr@r2rrrr1r5r:rGrrrrrrrZLockrrrrrr rrrrr r r r r r r r!sZ    >8 5"7