grthtrhthjhtyjytjytkergtrhtrjytjerhrfh4:24 29/09/2026a ce×i=Bã@sòdZddlZddlZddlZddlZddlZddlmZmZm Z m Z m Z m Z e dƒZ dZdZgd¢ZGdd „d eƒZGd d „d eƒZGd d „d eƒZGdd„deƒZGdd„deƒZGdd„deƒZdae edœdd„Zdejedœdd„ZdS)aILibvirt event loop implementation using asyncio Register the implementation of default loop: import asyncio import libvirtaio async def myapp(): libvirtaio.virEventRegisterAsyncIOImpl() conn = libvirt.open("test:///default") For compatibility with Python < 3.7: loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) loop.run_until_complete(myapp()) asyncio.set_event_loop(None) loop.close() If Python >= 3.7 can be required then asyncio.run(myapp()) .. seealso:: https://libvirt.org/html/libvirt-libvirt-event.html éN)ÚAnyÚCallableÚDictÚ GeneratorÚOptionalÚTypeVarÚ_Tz,Wojtek Porczyk z LGPL-2.1+)ÚgetCurrentImplÚvirEventAsyncIOImplÚvirEventRegisterAsyncIOImplcs`eZdZdZe ¡Zdeee gdfe e e ddœ‡fdd„ Z e dœdd „Z ddœd d „Z‡ZS) ÚCallbackzÆBase class for holding callback :param virEventAsyncIOImpl impl: the implementation in which we run :param cb: the callback itself :param opaque: the opaque tuple passed by libvirt r N)ÚimplÚcbÚopaqueÚargsÚkwargsÚreturncs4tƒj|i|¤Žt|jƒ|_||_||_||_dS©N)ÚsuperÚ__init__ÚnextÚ _iden_counterÚidenr rr)Úselfr rrrr©Ú __class__©ú0/usr/lib64/python3.9/site-packages/libvirtaio.pyrPs  zCallback.__init__©rcCsd |jj|j¡S)Nz <{} iden={}>)ÚformatrÚ__name__r©rrrrÚ__repr__WszCallback.__repr__cCs(|jj d|j¡|j |j|j¡dS)zSchedule *ff* callbackz"callback %d close(), scheduling ffN)r ÚlogÚdebugrÚschedule_ff_callbackrr!rrrÚcloseZszCallback.close)r Ú __module__Ú __qualname__Ú__doc__Ú itertoolsÚcountrrÚintrrrÚstrr"r&Ú __classcell__rrrrr Es (r c@s`eZdZdZdeddœdd„Zeddœdd „Zdd œd d „Zd ddœdd„Zeddœdd„Z dS)Ú Descriptorz“Manager of one file descriptor :param virEventAsyncIOImpl impl: the implementation in which we run :param int fd: the file descriptor r N)r ÚfdrcCs||_||_i|_dSr)r r0Ú callbacks)rr r0rrrrjszDescriptor.__init__©ÚeventrcCsBt|j ¡ƒD].}|jdur|j|@r| |j|j||j¡qdS)zDispatch the event to the descriptors :param int event: The event (from libvirt's constants) being dispatched N)Úlistr1Úvaluesr3rrr0r)rr3ÚcallbackrrrÚ_handleoszDescriptor._handlercCs¬tdd„|j ¡Dƒƒr$t dt¡tdd„|j ¡DƒƒrV|jj |j |j t j ¡n|jj  |j ¡tdd„|j ¡Dƒƒr˜|jj |j |j t j¡n|jj |j ¡dS)zƒRegister or unregister callbacks at event loop This should be called after change of any ``.event`` in callbacks. css"|]}|jtjtjB@VqdSr)r3ÚlibvirtÚVIR_EVENT_HANDLE_READABLEÚVIR_EVENT_HANDLE_WRITABLE©Ú.0r6rrrÚ ƒs ýÿÿz$Descriptor.update..zTThe only event supported are VIR_EVENT_HANDLE_READABLE and VIR_EVENT_HANDLE_WRITABLEcss|]}|jtj@VqdSr)r3r8r9r;rrrr=Œsÿcss|]}|jtj@VqdSr)r3r8r:r;rrrr=“sÿN)Úanyr1r5ÚwarningsÚwarnÚ UserWarningr ÚloopZ add_readerr0r7r8r9Z remove_readerZ add_writerr:Z remove_writerr!rrrÚupdatexs* ýýÿ ÿÿ ÿzDescriptor.updateÚ FDCallback)r6rcCs||j|j<| ¡dS)z¶Add a callback to the descriptor :param FDCallback callback: the callback to add :rtype: None After adding the callback, it is immediately watched. N)r1rrC)rr6rrrÚ add_handlešs zDescriptor.add_handle)rrcCs|j |¡}| ¡|S)aRemove a callback from the descriptor :param int iden: the identifier of the callback :returns: the callback :rtype: FDCallback After removing the callback, the descriptor may be unwatched, if there are no more handles for it. )r1ÚpoprC)rrr6rrrÚ remove_handle¥s zDescriptor.remove_handle) r r'r(r)r,rr7rCrErGrrrrr/ds  " r/cs8eZdZdZdddœ‡fdd„ Zeedœdd „Z‡ZS) ÚDescriptorDictzdDescriptors collection This is used internally by virEventAsyncIOImpl to hold descriptors. r N)r rcstƒ ¡||_dSr)rrr )rr rrrr¹s zDescriptorDict.__init__)r0rcCst|j|ƒ}|||<|Sr)r/r )rr0Ú descriptorrrrÚ __missing__½s zDescriptorDict.__missing__) r r'r(r)rr,r/rJr.rrrrrH´srHcsLeZdZdZeeeeddœ‡fdd„ Zedœdd„Z edd œd d „Z ‡Z S) rDzªCallback for file descriptor (watcher) :param Descriptor descriptor: the descriptor manager :param int event: bitset of events on which to fire the callback N)rrIr3rrcs"tƒj|i|¤Ž||_||_dSr)rrrIr3)rrIr3rrrrrrËszFDCallback.__init__rcCsd |jj|j|jj|j¡S)Nz<{} iden={} fd={} event={}>)rrr rrIr0r3r!rrrr"ÐsÿzFDCallback.__repr__r2cCs||_|j ¡dS)z1Update the callback and fix descriptor's watchersN)r3rIrC)rr3rrrrCÔszFDCallback.update) r r'r(r)rr/r,rr-r"rCr.rrrrrDÃsrDcsreZdZdZeeddœ‡fdd„ Zedœdd„Zeeddfdœd d „Z e dd œd d „Z ddœ‡fdd„ Z ‡Z S)ÚTimeoutCallbackzCallback for timerN)rrrcs"tƒj|i|¤Žd|_d|_dS)Néÿÿÿÿ)rrÚtimeoutÚ_task)rrrrrrràszTimeoutCallback.__init__rcCsd |jj|j|j¡S)Nz<{} iden={} timeout={}>)rrr rrMr!rrrr"åsÿzTimeoutCallback.__repr__cÃsžzJ|jdkr8|jd}|jj d|¡t |¡IdHnt d¡IdHWn*tjyt|jj d|j¡YqšYn0| |j|j ¡|jj d|j¡qdS)zQAn actual timer running on the event loop. This is a coroutine. rgü©ñÒMbP?z sleeping %rNztimer %d cancelledztimer %r callback ended) rMr r#r$ÚasyncioÚsleepZCancelledErrorrrr©rrMrrrÚ_timerés   zTimeoutCallback._timer)rMrcCs€||_|jdkrF|jdurF|jj d|j¡tj| ¡|jj d�|_n6|jdkr||jdur||jj d|j¡|j  ¡d|_dS)z-Start or the timer, possibly updating timeoutrNztimer %r start©rBz timer %r stop) rMrNr r#r$rrOÚ ensure_futurerRrBÚcancelrQrrrrCÿs ÿ  zTimeoutCallback.updatecs|jdd�tt|ƒ ¡dS)z#Stop the timer and call ff callbackrL©rMN)rCrrKr&r!rrrr& s zTimeoutCallback.close)r r'r(r)rrr-r"rrRr,rCr&r.rrrrrKÞs rKc@seZdZdZd*ejddœdd„Zedœdd„Zddœd d „Z ddœd d „Z ddœd d„Z e e ddœdd„Ze e ddœdd„Zddœdd„Zedœdd„Ze e eje e dœdd„Ze e ddœdd„Ze e dœdd „Ze eje e d!œd"d#„Ze e dd$œd%d&„Ze e d'œd(d)„ZdS)+r z›Libvirt event adapter to asyncio. :param loop: asyncio's event loop If *loop* is not specified, the current (or default) event loop is used. N©rBrcCs>|p t ¡|_i|_t|ƒ|_t |jj ¡|_ d|_ d|_ dS)Nr) rOZget_event_looprBr1rHÚ descriptorsÚloggingZ getLoggerrr r#Ú_pendingÚ _finished)rrBrrrrs  zvirEventAsyncIOImpl.__init__rcCsd t|ƒj|j|j¡S)Nz <{} callbacks={} descriptors={}>)rÚtyper r1rXr!rrrr"+sÿzvirEventAsyncIOImpl.__repr__cCs&|jd7_|jdur"|j ¡dS)z;Increase the count of pending affairs. Do not use directly.éN)rZr[Úclearr!rrrÚ _pending_inc/s z virEventAsyncIOImpl._pending_inccCs0|jd8_|jdkr,|jdur,|j ¡dS)z;Decrease the count of pending affairs. Do not use directly.r]rN)rZr[Úsetr!rrrÚ _pending_dec5sz virEventAsyncIOImpl._pending_deccCs0|j d¡t |j|j|j|j|j|j ¡|S)z3Register this instance as event loop implementationz register()) r#r$r8ZvirEventRegisterImplÚ _add_handleÚ_update_handleÚ_remove_handleÚ _add_timeoutÚ_update_timeoutÚ_remove_timeoutr!rrrÚregister<s    þzvirEventAsyncIOImpl.register)rrrcCstj| ||¡|jd�dS)z8Schedule a ff callback from one of the handles or timersrSN)rOrTÚ _ff_callbackrB©rrrrrrr%Esz(virEventAsyncIOImpl.schedule_ff_callbackcÃs$|j d|¡t |¡| ¡dS)zFDirectly free the opaque object This is a coroutine. z ff_callback(iden=%d, opaque=...)N)r#r$r8ZvirEventInvokeFreeCallbackrarjrrrriIs z virEventAsyncIOImpl._ff_callbackcÃsB|j d¡|jr2t ¡|_|j ¡IdHd|_|j d¡dS)zRWait for the implementation to become idle. This is a coroutine. zdrain()Nz drain ended)r#r$rZrOÚEventr[Úwaitr!rrrÚdrainRs   zvirEventAsyncIOImpl.draincCs|j o|j S)zÓReturns False if there are leftovers from a connection Those may happen if there are sematical problems while closing a connection. For example, not deregistered events before .close(). )r1rZr!rrrÚis_idle`szvirEventAsyncIOImpl.is_idle)r0r3rrrcCsVt||||j||d�}|j d|||j¡||j|j<|j| |¡| ¡|jS)aRegister a callback for monitoring file handle events :param int fd: file descriptor to listen on :param int event: bitset of events on which to fire the callback :param cb: the callback to be called when an event occurrs :param opaque: user data to pass to the callback :rtype: int :returns: handle watch number to be used for updating and unregistering for events .. seealso:: https://libvirt.org/html/libvirt-libvirt-event.html#virEventAddHandleFuncFunc )rIr3z4add_handle(fd=%d, event=%d, cb=..., opaque=...) = %d)rDrXr#r$rr1rEr_)rr0r3rrr6rrrrbhs  ÿÿ zvirEventAsyncIOImpl._add_handle)Úwatchr3rcCs*|j d||¡|j|}|j|d�dS)aChange event set for a monitored file handle :param int watch: file descriptor watch to modify :param int event: new events to listen on .. seealso:: https://libvirt.org/html/libvirt-libvirt-event.html#virEventUpdateHandleFunc z!update_handle(watch=%d, event=%d))r3N©r#r$r1rC)rror3r6rrrrc€s  z"virEventAsyncIOImpl._update_handle)rorc CsŠ|j d|¡z|j |¡}Wn:tyX}z"|j d|jd¡WYd}~dSd}~00|jj}t |j |jƒdkr~|j |=|  ¡dS)aUnregister a callback from a file handle. :param int watch: file descriptor watch to stop listening on :returns: -1 on error, 0 on success .. seealso:: https://libvirt.org/html/libvirt-libvirt-event.html#virEventRemoveHandleFunc zremove_handle(watch=%d)z#remove_handle(): no such handle: %rrNrL) r#r$r1rFÚKeyErrorÚwarningrrIr0ÚlenrXr&)rror6Úerrr0rrrrdŽs z"virEventAsyncIOImpl._remove_handle)rMrrrcCsDt|||ƒ}|j d||j¡||j|j<|j|d�| ¡|jS)a}Register a callback for a timer event :param int timeout: the timeout to monitor :param cb: the callback to call when timeout has expired :param opaque: user data to pass to the callback :rtype: int :returns: a timer value .. seealso:: https://libvirt.org/html/libvirt-libvirt-event.html#virEventAddTimeoutFunc z0add_timeout(timeout=%d, cb=..., opaque=...) = %drV)rKr#r$rr1rCr_)rrMrrr6rrrre¥s ÿ  z virEventAsyncIOImpl._add_timeout)ÚtimerrMrcCs*|j d||¡|j|}|j|d�dS)züChange frequency for a timer :param int timer: the timer to modify :param int timeout: the new timeout value in ms .. seealso:: https://libvirt.org/html/libvirt-libvirt-event.html#virEventUpdateTimeoutFunc z$update_timeout(timer=%d, timeout=%d)rVNrp)rrurMr6rrrrf»s  z#virEventAsyncIOImpl._update_timeout)rurc Csf|j d|¡z|j |¡}Wn:tyX}z"|j d|jd¡WYd}~dSd}~00| ¡dS)zõUnregister a callback for a timer :param int timer: the timer to remove :returns: -1 on error, 0 on success .. seealso:: https://libvirt.org/html/libvirt-libvirt-event.html#virEventRemoveTimeoutFunc zremove_timeout(timer=%d)z%remove_timeout(): no such timeout: %rrNrL)r#r$r1rFrqrrrr&)rrur6rtrrrrgÉs z#virEventAsyncIOImpl._remove_timeout)N)r r'r(r)rOÚAbstractEventLooprr-r"r_rarhr,rr%rirmÚboolrnr8Z_EventCBrbrcrdZ_TimerCBrerfrgrrrrr s    r rcCstS)z@Return the current implementation, or None if not yet registered)Ú _current_implrrrrr ßsr rWcCst|d� ¡atS)z¬Arrange for libvirt's callbacks to be dispatched via asyncio event loop The implementation object is returned, but in normal usage it can safely be discarded. rS)r rhrxrSrrrr äsr )N)r)rOr*rYr?r8ÚtypingrrrrrrrÚ __author__Z __license__Ú__all__Úobjectr r/ÚdictrHrDrKr rxr rvr rrrrÚs( P9F