
    g3fi                    8   d dl mZ d dlZd dlZd dlZd dlmZmZm	Z	 d dl
mZmZ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 d d	lmZ d d
lmZ d dlmZmZ d dlm Z   ed      Z! ed      Z" G d dee!e"f         Z# G d de      Z$ G d de      Z%ddZ&ddZ'y)    )annotationsN)	AwaitableCallable	Coroutine)AbstractAsyncContextManagerAbstractContextManager	ExitStack)copy_context)TracebackType)ProtocolTypeVarcast)RunnableConfig)get_executor_for_config)	ParamSpec)CONTEXT_NOT_SUPPORTEDrun_coroutine_threadsafe)GraphBubbleUpPTc                  >    e Zd Zddddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZy)SubmitNFT__name____cancel_on_exit____reraise_on_exit____next_tick__c                    y N )selffnr   r   r   r   argskwargss           X/var/www/auto_recruiter/arenv/lib/python3.12/site-packages/langgraph/pregel/_executor.py__call__zSubmit.__call__   s     (+    r"   Callable[P, T]r#   P.argsr   
str | Noner   boolr   r,   r   r,   r$   P.kwargsreturnzconcurrent.futures.Future[T])r   
__module____qualname__r&   r    r'   r%   r   r      sc    
  $#($(#	+	+ 	+ 		+
 !	+ "	+ 	+ 	+ 
&	+r'   r   c                  r    e Zd ZdZddZddddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZddZdd	Z	 	 	 	 	 	 	 	 dd
Zy)BackgroundExecutoraI  A context manager that runs sync tasks in the background.
    Uses a thread pool executor to delegate tasks to separate threads.
    On exit,
    - cancels any (not yet started) tasks with `__cancel_on_exit__=True`
    - waits for all tasks to finish
    - re-raises the first exception from tasks with `__reraise_on_exit__=True`c                    t               | _        | j                  j                  t        |            | _        i | _        y r   )r	   stackenter_contextr   executortasks)r!   configs     r%   __init__zBackgroundExecutor.__init__0   s.    [


001H1PQIK
r'   NFTr   c                  t               }|rZt        t        j                  j                  t
            | j                  j                  t        |j                  |g|i |      }	n+ | j                  j                  |j                  |g|i |}	||f| j                  |	<   |	j                  | j                         |	S r   )r
   r   
concurrentfuturesFuturer   r6   submit	next_tickrunr7   add_done_callbackdone)
r!   r"   r   r   r   r   r#   r$   ctxtasks
             r%   r>   zBackgroundExecutor.submit6   s     n""))!,$$$YMdMfMD
 (4==''EdEfED.0CD

4tyy)r'   c                    	 |j                          | j                  j                  |       y# t        $ r | j                  j                  |       Y yt        $ r Y yw xY w)z3Remove the task from the tasks dict when it's done.N)resultr7   popr   BaseException)r!   rD   s     r%   rB   zBackgroundExecutor.doneM   sP    		!KKM JJNN4   	! JJNN4  		s   . $AAAc                    | j                   S r   r>   r!   s    r%   	__enter__zBackgroundExecutor.__enter__Z   s    {{r'   c                   | j                   j                         }|j                         D ]  \  }\  }}|s|j                           |D ch c]  }|j	                         r| c}x}	rt
        j                  j                  |	       | j                  j                  |||       |0|j                         D ]  \  }\  }}
|
s	 |j                           y y c c}w # t
        j                  j                  $ r Y Ew xY wr   )r7   copyitemscancelrB   r;   r<   waitr4   __exit__rF   CancelledError)r!   exc_type	exc_value	tracebackr7   rD   rP   _tpendingreraises              r%   rR   zBackgroundExecutor.__exit__]   s     

!!& 	D+61	 #(8Qqvvxq8878##G,

Hi;&+kkm "lq'KKM	  9 "))88 s   C#CC  D ?D r8   r   r.   Noner(   )rD   zconcurrent.futures.Futurer.   r\   r.   r   )rT   type[BaseException] | NonerU   BaseException | NonerV   TracebackType | Noner.   zbool | None)	r   r/   r0   __doc__r9   r>   rB   rL   rR   r    r'   r%   r2   r2   (   s    RL  $#($(#  	
 ! "   
&.!, ( (	
 
r'   r2   c                  r    e Zd ZdZddZddddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZddZdd	Z	 	 	 	 	 	 	 	 dd
Zy)AsyncBackgroundExecutoraS  A context manager that runs async tasks in the background.
    Uses the current event loop to delegate tasks to asyncio tasks.
    On exit,
    - cancels any tasks with `__cancel_on_exit__=True`
    - waits for all tasks to finish
    - re-raises the first exception from tasks with `__reraise_on_exit__=True`
      ignoring CancelledErrorc                    i | _         t               | _        t        j                         | _        |j                  d      x}rt        j                  |      | _        y d | _        y )Nmax_concurrency)	r7   objectsentinelasyncioget_running_looploopget	Semaphore	semaphore)r!   r8   re   s      r%   r9   z AsyncBackgroundExecutor.__init__   sU    >@
,,.	$jj):;;?;7>7H7H8DN "DNr'   NFTr   c               f   t        t        d d t        f    ||i |      }| j                  rt	        | j                  |      }t
        rt        || j                  ||      }	n"t        || j                  |t               |      }	||f| j                  |	<   |	j                  | j                         |	S )N)namelazy)ro   contextrp   )r   r   r   rm   gatedr   r   rj   r
   r7   rA   rB   )
r!   r"   r   r   r   r   r#   r$   cororD   s
             r%   r>   zAsyncBackgroundExecutor.submit   s     IdD!m,b$.A&.AB>>.D +diih]D ,		$"D /0CD

4tyy)r'   c                "   	 |j                         x}r,t        |t              r8| j                  j	                  |       y | j                  j	                  |       y y # t
        j                  $ r | j                  j	                  |       Y y w xY wr   )	exception
isinstancer   r7   rG   rh   rS   )r!   rD   excs      r%   rB   zAsyncBackgroundExecutor.done   sq    		!nn&&s& c=1JJNN4(

t$ 2 %% 	!JJNN4 	!s   =A  A .BBc                "   K   | j                   S wr   rJ   rK   s    r%   
__aenter__z"AsyncBackgroundExecutor.__aenter__   s     {{s   c                  K   | j                   j                         }|j                         D ]&  \  }\  }}|s|j                  | j                         ( |rt        j                  |       d {    |4|j                         D ]   \  }\  }}|s	 |j                         x}	r|	" y y 7 ;# t
        j                  $ r Y <w xY wwr   )	r7   rN   rO   rP   rg   rh   rQ   ru   rS   )
r!   rT   rU   rV   r7   rD   rP   rW   rZ   rw   s
             r%   	__aexit__z!AsyncBackgroundExecutor.__aexit__   s      

!!& 	+D+61DMM*	+ ,,u%%%&+kkm "lq'"nn..s.!	 /	  & -- s:   6C7C0B,1"CB.(C.CCCCr[   )r"   zCallable[P, Awaitable[T]]r#   r*   r   r+   r   r,   r   r,   r   r,   r$   r-   r.   zasyncio.Future[T])rD   zasyncio.Futurer.   r\   r]   )rT   r^   rU   r_   rV   r`   r.   r\   )	r   r/   r0   ra   r9   r>   rB   ry   r{   r    r'   r%   rc   rc   z   s    !	"  $#($(#%  	
 ! "   
:
!, ( (	
 
r'   rc   c                   K   | 4 d{    | d{   cddd      d{    S 7 7 7 	# 1 d{  7  sw Y   yxY ww)zHA coroutine that waits for a semaphore before running another coroutine.Nr    )rm   rs   s     r%   rr   rr      sA       z     sD   A)A/+/A-A/AA8AAc                <    t        j                  d        | |i |S )zPA function that yields control to other threads before running another function.r   )timesleep)r"   r#   r$   s      r%   r?   r?      s    JJqMtvr'   )rm   zasyncio.Semaphorers   zCoroutine[None, None, T]r.   r   )r"   r)   r#   r*   r$   r-   r.   r   )(
__future__r   rh   concurrent.futuresr;   r~   collections.abcr   r   r   
contextlibr   r   r	   contextvarsr
   typesr   typingr   r   r   langchain_core.runnablesr   langchain_core.runnables.configr   typing_extensionsr   langgraph._internal._futurer   r   langgraph.errorsr   r   r   r   r2   rc   rr   r?   r    r'   r%   <module>r      s    "    : : U U $   4 C ' W *cNCL
+Xad^ 
+O/ OdY9 Yxr'   