
    Yf0                    (   d Z ddlmZ ddlmZmZ ddlmZmZ ddl	m
Z
 ddlmZmZ ddlmZ ddlmZ dd	lmZ dd
lmZmZmZ ddlmZmZ ddlmZ ddlmZ  e            Z d Z!ddZ" ee           G d dej#                              Z$d Z%dgZ&dS ) a  
Threaded select reactor

The threadedselectreactor is a specialized reactor for integrating with
arbitrary foreign event loop, such as those you find in GUI toolkits.

There are three things you'll need to do to use this reactor.

Install the reactor at the beginning of your program, before importing the rest
of Twisted::

    | from twisted.internet import _threadedselect
    | _threadedselect.install()

Interleave this reactor with your foreign event loop, at some point after your
event loop is initialized::

    | from twisted.internet import reactor
    | reactor.interleave(foreignEventLoopWakerFunction)
    | self.addSystemEventTrigger('after', 'shutdown', foreignEventLoopStop)

Instead of shutting down the foreign event loop directly, shut down the
reactor::

    | from twisted.internet import reactor
    | reactor.stop()

In order for Twisted to do its work in the main thread (the thread that
interleave is called from), a waker function is necessary.  The waker function
will be called from a "background" thread with one argument: func.  The waker
function's purpose is to call func() from the main thread.  Many GUI toolkits
ship with appropriate waker functions.  One example of this is wxPython's
wx.callAfter (may be wxCallAfter in older versions of wxPython).  These would
be used in place of "foreignEventLoopWakerFunction" in the above example.

The other integration point at which the foreign event loop and this reactor
must integrate is shutdown.  In order to ensure clean shutdown of Twisted, you
must allow for Twisted to come to a complete stop before quitting the
application.  Typically, you will do this by setting up an after shutdown
trigger to stop your foreign event loop, and call reactor.stop() where you
would normally have initiated the shutdown procedure for the foreign event
loop.  Shutdown functions that could be used in place of "foreignEventloopStop"
would be the ExitMainLoop method of the wxApp instance with wxPython.
    )annotations)EBADFEINTR)EmptyQueue)Thread)AnyCallable)implementer)ThreadWorker)	posixbase)IReactorFDSetIReadDescriptorIWriteDescriptor)_preenDescriptors_select)Logger)callWithLoggerc                    | N )es    R/var/www/html/env/lib/python3.11/site-packages/twisted/internet/_threadedselect.pyraiseExceptionr   D   s    
G    timeoutfloat | Nonereadmapdict[int, IReadDescriptor]writemapdict[int, IWriteDescriptor]handleResulteCallable[[list[int], list[int], dict[int, IReadDescriptor], dict[int, IWriteDescriptor], bool], None]returnNonec                h   d}g }g }	 |                                 }|                                 }	 t          ||g |           }	|	\  }}}
nY# t          $ r d}Y nKt          $ r?}|j        d         t
          k    rY d}~dS |j        d         t          k    rd}Y d}~n	 d}~ww xY w ||||||           dS )z
    Invoke C{select}.  This will be called in a non-main thread, so it is very
    careful to work only on integers and avoid calling any application code.
    F   Tr   N)keysr   
ValueErrorOSErrorargsr   r   )r   r   r    r"   preenrwreadints	writeintsresultignoredses               r   _threadsafeSelectr4   H   s    & E
A
A<<>>MMOO		Xy"g>>F. #MAq'/  	 	 	 EE 	 	 	wqzU""
 u$$
 	& LAw%00000s)   A
 
B 	B !B=BBB c                       e Zd ZdZ	 d0d1dZd Zd	 Zd2dZd3dZd4dZ		 d5d6dZ
d7dZd8dZd7dZd8d Zd9d"Zd:d$Zd;d&Zd' Zd( Zd4d)Zd4 fd*Zd<d= fd-Zd>d.Zd4 fd/Z xZS )?ThreadedSelectReactorzVA threaded select() based reactor - runs on all POSIX platforms and on
    Win32.
    Nwaker+Callable[[Callable[[], None]], None] | Noner$   r%   c                    t                      | _        t                      | _        t          j                            |            d | _        || _        d | _        d S r   )	setreadswritesr   PosixReactorBase__init___selectorThread	mainWaker_iterationQueue)selfr7   s     r   r>   zThreadedSelectReactor.__init__   sN     ,/55
-0UU"++D11148AEr   c                8    | j                                          d S r   )r7   wakeUprB   s    r   rD   zThreadedSelectReactor.wakeUp   s    
r   c                b    t          j        j        | g|R i |}|                                  |S r   )r   r=   	callLaterrD   )rB   r+   kwtples       r   rG   zThreadedSelectReactor.callLater   s7    )3DF4FFF2FFr   
selectableobjectmethodstrc                    t                               d|          5 } t          ||                      }d d d            n# 1 swxY w Y   |j        x}|j        }|r|                     |||dk               d S d S )Nzwhile handling selectable {sel})seldoRead)_logfailuresHandledgetattrfailurevalue_disconnectSelectable)rB   rJ   rL   opwhyfails         r   _doReadOrWritez$ThreadedSelectReactor._doReadOrWrite   s    !!-: " 
 
 	0-'*f--//C	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 JD+*C 	L&&z3(8JKKKKK	L 	Ls   AA	Ar   r   	keepGoingboolc                R   	
 i 	i 
d j         	fd j        
ffD ]"\  }}}|D ]}|||                                <   # j        
J d            d fd j        "t          d t                                 _         j                            	
fd           d S )NTFz]neither .interleave() nor .mainLoop() / .run() called, but we are somehow running the reactorr-   	list[int]r.   r   r   r    r!   r,   r\   r$   r%   c           	     :     d fd            }d S )Nr$   r%   c                    r"t          	j        	j        	j                   d S 	j        } D ]%}|         }|	j        v rt          || |d           &
D ]%}|         }|	j        v rt          || |d           &	                                 	j        r,r*	                    		                                d           d S 	
                                 d S )NrP   doWriteT)r   r;   r<   rV   rZ   _callWithLoggerrunUntilCurrent_started_selectOncer   _cleanUpThread)_drdwreadablerselectablewritablewselectabler[   r,   r-   r   rB   r.   r    s        r   onMainThreadzSThreadedSelectReactor._selectOnce.<locals>.callReadsAndWrites.<locals>.onMainThread   s    %
DK1K   F+ ! S SH")("3K"dj00'UKRRR ! T TH"*8"4K"dk11'UKSSS$$&&&= *Y *$$T\\^^T:::::'')))))r   r$   r%   r   )	r-   r.   r   r    r,   rl   r[   r@   rB   s	   ````` r   callReadsAndWritesz=ThreadedSelectReactor._selectOnce.<locals>.callReadsAndWrites   sZ     * * * * * * * * * * * Y* * *r   c                F    t          |                                           S )Ntarget)r   startrp   s    r   <lambda>z3ThreadedSelectReactor._selectOnce.<locals>.<lambda>   s    vV444::<< r   c                 (    t                     S r   )r4   )rn   r;   r   r<   s   r   rs   z3ThreadedSelectReactor._selectOnce.<locals>.<lambda>   s    %guf>PQQ r   )r-   r^   r.   r^   r   r   r    r!   r,   r\   r$   r%   )r;   r<   filenor@   r?   r   r   do)rB   r   r[   isReadfdmapdeachrn   r@   r;   r<   s   ```    @@@@r   re   z!ThreadedSelectReactor._selectOnce   s    "!#4:u%DK(!
 	( 	(FE1  ( (#'$++--  ( N	$$5 %$$
	* 	* 	* 	* 	* 	* 	* 	*B '#/<<egg$ $D  	QQQQQQQ	
 	
 	
 	
 	
r   c                T    | j         dc}| _         ||                                 dS dS )z=
        Ensure that the selector thread is stopped.
        N)r?   quit)rB   	oldThreads     r   rf   z$ThreadedSelectReactor._cleanUpThread   s:     +/*>'	4' NN ! r   T$Callable[[Callable[[], None]], None]installSignalHandlersc                j    || _         |                     |           |                     dd           dS )at  
        interleave(waker) interleaves this reactor with the current application
        by moving the blocking parts of the reactor (select() in this case) to
        a separate thread.  This is typically useful for integration with GUI
        applications which have their own event loop already running.

        See the module docstring for more information.
                TN)r@   startRunningre   )rB   r7   r   s      r   
interleavez ThreadedSelectReactor.interleave   s<    $ /000d#####r   readerr   c                b    | j                             |           |                                  dS )z@Add a FileDescriptor for notification of data available to read.N)r;   addrD   rB   r   s     r   	addReaderzThreadedSelectReactor.addReader   s'    
vr   writerr   c                b    | j                             |           |                                  dS )zAAdd a FileDescriptor for notification of data available to write.N)r<   r   rD   rB   r   s     r   	addWriterzThreadedSelectReactor.addWriter   s'    r   c                P    || j         v r| j                             |           dS dS )z?Remove a Selectable for notification of data available to read.N)r;   remover   s     r   removeReaderz"ThreadedSelectReactor.removeReader  s3    TZJf%%%%%  r   c                P    || j         v r| j                             |           dS dS )z@Remove a Selectable for notification of data available to write.N)r<   r   r   s     r   removeWriterz"ThreadedSelectReactor.removeWriter  s3    T[  Kv&&&&& ! r   (list[IReadDescriptor | IWriteDescriptor]c                B    |                      | j        | j                  S r   )
_removeAllr;   r<   rE   s    r   	removeAllzThreadedSelectReactor.removeAll  s    tz4;777r   list[IReadDescriptor]c                *    t          | j                  S r   )listr;   rE   s    r   
getReadersz ThreadedSelectReactor.getReaders  s    DJr   list[IWriteDescriptor]c                *    t          | j                  S r   )r   r<   rE   s    r   
getWritersz ThreadedSelectReactor.getWriters  s    DK   r   c                l    t           j                            |            |                                  dS )z
        Extend the base stop implementation to also wake up the select thread so
        that C{runUntilCurrent} notices the reactor should stop.
        N)r   r=   stoprD   rE   s    r   r   zThreadedSelectReactor.stop  s,    
 	"''---r   c                l    t           j                            |            |                                  d S r   )r   r=   crashrD   rE   s    r   r   zThreadedSelectReactor.crash  s*    "((...r   c                N    t                      | _        | j        j        | _        dS )a  
        Mostly for compliance with L{IReactorCore} and usability with the
        tests, set up a fake blocking main-loop; make the "foreign" main loop
        we are interfacing with be C{self.mainLoop()}, that is reading from a
        basic Queue.
        N)r   rA   putr@   rE   s    r   _testMainLoopSetupz(ThreadedSelectReactor._testMainLoopSetup$  s!      %ww-1r   c                p    t                                                       |                                  dS )zu
        Handle uninstallation to ensure that cleanup is properly performed by
        ReactorBuilder tests.
        N)super_uninstallHandlerrf   rB   	__class__s    r   r   z'ThreadedSelectReactor._uninstallHandler.  s3    
 	!!###r   r   floatc                    | j         | j        |                                  |                                  t	                                          |           d S r   )rA   r@   r   rD   r   iterate)rB   r   r   s     r   r   zThreadedSelectReactor.iterate6  sN    'DN,B##%%%     r   c                    | j         J |                     |d           	 | j                             |          }n# t          $ r Y d S w xY w |             d S )NF)r   )rA   re   getr   )rB   r   works      r   doIterationz!ThreadedSelectReactor.doIteration<  sx    #///%(((	'++G+<<DD 	 	 	FF	s   = 
A
Ac                p    |                                   t                                                       dS )z2
        This should not normally be run.
        N)r   r   mainLoopr   s    r   r   zThreadedSelectReactor.mainLoopE  s3     	!!!r   r   )r7   r8   r$   r%   )rJ   rK   rL   rM   r$   r%   )r   r   r[   r\   r$   r%   rm   )T)r7   r~   r   r\   r$   r%   )r   r   r$   r%   )r   r   r$   r%   )r$   r   )r$   r   )r$   r   )r   )r   r   r$   r%   )r   r   r$   r%   )__name__
__module____qualname____doc__r>   rD   rG   rZ   re   rf   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   __classcell__)r   s   @r   r6   r6   ~   s        
 DHF F F F F    
L L L L7
 7
 7
 7
r    '+$ $ $ $ $,   
   
& & & &
' ' ' '
8 8 8 8       ! ! ! !    2 2 2 2     ! ! ! ! ! ! !            r   r6   c                 D    t                      } ddlm}  ||            | S )zDConfigure the twisted mainloop to be run using the select() reactor.r   )installReactor)r6   twisted.internet.mainr   )reactorr   s     r   installr   M  s4    #%%G444444N7Nr   r   N)
r   r   r   r   r    r!   r"   r#   r$   r%   )'r   
__future__r   errnor   r   queuer   r   	threadingr   typingr	   r
   zope.interfacer   twisted._threadsr   twisted.internetr   twisted.internet.interfacesr   r   r   twisted.internet.selectreactorr   r   twisted.loggerr   twisted.python.logr   rb   rQ   r   r4   r=   r6   r   __all__r   r   r   <module>r      s  
+ +X # " " " " "                                       & & & & & & ) ) ) ) ) ) & & & & & & X X X X X X X X X X E E E E E E E E ! ! ! ! ! ! @ @ @ @ @ @vxx  31 31 31 31l ]K K K K KI6 K K K\   +r   