ó
    Ö›‘h�0  ã                  ó.  • S r SSKJr  SSKJrJr  SSKJrJr  SSK	J
r
  SSKJrJr  SSKJr  SSKJr  SS	KJr  SS
KJrJrJr  SSKJrJr  SSKJr  SSKJr  \" 5       r S r!          SS jr"\" \5       " S S\RF                  5      5       r$S r%S/r&g)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                ó   • U e©N© )Úes    ÚdC:\Users\psant\Downloads\Noiseback\school\venv\Lib\site-packages\twisted/internet/_threadedselect.pyÚraiseExceptionr   D   s   € Ø
€Gó    c                óF  • Sn/ n/ n UR                  5       nUR                  5       n [        Xx/ U 5      n	U	u  pVn
 U" XVXU5        g! [         a    Sn M  [         aA  nUR                  S   [
        :X  a   SnAgUR                  S   [        :X  a  Sn SnAM^  e SnAff = f)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.
FTr   N)Úkeysr   Ú
ValueErrorÚOSErrorÚargsr   r   )ÚtimeoutÚreadmapÚwritemapÚhandleResultÚpreenÚrÚwÚreadintsÚ	writeintsÚresultÚignoredÚses               r   Ú_threadsafeSelectr-   H   s¯   € ð& €EØ
€AØ
€AØ
Ø—<‘<“>ˆØ—M‘M“Oˆ	ð	Ü˜X°"°gÓ>ˆFð. #‰MˆA�'ØÙ��w¨%Õ0øô1 ó 	ð ˆEÚÜó 	à�w‰w�q‰zœUÓ"ô
 Ø—‘˜‘œuÓ$Ø�Ýð
 ûð	ús)   ©A ÁB Á	B ÁBÁ;BÂBÂB c                  ó  ^ • \ rS rSrSr S   SS jjrS rS rSS jrSS jr	SS jr
 S     SS	 jjrS S
 jrS!S jrS S jrS!S jrS"S jrS#S jrS$S jrS rS rSS jrSU 4S jjrS%S&U 4S jjjrS'S jrSU 4S jjrSrU =r$ )(ÚThreadedSelectReactoré~   zNA threaded select() based reactor - runs on all POSIX platforms and on
Win32.
c                ó¦   • [        5       U l        [        5       U l        [        R                  R                  U 5        S U l        Xl        S U l        g r   )	ÚsetÚreadsÚwritesr   ÚPosixReactorBaseÚ__init__Ú_selectorThreadÚ	mainWakerÚ_iterationQueue)ÚselfÚwakers     r   r6   ÚThreadedSelectReactor.__init__„   s?   € ô ,/«5ˆŒ
Ü-0«UˆŒÜ×"Ñ"×+Ñ+¨DÔ1Ø48ˆÔØŒØAEˆÕr   c                ó8   • U R                   R                  5         g r   )r;   ÚwakeUp©r:   s    r   r>   ÚThreadedSelectReactor.wakeUpŽ   s   € à�
‰
×ÑÕr   c                ón   • [         R                  R                  " U /UQ70 UD6nU R                  5         U$ r   )r   r5   Ú	callLaterr>   )r:   r    ÚkwÚtples       r   rB   ÚThreadedSelectReactor.callLater’   s0   € Ü×)Ñ)×3Ò3°DÐF¸4ÒFÀ2ÑFˆØ�‰ŒØˆr   c                óð   • [         R                  SUS9 n[        X5      " 5       nS S S 5        WR                  =nb  UR                  nW(       a  U R                  XUS:H  5        g g ! , (       d  f       NG= f)Nzwhile handling selectable {sel})ÚselÚdoRead)Ú_logÚfailuresHandledÚgetattrÚfailureÚvalueÚ_disconnectSelectable)r:   Ú
selectableÚmethodÚopÚwhyÚfails         r   Ú_doReadOrWriteÚ$ThreadedSelectReactor._doReadOrWrite—   sq   € Ü×!Ñ!Ø-°:ð "ñ 
àÜ˜*Ô-Ó/ˆC÷
ð —J‘JÐˆDÑ+Ø—*‘*ˆCÞØ×&Ñ& z¸À(Ñ8JÕKð ÷
õ 
ús   •A'Á'
A5c                óŠ  ^ ^^^^^	^
• 0 m	0 m
ST R                   T	4ST R                  T
44 H!  u  p4nU H  nXeUR                  5       '   M     M#     T R                  mTc   S5       e            SUUU 4S jjmT R                  c  [        S [        5       5      T l        T R                  R                  UU	UU
4S j5        g )NTFz]neither .interleave() nor .mainLoop() / .run() called, but we are somehow running the reactorc           	     ó6   >^ ^^^^• TSUUU UUUU4S jj5       ng )Nc                 óê  >• T(       a,  [        T	R                  T	R                  T	R                  5        g T	R                  n T H'  nTU   nUT	R                  ;   d  M  [        X US5        M)     T
 H'  nTU   nUT	R                  ;   d  M  [        X@US5        M)     T	R                  5         T	R                  (       a(  T(       a!  T	R                  T	R                  5       S5        g T	R                  5         g )NrH   ÚdoWriteT)r   r3   r4   rN   rT   Ú_callWithLoggerÚrunUntilCurrentÚ_startedÚ_selectOncer!   Ú_cleanUpThread)Ú_drdwÚreadableÚrselectableÚwritableÚwselectableÚ	keepGoingr%   r&   r"   r:   r'   r#   s        €€€€€€€r   ÚonMainThreadÚSThreadedSelectReactor._selectOnce.<locals>.callReadsAndWrites.<locals>.onMainThread¸   sÆ   ø€ æÜ%ØŸ
™
 D§K¡K°×1KÑ1Kôð Ø×+Ñ+�ã !�HØ")¨(Ñ"3�KØ" d§j¡jÕ0Ü'¨¸KÈÖRñ !"ó
 !"�HØ"*¨8Ñ"4�KØ" d§k¡kÕ1Ü'¨¸KÈÖSñ !"ð
 ×$Ñ$Ô&Ø—=—=¦Yà×$Ñ$ T§\¡\£^°TÕ:à×'Ñ'Õ)r   ©ÚreturnÚNoner   )	r&   r'   r"   r#   r%   re   rd   r8   r:   s	   ````` €€€r   ÚcallReadsAndWritesÚ=ThreadedSelectReactor._selectOnce.<locals>.callReadsAndWrites±   s   ý€ ð ÷*ô *ó ñ*r   c                ó0   • [        U S9R                  5       $ )N©Útarget)r   Ústartrm   s    r   Ú<lambda>Ú3ThreadedSelectReactor._selectOnce.<locals>.<lambda>Ô   s   € œv¨VÑ4×:Ñ:Ô<r   c                 ó    >• [        TTTT 5      $ r   )r-   )rj   r3   r!   r4   s   €€€€r   rp   rq   ×   s   ø€ Ô% g¨u°fÐ>PÔQr   )r&   ú	list[int]r'   rs   r"   údict[int, IReadDescriptor]r#   údict[int, IWriteDescriptor]r%   Úboolrh   ri   )r3   r4   Úfilenor8   r7   r   r   Údo)r:   r!   rd   ÚisReadÚfdmapÚdÚeachrj   r8   r3   r4   s   ```    @@@@r   r]   Ú!ThreadedSelectReactor._selectOnce¡   sõ   þ€ Ø "ˆØ!#ˆà�4—:‘:˜uÐ%Ø�D—K‘K Ð(ó!
ÑˆF˜1ó �Ø#'�$—+‘+“-Ó ó ñ	!
ð —N‘Nˆ	ØÑ$ð 	
ð5ó	
Ð$ð
	*Øð	*àð	*ð 0ð	*ð 2ð		*ð
 ð	*ð ÷	*ñ 	*ðB ×ÑÑ'Ü#/Ù<¼e»gó$ˆDÔ ð 	×Ñ×ÑßQõ	
r   c                óR   • U R                   Ssol         Ub  UR                  5         gg)z-
Ensure that the selector thread is stopped.
N)r7   Úquit)r:   Ú	oldThreads     r   r^   Ú$ThreadedSelectReactor._cleanUpThreadÚ   s,   € ð +/×*>Ñ*>ÀÐ'ˆ	Ô'ØÑ Ø�N‰NÕð !r   c                óV   • Xl         U R                  U5        U R                  SS5        g)aD  
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)r8   ÚstartRunningr]   )r:   r;   ÚinstallSignalHandlerss      r   Ú
interleaveÚ ThreadedSelectReactor.interleaveâ   s(   € ð$ ŒØ×ÑÐ/Ô0Ø×Ñ˜˜dÕ#r   c                óZ   • U R                   R                  U5        U R                  5         g)z@Add a FileDescriptor for notification of data available to read.N)r3   Úaddr>   ©r:   Úreaders     r   Ú	addReaderÚThreadedSelectReactor.addReaderø   s   € à�
‰
�‰�vÔØ�‰�r   c                óZ   • U R                   R                  U5        U R                  5         g)zAAdd a FileDescriptor for notification of data available to write.N)r4   r‰   r>   ©r:   Úwriters     r   Ú	addWriterÚThreadedSelectReactor.addWriterý   s   € à�‰�‰˜ÔØ�‰�r   c                óZ   • XR                   ;   a  U R                   R                  U5        gg)z?Remove a Selectable for notification of data available to read.N)r3   ÚremoverŠ   s     r   ÚremoveReaderÚ"ThreadedSelectReactor.removeReader  s#   € à—Z‘ZÓØ�J‰J×Ñ˜fÕ%ð  r   c                óZ   • XR                   ;   a  U R                   R                  U5        gg)z@Remove a Selectable for notification of data available to write.N)r4   r”   r�   s     r   ÚremoveWriterÚ"ThreadedSelectReactor.removeWriter  s#   € à—[‘[Ó Ø�K‰K×Ñ˜vÕ&ð !r   c                óN   • U R                  U R                  U R                  5      $ r   )Ú
_removeAllr3   r4   r?   s    r   Ú	removeAllÚThreadedSelectReactor.removeAll  s   € Ø�‰˜tŸz™z¨4¯;©;Ó7Ð7r   c                ó,   • [        U R                  5      $ r   )Úlistr3   r?   s    r   Ú
getReadersÚ ThreadedSelectReactor.getReaders  s   € Ü�D—J‘JÓÐr   c                ó,   • [        U R                  5      $ r   )rŸ   r4   r?   s    r   Ú
getWritersÚ ThreadedSelectReactor.getWriters  s   € Ü�D—K‘KÓ Ð r   c                ób   • [         R                  R                  U 5        U R                  5         g)zƒ
Extend the base stop implementation to also wake up the select thread so
that C{runUntilCurrent} notices the reactor should stop.
N)r   r5   Ústopr>   r?   s    r   r¦   ÚThreadedSelectReactor.stop  s!   € ô
 	×"Ñ"×'Ñ'¨Ô-Ø�‰�r   c                ób   • [         R                  R                  U 5        U R                  5         g r   )r   r5   Úcrashr>   r?   s    r   r©   ÚThreadedSelectReactor.crash  s   € Ü×"Ñ"×(Ñ(¨Ô.Ø�‰�r   c                óX   • [        5       U l        U R                  R                  U l        g)zÜ
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   r9   Úputr8   r?   s    r   Ú_testMainLoopSetupÚ(ThreadedSelectReactor._testMainLoopSetup$  s!   € ô  %›wˆÔØ×-Ñ-×1Ñ1ˆ�r   c                óB   >• [         TU ]  5         U R                  5         g)z]
Handle uninstallation to ensure that cleanup is properly performed by
ReactorBuilder tests.
N)ÚsuperÚ_uninstallHandlerr^   ©r:   Ú	__class__s    €r   r±   Ú'ThreadedSelectReactor._uninstallHandler.  s   ø€ ô
 	‰Ñ!Ô#Ø×ÑÕr   c                ó˜   >• U R                   c  U R                  c  U R                  5         U R                  5         [        TU ]  U5        g r   )r9   r8   r­   r>   r°   Úiterate)r:   r!   r³   s     €r   r¶   ÚThreadedSelectReactor.iterate6  s9   ø€ Ø×ÑÑ'¨D¯N©NÑ,BØ×#Ñ#Ô%Ø�‰ŒÜ‰‰˜Õ r   c                ó¨   • U R                   c   eU R                  US5         U R                   R                  US9nU" 5         g ! [         a     g f = f)NF)r!   )r9   r]   Úgetr   )r:   r!   Úworks      r   ÚdoIterationÚ!ThreadedSelectReactor.doIteration<  s]   € Ø×#Ñ#Ñ/Ð/Ð/Ø×Ñ˜ %Ô(ð	Ø×'Ñ'×+Ñ+°GÐ+Ð<ˆDñ 	�øô ó 	Ùð	ús   £A Á
AÁAc                óB   >• U R                  5         [        TU ]	  5         g)z"
This should not normally be run.
N)r­   r°   ÚmainLoopr²   s    €r   r¾   ÚThreadedSelectReactor.mainLoopE  s   ø€ ð 	×ÑÔ!Ü‰ÑÕr   )r9   r7   r8   r3   r4   r   )r;   z+Callable[[Callable[[], None]], None] | Nonerh   ri   )rO   ÚobjectrP   Ústrrh   ri   )r!   úfloat | Nonerd   rv   rh   ri   rg   )T)r;   z$Callable[[Callable[[], None]], None]r…   rv   rh   ri   )r‹   r   rh   ri   )r�   r   rh   ri   )rh   z(list[IReadDescriptor | IWriteDescriptor])rh   zlist[IReadDescriptor])rh   zlist[IWriteDescriptor])rƒ   )r!   Úfloatrh   ri   )r!   rÂ   rh   ri   )Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r6   r>   rB   rT   r]   r^   r†   rŒ   r‘   r•   r˜   rœ   r    r£   r¦   r©   r­   r±   r¶   r»   r¾   Ú__static_attributes__Ú__classcell__)r³   s   @r   r/   r/   ~   s¸   ø† ñð
 DHðFØ@ðFà	õFòòô
Lô7
ôrð '+ð$à3ð$ð  $ð$ð 
õ	$ô,ô
ô
&ô
'ô
8ô ô!òòô2÷÷!ñ !ô÷õ r   r/   c                 ó6   • [        5       n SSKJn  U" U 5        U $ )zDConfigure the twisted mainloop to be run using the select() reactor.r   )ÚinstallReactor)r/   Útwisted.internet.mainrÌ   )ÚreactorrÌ   s     r   ÚinstallrÏ   M  s   € ä#Ó%€GÝ4á�7ÔØ€Nr   rÏ   N)
r!   rÂ   r"   rt   r#   ru   r$   zeCallable[[list[int], list[int], dict[int, IReadDescriptor], dict[int, IWriteDescriptor], bool], None]rh   ri   )'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   rZ   rI   r   r-   r5   r/   rÏ   Ú__all__r   r   r   Ú<module>rÝ      s¬   ðñ
+õX #ç ß Ý ß  å &å )Ý &ß XÑ Xß EÝ !Ý @áƒx€òð31Øð31à'ð31ð *ð31ð	ð	31ð 
ô31ñl ˆ]ÓôK˜I×6Ñ6ó Kó ðKò\ð ˆ+�r   