U
    ½){fT  ã                   @   s¢   d Z ddlZddlZddlZddlZddlZddlmZ ddlmZ ddlm	Z	 ddlm
Z
 ddl
mZ G d	d
„ d
ejƒZG dd„ dƒZddœdd„Zdd„ ZdS ))ÚRunnerÚruné    N)Ú
coroutines)Úevents)Ú
exceptions)Útasksé   )Útask_factoryc                   @   s   e Zd ZdZdZdZdS )Ú_StateÚcreatedZinitializedÚclosedN)Ú__name__Ú
__module__Ú__qualname__ÚCREATEDÚINITIALIZEDÚCLOSED© r   r   úP/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/taskgroup/runners.pyr
      s   r
   c                   @   s^   e Zd ZdZdddœdd„Zdd„ Zdd	„ Zd
d„ Zdd„ Zddœdd„Z	dd„ Z
dd„ ZdS )r   a5  A context manager that controls event loop life cycle.

    The context manager always creates a new event loop,
    allows to run async functions inside it,
    and properly finalizes the loop at the context manager exit.

    If debug is True, the event loop will be run in debug mode.
    If loop_factory is passed, it is used for new event loop creation.

    asyncio.run(main(), debug=True)

    is a shortcut for

    with asyncio.Runner(debug=True) as runner:
        runner.run(main())

    The run() method can be called multiple times within the runner's context.

    This can be useful for interactive console (e.g. IPython),
    unittest runners, console tools, -- everywhere when async code
    is called from existing sync framework and where the preferred single
    asyncio.run() call doesn't work.

    N)ÚdebugÚloop_factoryc                C   s0   t j| _|| _|| _d | _d | _d| _d| _d S )Nr   F)	r
   r   Ú_stateÚ_debugÚ_loop_factoryÚ_loopÚ_contextÚ_interrupt_countÚ_set_event_loop)Úselfr   r   r   r   r   Ú__init__6   s    zRunner.__init__c                 C   s   |   ¡  | S ©N)Ú
_lazy_init©r   r   r   r   Ú	__enter__?   s    zRunner.__enter__c                 C   s   |   ¡  d S r    )Úclose)r   Úexc_typeÚexc_valÚexc_tbr   r   r   Ú__exit__C   s    zRunner.__exit__c              	   C   sl   | j tjk	rdS z.| j}t	|ƒ | 
| ¡ ¡ | 
| ¡ ¡ W 5 | jrPt d¡ | ¡  d| _tj| _ X dS )zShutdown and close event loop.N)r   r
   r   r   r   Úset_event_loopr$   r   r   Ú_cancel_all_tasksÚrun_until_completeÚshutdown_asyncgensZshutdown_default_executor)r   Úloopr   r   r   r$   F   s    
zRunner.closec                 C   s   |   ¡  | jS )zReturn embedded event loop.)r!   r   r"   r   r   r   Úget_loopV   s    zRunner.get_loop©Úcontextc             
   C   sV  t  |¡std |¡ƒ‚t ¡ dk	r,tdƒ‚|  ¡  |dkrB| j}t	| j
||d}t ¡ t ¡ kr²t tj¡tjkr²tj| j|d}zt tj|¡ W q¶ tk
r®   d}Y q¶X nd}d| _zfz$| jrÒt | j
¡ | j
 |¡W W ¢BS  tjk
r   | jdkr| ¡ dkrtƒ ‚n‚ Y nX W 5 |dk	rPt tj¡|krPt tjtj¡ X dS )z/Run a coroutine inside the embedded event loop.z"a coroutine was expected, got {!r}Nz7Runner.run() cannot be called from a running event loopr/   )Ú	main_taskr   )r   ÚiscoroutineÚ
ValueErrorÚformatr   Ú_get_running_loopÚRuntimeErrorr!   r   Ú_task_factoryr   Ú	threadingÚcurrent_threadÚmain_threadÚsignalÚ	getsignalÚSIGINTÚdefault_int_handlerÚ	functoolsÚpartialÚ
_on_sigintr   r   r)   r+   r   ÚCancelledErrorZuncancelÚKeyboardInterrupt)r   Úcoror0   ÚtaskZsigint_handlerr   r   r   r   [   s@    
ÿÿ
ÿz
Runner.runc                 C   s„   | j tjkrtdƒ‚| j tjkr$d S | jd kr@t ¡ | _d| _	n
|  ¡ | _| j
d k	rb| j | j
¡ | j t¡ t ¡ | _tj| _ d S )NzRunner is closedT)r   r
   r   r6   r   r   r   Únew_event_loopr   r   r   Ú	set_debugÚset_task_factoryr7   ÚcontextvarsÚcopy_contextr   r"   r   r   r   r!   ‰   s    




zRunner._lazy_initc                 C   sF   |  j d7  _ | j dkr<| ¡ s<| ¡  | j dd„ ¡ d S tƒ ‚d S )Nr   c                   S   s   d S r    r   r   r   r   r   Ú<lambda>ž   ó    z#Runner._on_sigint.<locals>.<lambda>)r   ÚdoneÚcancelr   Úcall_soon_threadsaferC   )r   ÚsignumÚframer1   r   r   r   rA   ™   s    zRunner._on_sigint)r   r   r   Ú__doc__r   r#   r(   r$   r.   r   r!   rA   r   r   r   r   r      s   	.r   ©r   c             
   C   s@   t  ¡ dk	rtdƒ‚t|d}| | ¡W  5 Q R £ S Q R X dS )a¥  Execute the coroutine and return the result.

    This function runs the passed coroutine, taking care of
    managing the asyncio event loop and finalizing asynchronous
    generators.

    This function cannot be called when another asyncio event loop is
    running in the same thread.

    If debug is True, the event loop will be run in debug mode.

    This function always creates a new event loop and closes it at the end.
    It should be used as a main entry point for asyncio programs, and should
    ideally only be called once.

    Example:

        async def main():
            await asyncio.sleep(1)
            print('hello')

        asyncio.run(main())
    Nz8asyncio.run() cannot be called from a running event looprS   )r   r5   r6   r   r   )Úmainr   Úrunnerr   r   r   r   £   s    ÿr   c                 C   st   t  | ¡}|sd S |D ]}| ¡  q|  t j|ddiŽ¡ |D ]0}| ¡ rLq>| ¡ d k	r>|  d| ¡ |dœ¡ q>d S )NÚreturn_exceptionsTz1unhandled exception during asyncio.run() shutdown)ÚmessageÚ	exceptionrE   )r   Ú	all_tasksrN   r+   ÚgatherÚ	cancelledrX   Úcall_exception_handler)r-   Ú	to_cancelrE   r   r   r   r*   Ä   s    

ýr*   )Ú__all__rI   Úenumr?   r8   r;   Úasyncior   r   r   r   r	   r7   ÚEnumr
   r   r   r*   r   r   r   r   Ú<module>   s    
!