
    %hS                        % S SK r S SKr S SKrS SKrS SKrS SKrS SKrS SKrS SKrS SK	r	S SK
JrJr  S SKJrJrJrJrJrJrJrJrJrJrJrJr  SSKJr  SSKJr  \R<                  S:  a  S SKJr  OS SK Jr  \(       a  S S	K!J"r"  \" S
\S\4   S9r#\" S5      r$\" S5      r%S\RL                  SS4S jr'\(" \S5      (       a$  \RR                  r)\RT                  r*\\#/\#4   \+S'   O\ RR                  r)S\#S\#4S jr* " S S5      r, " S S\\$\%4   5      r- " S S\\$\%4   5      r.\SS.S\/S\\\\$\\\\%4   4   \\$\\%   4   4   /\\$\%4   4   4S jj5       r0\SS.S\\\$\\\\%4   4   \\$\\%   4   4   S\/S\\$\%4   4S  jj5       r0 S*SS.S\\\\$\\\\%4   4   \\$\\%   4   4      S\/S\\\\\$\\\\%4   4   \\$\\%   4   4   /\\$\%4   4   \\$\%4   4   4S! jjjr0\S"SS#.S$\/S%\S&   S\\\$\%4   /\\$\\\\%4   4   4   4S' jj5       r1\S"SS#.S\\$\%4   S$\/S%\S&   S\\$\\\\%4   4   4S( jj5       r1 S*S"SS#.S\\\$\%4      S$\/S%\S&   S\\\\$\%4   /\\$\\\\%4   4   4   \\$\\\\%4   4   4   4S) jjjr1g)+    N)FutureThreadPoolExecutor)TYPE_CHECKINGAny	AwaitableCallable	CoroutineDictGenericListOptionalTypeVarUnionoverload   )CurrentThreadExecutor)Local)   
   )	ParamSpec)
OptExcInfo_F.)bound_P_Rcontextreturnc                     U  H<  nU R                  U5      n UR                  5       U:w  a  UR                  U5        M<  M>     g ! [         a    UR                  U5         M_  f = fN)getsetLookupError)r   cvarcvalues      >/var/www/html/env/lib/python3.13/site-packages/asgiref/sync.py_restore_contextr&   ,   s]     T"	xxzV#  $ 
  	HHV	s   %AA#"A#markcoroutinefunctionfuncc                 D    [         R                  R                  U l        U $ r   )asyncio
coroutines_is_coroutine)r(   s    r%   r'   r'   C   s    $//==    c                   *    \ rS rSrSrS rS rS rSrg)ThreadSensitiveContextH   a)  Async context manager to manage context for thread sensitive mode

This context manager controls which thread pool executor is used when in
thread sensitive mode. By default, a single thread pool executor is shared
within a process.

The ThreadSensitiveContext() context manager may be used to specify a
thread pool per context.

This context manager is re-entrant, so only the outer-most call to
ThreadSensitiveContext will set the context.

Usage:

>>> import time
>>> async with ThreadSensitiveContext():
...     await sync_to_async(time.sleep, 1)()
c                     S U l         g r   tokenselfs    r%   __init__ThreadSensitiveContext.__init__\   s	    
r-   c                    #     [         R                  R                  5         U $ ! [         a(    [         R                  R	                  U 5      U l         U $ f = f7fr   )SyncToAsyncthread_sensitive_contextr    r"   r!   r3   r4   s    r%   
__aenter__!ThreadSensitiveContext.__aenter___   sS     	H00446   	H$==AA$GDJ	Hs%   A$ A.AAAAc                    #    U R                   (       d  g [        R                  R                  U S 5      nU(       a  UR	                  5         [        R
                  R                  U R                   5        g 7fr   )r3   r9   context_to_thread_executorpopshutdownr:   reset)r5   excvaluetbexecutors        r%   	__aexit__ ThreadSensitiveContext.__aexit__g   sN     zz99==dDI,,224::>s   A4A6r2   N)	__name__
__module____qualname____firstlineno____doc__r6   r;   rF   __static_attributes__ r-   r%   r/   r/   H   s    &?r-   r/   c                   T   \ rS rSr% Sr\" 5       rS\S'   0 rS\S'    SS\	\
\\\\\4   4   \
\\\   4   4   S\4S	 jjrS
\R$                  S\R&                  S\4S jrS rS\S\S\
\\4   4S jrSSSSSSS\\R2                     S
\R$                  S\R&                  SS4S jrSrg)AsyncToSyncq   a  
Utility class which turns an awaitable that only works on the thread with
the event loop into a synchronous callable that works in a subthread.

If the call stack contains an async loop, the code runs there.
Otherwise, the code runs in a new loop in a new thread.

Either way, this thread then pauses and waits to run any thread_sensitive
code called from further down the call stack using SyncToAsync, before
finally exiting once the async task returns.
r   	executorsz6Dict[asyncio.AbstractEventLoop, CurrentThreadExecutor]loop_thread_executors	awaitableforce_new_loopc                 x   [        U5      (       a+  [        U5      (       d0  [        [        USU5      5      (       d  [        R                  " SSS9  Xl         U R
                  R                  U l        X l        S U l	         [        R                  " 5       U l	        g ! [         a     N5f = f! [         a     g f = f)N__call__z4async_to_sync was passed a non-async-marked callable   )
stacklevel)callableiscoroutinefunctiongetattrwarningswarnrT   __self__AttributeErrorrU   main_event_loopr*   get_running_loopRuntimeError)r5   rT   rU   s      r%   r6   AsyncToSync.__init__   s     	""#I..'	:y(QRR MMFST #	 NN33DM -#	#*#;#;#=D   		  		s$   B B, 
B)(B),
B98B9argskwargsr   c                 z   SnU R                   (       dl  U R                  (       d[  [        [        R                  SS 5      nU(       a9  U[
        R                  " 5       :X  a   [        [        R                  SS 5      U l         [        R                  " 5       nUR                  5       (       a  [        S5      e [        5       n[        U R                  SS 5      n[        5       nXR                  l        [        R                   " 5       /n	[        [        R                  SS 5      n
S n U R"                  " U[$        R&                  " 5       U
U	/UQ70 UD6nU R                  (       a  U R                  R                  5       (       dq  [        R(                  " 5       nXR*                  U'   [-        SS9nUR/                  U R0                  X5      nU(       a  UR3                  U5        UR5                  5         OHU R                  R7                  U R                  R8                  U5        U(       a  UR3                  U5        Ub  U R*                  U	 [;        U	S	   5        XpR                  l        UR5                  5       $ ! [         a     GNf = f! Ub  U R*                  U	 [;        U	S	   5        XpR                  l        f = f)
NTmain_event_loop_pidra   znYou cannot use AsyncToSync in the same thread as an async event loop - just await the async function directly.currenttask_contextr   max_workersr   )rU   ra   r\   r9   threadlocalosgetpidr*   rb   
is_runningrc   r   rR   r   ri   contextvarscopy_context	main_wrapsysexc_infonew_event_looprS   r   submit_run_event_looprun_until_futureresultcall_soon_threadsafecreate_taskr&   )r5   re   rf   __traceback_hide__rh   
event_loopcall_resultold_executorcurrent_executorr   rj   looprT   loop_executorloop_futures                  r%   rW   AsyncToSync.__call__   sm   !""4+?+? #*'')>#
 #':biik'I'.++->($
		 113J $$&&">  ' %+H
 t~~y$?02!1 ++-. {66M%	2	
  I ((T-A-A-L-L-N-N--/3C**40 2q A+22(($ $$55kB""$ $$99((44i $$55kB ..t4WQZ(%1NN" !!##O  		@ ..t4WQZ(%1NN"s   I9 &DJ
 9
JJ
0J:c           	      <  ^ [         R                  " U5         UR                  U5         [         R                  " U5      mT H  nUR	                  5         M     U4S jnUR                  U" 5       5        T HP  nUR                  5       (       a  M  UR                  5       c  M-  UR                  SUR                  5       US.5        MR     [        US5      (       a  UR                  UR                  5       5        UR                  5         [         R                  " U R                  5        g! UR                  5         [         R                  " U R                  5        f = f!  [         R                  " U5      mT H  nUR	                  5         M     U4S jnUR                  U" 5       5        T HP  nUR                  5       (       a  M  UR                  5       c  M-  UR                  SUR                  5       US.5        MR     [        US5      (       a  UR                  UR                  5       5        UR                  5         [         R                  " U R                  5        f ! UR                  5         [         R                  " U R                  5        f = f= f)z@
Runs the given event loop (designed to be called in a thread).
c                  N   >#    [         R                  " T SS06I S h  vN   g  N7f)Nreturn_exceptionsT)r*   gather)taskss   r%   r   +AsyncToSync._run_event_loop.<locals>.gather  s     !..%H4HHHs   %#%Nz(unhandled exception during loop shutdown)message	exceptiontaskshutdown_asyncgens)r*   set_event_looprun_until_complete	all_taskscancel	cancelledr   call_exception_handlerhasattrr   closera   )r5   r   coror   r   r   s        @r%   rx   AsyncToSync._run_event_loop   s    	t$	=##D)=  ))$/!DKKM "I ''1!D~~'' ~~'333+U-1^^-=(,	 " 4!566++D,C,C,EF

&&t';';< 

&&t';';<5=  ))$/!DKKM "I ''1!D~~'' ~~'333+U-1^^-=(,	 " 4!566++D,C,C,EF

&&t';';< 

&&t';';<s>   E" A6D- %AD- -2E"J$A6I&AI&51J&2JJparentobjtypec                     [         R                  " U R                  U5      n[         R                  " X0R                  5      $ z
Include self for methods
)	functoolspartialrW   update_wrapperrT   r5   r   r   r(   s       r%   __get__AsyncToSync.__get__$  s/       7''nn==r-   r   z
Future[_R]ru   r   rj   z!Optional[List[asyncio.Task[Any]]]r   Nc                 r  #    SnUb  [        US   5        [        R                  " 5       nUb  Ub  UR                  U5         US   (       a   US   eU R
                  " U0 UD6I Sh  vN n	UR                  U	5        Ub  Ub  UR                  U5        [        R                  " 5       US'   g! [         a    U R
                  " U0 UD6I Sh  vN  n	 Nhf = f Np! [         a  n
UR                  U
5         Sn
A
N}Sn
A
ff = f! Ub  Ub  UR                  U5        [        R                  " 5       US'   f = f7f)z[
Wraps the awaitable with something that puts the result into the
result/exception future.
TNr   r   )r&   r*   current_taskappendBaseExceptionrT   
set_resultset_exceptionremoverq   rr   )r5   r   ru   rj   r   re   rf   r}   r   rz   es              r%   rs   AsyncToSync.main_wrap+  s6     "WQZ(++-#(@-	4 {C"1+%  $~~t>v>> ""6*'L,D##L1$113GAJ % C#'>>4#B6#BBBFC ? 	)%%a((	)
 'L,D##L1$113GAJs~   A D7
C B. C (C)C -D >0D7.CCCC CC 
D %C;6D ;D  D 1D44D7)r_   rT   rU   ra   )F)rH   rI   rJ   rK   rL   r   rR   __annotations__rS   r   r   r   r	   r   r   r   boolr6   re   rf   rW   rx   r   r   rq   Contextrs   rM   rN   r-   r%   rP   rP   q   s    
 Iw  WYSX  %R3R<001R2&')
 <Z$bgg Z$ Z$r Z$x"=H>c >C >HRV4D >(4!(4 (4 :	(4
 k))*(4 ww(4 ))(4 
(4r-   rP   c                   r   \ rS rSr% Sr\R                  " 5       r\" SS9r	\
R                  " S5      rS\S'   \
R                  " S5      rS\S'   \R                   " 5       rS	\S
'     SS\\\4   S\S\S   SS4S jjrS\R0                  S\R2                  S\4S jrS\S\S\\\\\\4   4   4S jrS rSrg)r9   iV  a  
Utility class which turns a synchronous callable into an awaitable that
runs in a threadpool. It also sets a threadlocal inside the thread so
calls to AsyncToSync can escape it.

If thread_sensitive is passed, the code will run in the same thread as any
outer code. This is needed for underlying Python code that is not
threadsafe (for example, code which handles SQLite database connections).

If the outermost program is async (i.e. SyncToAsync is outermost), then
this will be a dedicated single sub-thread that all sync code runs in,
one after the other. If the outermost program is sync (i.e. AsyncToSync is
outermost), this will just be the main thread. This is achieved by idling
with a CurrentThreadExecutor while AsyncToSync is blocking its sync parent,
rather than just blocking.

If executor is passed in, that will be used instead of the loop's default executor.
In order to pass in an executor, thread_sensitive must be set to False, otherwise
a TypeError will be raised.
r   rk   r:   z.contextvars.ContextVar[ThreadSensitiveContext]deadlock_contextzcontextvars.ContextVar[bool]zEweakref.WeakKeyDictionary[ThreadSensitiveContext, ThreadPoolExecutor]r>   Nr(   thread_sensitiverE   r   r   c                 d   [        U5      (       a+  [        U5      (       d  [        [        USU5      5      (       a  [        S5      eXl        [
        R                  " X5        X l        [        U 5        U(       a  Ub  [        S5      eX0l	         UR                  U l
        g ! [         a     g f = f)NrW   z4sync_to_async can only be applied to sync functions.z6executor must not be set when thread_sensitive is True)rZ   r[   r\   	TypeErrorr(   r   r   _thread_sensitiver'   	_executorr_   r`   )r5   r(   r   rE   s       r%   r6   SyncToAsync.__init__  s     "4(("74T#BCCRSS	  ,!1d# 4TUU!	 MMDM 		s   B" "
B/.B/re   rf   c                   #    Sn[         R                  " 5       nU R                  (       Ga  [        [        R
                  SS 5      nU(       a  UnOU R                  R                  S 5      (       aQ  U R                  R                  5       nXpR                  ;   a  U R                  U   nO[        SS9nX`R                  U'   OU[        R                  ;   a  [        R                  U   nO_U R                  R                  S5      (       a  [        S5      eU R                  nU R                  R                  S5        OU R                  n[         R"                  " 5       n[$        R&                  " U R(                  /UQ70 UD6n	UR*                  n
/ nUR-                  U[$        R&                  " U R.                  U[0        R2                  " 5       UU
U	5      5      n [         R4                  " U5      I S h  vN n[?        U5        U R                  R                  S5        U$  N,! [         R6                   a    Sn US   nUR9                  5          UI S h  vN    SnO*! [         R6                   a     Of = f! [:         a     Of = fUR=                  5       (       a  e U(       a  UR9                  5         UI S h  vN  n Nf = f! [?        U5        U R                  R                  S5        f = f7f)NTri   r   rk   Fz9Single thread executor already being used, would deadlockr   ) r*   rb   r   r\   rP   rR   r:   r    r>   r   rS   r   rc   single_thread_executorr!   r   rq   rr   r   r   r(   runrun_in_executorthread_handlerrt   ru   shieldCancelledErrorr   
IndexErrordoner&   )r5   re   rf   r}   r   current_thread_executorrE   r:   r   childr(   rj   	exec_cororetcancel_parentr   s                   r%   rW   SyncToAsync.__call__  s    !'') !!!&-k.C.CYPT&U#&2..22488 ,0+H+H+L+L+N(+/N/NN#>>?WXH  2a@HPX334LM:::&<<TB&&**511"O 
  66%%))$/ ~~H**,!!$))=d=f={{02 ((##

		-y11C& W%!!%%e,
- 2%% 	" M	#AJJ$)M--  ~~  "!//C!	"$ W%!!%%e,s   GK/H! 2H3H! 7(K/H! !K8I6IIIKI30I61K2I33I66
J KJ5K8J;9K>K  KK (K,,K/r   r   c                     [         R                  " U R                  U5      n[         R                  " X0R                  5      $ r   )r   r   rW   r   r(   r   s       r%   r   SyncToAsync.__get__  s/       7''ii88r-   c                     SnXR                   l        [        R                  " 5       U R                   l        X0R                   l        US   (       a   US   eU" U0 UD6$ ! [         a    U" U0 UD6s $ f = f)z5
Wraps the sync application with exception handling.
Tr   )rm   ra   rn   ro   rh   rj   r   )r5   r   ru   rj   r(   re   rf   r}   s           r%   r   SyncToAsync.thread_handler  s    
 " ,0(/1yy{,(4%
 A;-qk! ((( ! -T,V,,-s   A A43A4)r_   r   r   r(   )TN) rH   rI   rJ   rK   rL   	threadinglocalrm   r   r   rq   
ContextVarr:   r   r   weakrefWeakKeyDictionaryr>   r   r   r   r   r   r6   re   rf   rW   r   r	   r   r   rM   rN   r-   r%   r9   r9   V  s   , //#K 0A>
 	9: N  8C7M7M84  	!!#  g  "&37	r2v  /0	
 
0NBGG Nryy NR N`99$'9	"iS"--	.9)r-   r9   FrU   rU   c                     g r   rN   r   s    r%   async_to_syncr     s     r-   rT   c                    g r   rN   rT   rU   s     r%   r   r     s     r-   c                *   ^ U c  U4S j$ [        U TS9$ )Nc                    > [        U TS9$ )Nr   rP   )frU   s    r%   <lambda>async_to_sync.<locals>.<lambda>5  s    )
r-   r   r   r   s    `r%   r   r   $  s*      
 	
 % r-   Tr   rE   r   rE   r   c                     g r   rN   r   s     r%   sync_to_asyncr   ?  s     r-   c                    g r   rN   r(   r   rE   s      r%   r   r   H  s     r-   c                0   ^^ U c  UU4S j$ [        U TTS9$ )Nc                    > [        U TTS9$ )Nr   r9   )r   rE   r   s    r%   r   sync_to_async.<locals>.<lambda>\  s    -
r-   r   r   r   s    ``r%   r   r   R  s,     |
 	

 ) r-   r   )2r*   asyncio.coroutinesrq   r   inspectrn   rt   r   r]   r   concurrent.futuresr   r   typingr   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   version_infor   typing_extensions	_typeshedr   r   r   r   r   r&   r   r[   r'   r   r/   rP   r9   r   r   r   rN   r-   r%   <module>r      sP        	 
    9    ; w +$T#s(+,t_T]	k11 	d 	" 7+,,!55070M0M8RD"H-M!55B 2 
&? &?Rb4'"b&/ b4Jt)'"b&/ t)n 
 ! 
8B	#sB,//0(2y};L2MMNORV 
 
 !YsC|,,-Yr]"#	%  b"f 
" 	 !R3R<001R2&')	
  	xIc3l334hr9R=?P6QQ	RSR	 RV	6 
 "/3 +, xB (2yc2/F+F"GGH	 
 
 "/3	
2r6
  +,	
 b)CbL))* 
 (, "/3	
8BF#
$  +,	
 hr2v"iS".E*E!FFGR3R<(()+r-   