
    jKj5
                     @    d dl Z d dlZd dlmZ d dlmZ  G d d      Zy)    N)	Awaitable)Anyc                   ~    e Zd ZdZddZdee   defdZddZe	dee   defd       Z
dej                  fd	Zdd
Zy)_BackgroundEventLoopRunnerzARun awaitables to completion on a reusable background event loop.returnNc                     d | _         d | _        t        j                         | _        t        j
                         | _        y N)_loop_thread	threadingEvent_startedLock_lockselfs    e/Users/ahmed/devFolder/Ultron/claude-voice/.venv/lib/python3.12/site-packages/posthog/_async_utils.py__init__z#_BackgroundEventLoopRunner.__init__
   s,    7;
04!)^^%
    	awaitablec                     | j                         }t        j                  | j                  |      |      }|j	                         S r	   )_ensure_loopasynciorun_coroutine_threadsafe_await_resultresult)r   r   loopfutures       r   runz_BackgroundEventLoopRunner.run   s:      "11$2D2DY2OQUV}}r   c                 v   | j                   5  | j                  }| j                  }d | _        d | _        d d d        |j                         ry |t	        j
                         u r|j                  |j                         y |j                  |j                         |j                          y # 1 sw Y   |xY wr	   )
r   r
   r   	is_closedr   current_thread	call_soonstopcall_soon_threadsafejoin)r   r   threads      r   closez _BackgroundEventLoopRunner.close   s    ZZ::D\\FDJDL	  <6>T^^-=Y--//NN499%!!$)), Zs   'B//B8c                 "   K   |  d {   S 7 wr	    )r   s    r   r   z(_BackgroundEventLoopRunner._await_result&   s     s   c                 h   | j                   5  | j                  U| j                  I| j                  j                         r/| j                  j	                         s| j                  cd d d        S | j
                  j                          t        j                  | j                  dd      | _        | j                  j                          d d d        | j
                  j                          | j                   5  | j                  J | j                  cd d d        S # 1 sw Y   SxY w# 1 sw Y   y xY w)N PostHogBackgroundEventLoopRunnerT)targetnamedaemon)r   r
   r   is_aliver!   r   clearr   Thread	_run_loopstartwaitr   s    r   r   z'_BackgroundEventLoopRunner._ensure_loop*   s    ZZ

&LL,LL))+

,,.zz Z MM!$++~~7DL
 LL  " 	ZZ::))):: Z% Z$ Zs   AD/AD8D(D%(D1c           	         t        j                         }t        j                  |       | j                  5  || _        | j
                  j                          d d d        	 |j                          t        j                  |      }|D ]  }|j                           |r$|j                  t        j                  |ddi       |j                  |j                                |j                  |j                                t        j                  d        |j                          y # 1 sw Y   xY w# t        j                  |      }|D ]  }|j                           |r$|j                  t        j                  |ddi       |j                  |j                                |j                  |j                                t        j                  d        |j                          w xY w)Nreturn_exceptionsT)r   new_event_loopset_event_loopr   r
   r   setrun_forever	all_taskscancelrun_until_completegathershutdown_asyncgensshutdown_default_executorr(   )r   r   pendingtasks       r   r3   z$_BackgroundEventLoopRunner._run_loopA   su   %%'t$ZZDJMM 	''-G  ''NNGDtD ##D$;$;$=>##D$B$B$DE""4(JJL# Z ''-G  ''NNGDtD ##D$;$;$=>##D$B$B$DE""4(JJLs   "D'!D3 'D03B7G*)r   N)__name__
__module____qualname____doc__r   r   r   r   r(   staticmethodr   r   AbstractEventLoopr   r3   r*   r   r   r   r      sc    K&Ys^  
" y~ #  g77 .r   r   )r   r   collections.abcr   typingr   r   r*   r   r   <module>rL      s      % N Nr   