← back to Atmos Game

assets/gen/.venv/lib/python3.12/site-packages/anyio/__pycache__/from_thread.cpython-312.pyc

236 lines

�

c�bjAK��j�ddlmZdZddlZddlmZmZmZmZddl	m
Z
ddlmZm
Z
mZddlmZmZddlmZdd	lmZdd
lmZmZmZmZddlmZddlmZmZm Z m!Z!m"Z"d
dl#m$Z$m%Z%d
dl#m&Z'd
dl(m)Z)d
dl*m+Z+d
dl,m-Z-m.Z.d
dl/m0Z0d
dl1m2Z2m3Z3ejhdk\r	ddlm5Z5m6Z6nddl7m5Z5m6Z6e d�Z8e dd��Z9e5d�Z:d+d�Z;dd�							d,d�Z&dd�							d-d�Z<Gd �d!ee9e
�Z=Gd"�d#e0�Z>Gd$�d%�Z?eGd&�d'��Z@e		d.dd(�							d/d)��ZAd0d*�ZBy)1�)�annotations)�BlockingPortal�BlockingPortalProvider�check_cancelled�run�run_sync�start_blocking_portalN)�	Awaitable�Callable�	Coroutine�	Generator)�Future)�AbstractAsyncContextManager�AbstractContextManager�contextmanager)�	dataclass�field)�partial)�isawaitable)�Lock�Thread�current_thread�	get_ident)�
TracebackType)�Any�Generic�TypeVar�cast�overload�)�get_cancelled_exc_class�threadlocals)r)�NoEventLoopError)�Event)�CancelScope�create_task_group)�
TaskStatus)�EventLoopToken�
current_token)��)�TypeVarTuple�Unpack�T_Retval�T_coT)�	covariant�PosArgsTc�^�|�|S	tjS#t$r
td�d�wxYw)NzONot running inside an AnyIO worker thread, and no event loop token was provided)r"r)�AttributeErrorr#��tokens �h/Users/macstudio3/Projects/atmos-game/assets/gen/.venv/lib/python3.12/site-packages/anyio/from_thread.py�_token_or_errorr76sD��������)�)�)�����
�
��	��s��,r4c��|du}t|�}|jj|||r|j��Sd��S)a
    Call a coroutine function from a worker thread.

    :param func: a coroutine function
    :param args: positional arguments for the callable
    :param token: an event loop token to use to get back to the event loop thread
        (required if calling this function from outside an AnyIO worker thread)
    :return: the return value of the coroutine function
    :raises MissingTokenError: if no token was provided and called from outside an
        AnyIO worker thread
    :raises RunFinishedError: if the event loop tied to ``token`` is no longer running

    .. versionchanged:: 4.11.0
        Added the ``token`` parameter.

    Nr4)r7�
backend_class�run_async_from_thread�native_token��funcr5�args�explicit_tokens    r6rrCsW��*�$�&�N��E�"�E����4�4��d��%�,�,�5���DH�5���c��|du}t|�}|jj|||r|j��Sd��S)az
    Call a function in the event loop thread from a worker thread.

    :param func: a callable
    :param args: positional arguments for the callable
    :param token: an event loop token to use to get back to the event loop thread
        (required if calling this function from outside an AnyIO worker thread)
    :return: the return value of the callable
    :raises MissingTokenError: if no token was provided and called from outside an
        AnyIO worker thread
    :raises RunFinishedError: if the event loop tied to ``token`` is no longer running

    .. versionchanged:: 4.11.0
        Added the ``token`` parameter.

    Nr4)r7r9�run_sync_from_threadr;r<s    r6rr_sW��*�$�&�N��E�"�E����3�3��d��%�,�,�4���DH�4��r@c�r�eZdZUded<ded<ded<dZded	<				dd
�Zdd�Zdd�Z								dd
�Zy)�_BlockingAsyncContextManagerzFuture[T_co]�
_enter_futurezFuture[bool | None]�_exit_futurer$�_exit_event)NNNzMtuple[type[BaseException] | None, BaseException | None, TracebackType | None]�_exit_exc_infoc� �||_||_y�N)�	_async_cm�_portal)�self�async_cm�portals   r6�__init__z%_BlockingAsyncContextManager.__init__�s��"�����r@c��K�	t�|_|jj��d{���}|jj|�	|jj��d{���|jj|j��d{���}|S7�o#t$r!}|jj|��d}~wwxYw7�`7�7#|jj|j��d{���7}wxYw�wrJ)r$rGrK�
__aenter__rE�
set_result�
BaseException�
set_exception�wait�	__aexit__rH)rM�value�exc�results    r6�run_async_cmz)_BlockingAsyncContextManager.run_async_cm�s�����	1�$�w�D���.�.�3�3�5�5�E�

���)�)�%�0�
	J�
�"�"�'�'�)�)�)�
4�4�>�>�3�3�T�5H�5H�I�I�F��
�'6���	����,�,�S�1���	��
*��
J��3�4�>�>�3�3�T�5H�5H�I�I�I�F�s�D�,B"�B �B"�D�C�.C�/C�3&D�C�D� B"�"	C�+C�C�D�C�D�'D�:C=�;D�Dc��t�|_|jj|j�|_|jj
�SrJ)rrErL�start_task_soonr[rFrZ�rMs r6�	__enter__z&_BlockingAsyncContextManager.__enter__�s>��#�X��� �L�L�8�8��9J�9J�K����!�!�(�(�*�*r@c��|||f|_|jj|jj�|j
j
�SrJ)rHrL�callrG�setrFrZ)rM�&_BlockingAsyncContextManager__exc_type�'_BlockingAsyncContextManager__exc_value�'_BlockingAsyncContextManager__tracebacks    r6�__exit__z%_BlockingAsyncContextManager.__exit__�sF��)�+�{�B��������$�*�*�.�.�/�� � �'�'�)�)r@N)rN�!AbstractAsyncContextManager[T_co]rOr)�return�bool | None)rhr/)rc�type[BaseException] | Nonerd�BaseException | Nonere�TracebackType | Nonerhri)	�__name__�
__module__�__qualname__�__annotations__rHrPr[r_rf�r@r6rDrD{sr����%�%���	�����9��CQ���0+�
*�.�*�*�*�*�	*�

�*r@rDc��eZdZdd�Zddd�Zy)�_BlockingPortalTaskStatusc��||_yrJ)�_future)rM�futures  r6rPz"_BlockingPortalTaskStatus.__init__�s	����r@Nc�:�|jj|�yrJ)rurS)rMrXs  r6�startedz!_BlockingPortalTaskStatus.started�s��������&r@)rvrrJ)rX�objectrh�None)rmrnrorPrxrqr@r6rsrs�s���'r@rsc�r�eZdZdZdd�Zdd�Z								dd�Zdd�Zdd�Zddd�Z											dd�Z
												dd	�Ze						dd
��Z
e						dd��Z
						dd�Z
ed
d�							dd��Zed
d�							dd��Zd
d�							d d�Zd
d�							d!d�Z				d"d�Zy
)#rz�
    An object that lets external threads run code in an asynchronous event loop.

    :raises NoEventLoopError: if no supported asynchronous event loop is running in the
        current thread
    c�|�t�|_t�|_t	�|_t
�|_yrJ)r)�_tokenr�_event_loop_thread_idr$�_stop_eventr&�_task_groupr^s r6rPzBlockingPortal.__init__�s*��#�o���1:���"� �7���,�.��r@c��VK�|jj��d{���|S7��wrJ)r�rRr^s r6rRzBlockingPortal.__aenter__�s(�������)�)�+�+�+���	,�s�)�'�)c��K�|j��d{���|jj|||��d{���S7�)7��wrJ)�stopr�rW)rM�exc_type�exc_val�exc_tbs    r6rWzBlockingPortal.__aexit__�s>�����i�i�k����%�%�/�/��'�6�J�J�J�	��J�s�A�A�$A�A�A�Ac�v�|j�td��|jt�k(rtd��y)NzThis portal is not runningz7This method cannot be called from the event loop thread)r~�RuntimeErrorrr^s r6�_check_runningzBlockingPortal._check_running�s?���%�%�-��;�<�<��%�%���4��I��
�5r@c��TK�|jj��d{���y7��w)z#Sleep until :meth:`stop` is called.N)rrVr^s r6�sleep_until_stoppedz"BlockingPortal.sleep_until_stopped�s�������#�#�%�%�%�s�(�&�(c��K�d|_|jj�|r&|jjjd�yy�w)a.
        Signal the portal to shut down.

        This marks the portal as no longer accepting new calls and exits from
        :meth:`sleep_until_stopped`.

        :param cancel_remaining: ``True`` to cancel all the remaining tasks, ``False``
            to let them finish before returning

        Nz$the blocking portal is shutting down)r~rrbr��cancel_scope�cancel)rM�cancel_remainings  r6r�zBlockingPortal.stop�sE����&*��"�����������)�)�0�0�1W�X��s�AA
c��2��	�
K��j�	d�	�
�fd�}	||i|��}t|�r/t�5�
|j|�|�d{���}ddd�n|}|j	�s|j�	d�
y	d�
y7�8#1swY�4xYw#t
�$r%|j�|j�Yd�
yt$r>}|j	�s|j|�t|t�s�Yd}~d�
yd}~wwxYw#d�
wxYw�w)Nc���|j�rE�t�k(r�jd�y��#t�jd�j��yyy)Nzthe future was cancelledr4)�	cancelledrr�rr})�f�event_loop_thread_id�scoperMs ���r6�callbackz+BlockingPortal._call_func.<locals>.callback�sK����{�{�}�'�9�;�6��L�L�!;�<�)�5�����&@�����6�r@)r��Future[T_Retval]rhrz)
r~rr%�add_done_callbackr�rSr!r��set_running_or_notify_cancelrTrU�
isinstance�	Exception)rMr=r>�kwargsrvr��retval_or_awaitable�retvalrYr�r�s`        @@r6�
_call_funczBlockingPortal._call_func�s����� $�9�9��	�	�"&��"7��"7���.�/� �]�e��,�,�X�6�#6�6�F�#�]�-���#�#�%��!�!�&�)��E�&��E�#7��#�]��
'�(�	2��M�M�O��/�/�1��E��	��#�#�%��$�$�S�)��c�9�-��.��E��	���E�s��D�B�B
�B�B
�B�!D�D�D�B
�
B�B�-D
�D�D�D
�2D�D�D�D
�
D�D�Dc	��tt|jj|��|j|||||j
��y)a�
        Spawn a new task using the given callable.

        :param func: a callable
        :param args: positional arguments to be passed to the callable
        :param kwargs: keyword arguments to be passed to the callable
        :param name: name of the task (will be coerced to a string if not ``None``)
        :param future: a future that will resolve to the return value of the callable,
            or the exception raised during its execution

        ��namer4N)rrr��
start_soonr�r})rMr=r>r�r�rvs      r6�_spawn_task_from_threadz&BlockingPortal._spawn_task_from_threads<��&	��D�$�$�/�/�d�;��O�O������+�+�	
r@c��yrJrq�rMr=r>s   r6razBlockingPortal.call7s��
r@c��yrJrqr�s   r6razBlockingPortal.call>s��r@c�`�tt|j|g|���j��S)a3
        Call the given function in the event loop thread.

        If the callable returns a coroutine object, it is awaited on.

        :param func: any callable
        :raises RuntimeError: if the portal is not running or if this method is called
            from within the event loop thread

        )rr.r]rZr�s   r6razBlockingPortal.callCs-���H�2�d�2�2�4�?�$�?�F�F�H�I�Ir@Nr�c��yrJrq�rMr=r�r>s    r6r]zBlockingPortal.start_task_soonT���r@c��yrJrqr�s    r6r]zBlockingPortal.start_task_soon\r�r@c�d�|j�t�}|j||i||�|S)a�
        Start a task in the portal's task group.

        The task will be run inside a cancel scope which can be cancelled by cancelling
        the returned future.

        :param func: the target function
        :param args: positional arguments passed to ``func``
        :param name: name of the task (will be coerced to a string if not ``None``)
        :return: a future that resolves with the return value of the callable if the
            task completes successfully, or with the exception raised in the task
        :raises RuntimeError: if the portal is not running or if this method is called
            from within the event loop thread
        :rtype: concurrent.futures.Future[T_Retval]

        .. versionadded:: 3.0

        )r�rr�)rMr=r�r>r�s     r6r]zBlockingPortal.start_task_soonds2��0	
����$�h���$�$�T�4��T�1�=��r@c����d�fd�}|j�t��t��}t�}|j|�|j	||d|i||�|�j�fS)a�
        Start a task in the portal's task group and wait until it signals for readiness.

        This method works the same way as :meth:`.abc.TaskGroup.start`.

        :param func: the target function
        :param args: positional arguments passed to ``func``
        :param name: name of the task (will be coerced to a string if not ``None``)
        :return: a tuple of (future, task_status_value) where the ``task_status_value``
            is the value passed to ``task_status.started()`` from within the target
            function
        :rtype: tuple[concurrent.futures.Future[T_Retval], Any]

        .. versionadded:: 3.0

        c����j�sn|j�r�j�y|j�r �j	|j��ytd�}�j	|�yy)Nz1Task exited without calling task_status.started())�doner�r��	exceptionrUr�)rvrY�task_status_futures  �r6�	task_donez,BlockingPortal.start_task.<locals>.task_done�sl���%�*�*�,��#�#�%�&�-�-�/��%�%�'�&�4�4�V�5E�5E�5G�H�&�K��C�'�4�4�S�9�-r@�task_status)rvr�rhrz)r�rrsr�r�rZ)rMr=r�r>r�r�r�r�s       @r6�
start_taskzBlockingPortal.start_task�so���.
	:�	
����%+�X��/�0B�C���H��	���I�&��$�$�T�4�-��1M�t�UV�W��$�+�+�-�-�-r@c��t||�S)a�
        Wrap an async context manager as a synchronous context manager via this portal.

        Spawns a task that will call both ``__aenter__()`` and ``__aexit__()``, stopping
        in the middle until the synchronous context manager exits.

        :param cm: an asynchronous context manager
        :return: a synchronous context manager

        .. versionadded:: 2.1

        )rD)rM�cms  r6�wrap_async_context_managerz)BlockingPortal.wrap_async_context_manager�s��,�B��5�5r@�rhrz�rhr)r�rjr�rkr�rlrh�bool)F)r�r�rhrz)
r=�<Callable[[Unpack[PosArgsT]], Awaitable[T_Retval] | T_Retval]r>�tuple[Unpack[PosArgsT]]r��dict[str, Any]rvr�rhrz)r=r�r>r�r�r�r�ryrvr�rhrz)r=�1Callable[[Unpack[PosArgsT]], Awaitable[T_Retval]]r>�Unpack[PosArgsT]rhr.)r=�&Callable[[Unpack[PosArgsT]], T_Retval]r>r�rhr.)r=r�r>r�rhr.)r=r�r>r�r�ryrhr�)r=r�r>r�r�ryrhr�)r=r�r>r�r�ryrhr�)r=z"Callable[..., Awaitable[T_Retval]]r>ryr�ryrhztuple[Future[T_Retval], Any])r�rgrhzAbstractContextManager[T_co])rmrnro�__doc__rPrRrWr�r�r�r�r�rrar]r�r�rqr@r6rr�s<���/��K�,�K�&�K�%�	K�

�K��&�Y� (�J�(�&�(��	(�
!�(�
�
(�T
�J�
�&�
��	
�
�
�!�

�
�
�:��?�� ��
�	�����:��CS��	����J�J�J� �J�
�	J�"�
�	�?�� ���	�

�����
�	�4�� ���	�

�����	�J�� ���	�

��B�	).�0�).��).��	).�

&�).�V6�3�6�	%�6r@rc���eZdZUdZdZded<dZded<ede�	�Z	d
ed<edd�
�Z
ded<ed��Zded<edd�
�Zded<dd�Z
								dd�Zy)ra=
    A manager for a blocking portal. Used as a context manager. The first thread to
    enter this context manager causes a blocking portal to be started with the specific
    parameters, and the last thread to exit causes the portal to be shut down. Thus,
    there will be exactly one blocking portal running in this context as long as at
    least one thread has entered this context manager.

    The parameters are the same as for :func:`~anyio.run`.

    :param backend: name of the backend
    :param backend_options: backend options

    .. versionadded:: 4.4
    �asyncio�str�backendN�dict[str, Any] | None�backend_optionsF)�init�default_factoryr�_lockr)r��default�int�_leases)r�rrLz-AbstractContextManager[BlockingPortal] | None�
_portal_cmc�(�|j5|j�Dt|j|j�|_|jj�|_|xjdz
c_|jcddd�S#1swYyxYw)Nr )r�r�r	r�r�r_rLr�r^s r6r_z BlockingPortalProvider.__enter__�sc��
�Z�Z����&�"7��L�L�$�"6�"6�#��� $���8�8�:����L�L�A��L��<�<��Z�Z�s�A1B�Bc� �d}|j5|jsJ�|jdkDsJ�|xjdzc_|js|j}d|_|`ddd�|r|j	ddd�yy#1swY� xYw)Nrr )r�r�r�rLrf)rMr�r�r��	portal_cms     r6rfzBlockingPortalProvider.__exit__�s���DH�	�
�Z�Z��?�?�"�?��<�<�!�#�#�#��L�L�A��L��<�<� �O�O�	�"&����L������t�T�4�0���Z�s�AB�B
r�)r�rjr�rkr�rlrhrz)rmrnror�r�rpr�rrr�r�rLr�r_rfrqr@r6rr�s���
��G�S��-1�O�*�1��U�D�9�E�4�9��e�Q�/�G�S�/�#��/�G�^�/�@E�
�D�A�J�=��	 �1�,�1�&�1�%�	1�

�1r@rr�c#�������K�d���fd��d����fd�}t��t|d���}|j�	d}�j�}	|��		|j|j|�|j�y#t$rd}�wxYw#t$rY�,wxYw#	|j|j|�w#t$rYwwxYwxYw#|j�wxYw�w)a�
    Start a new event loop in a new thread and run a blocking portal in its main task.

    The parameters are the same as for :func:`~anyio.run`.

    :param backend: name of the backend
    :param backend_options: backend options
    :param name: name of the thread
    :return: a context manager that yields a blocking portal

    .. versionchanged:: 3.0
        Usage as a context manager is now required.

    c���K�t�4�d{���}����dt|�d��t�_�j	|�|j��d{���ddd��d{���y7�^7�7�	#1�d{���7swYyxYw�w)Nz-portal-�x)r�idrr�rSr�)�portal_r�rvr�s ���r6�
run_portalz)start_blocking_portal.<locals>.run_portal
st�����!�#�#�w��|�+2�)�8�B�w�K��?�(K�� �%����g�&��-�-�/�/�/�$�#�#��

0��$��#�#�#�sW�B�A1�B�AA7�A3�A7� B�+A5�,B�3A7�5B�7B	�=B�>B	�Bc����j�r	t�����yy#t$r0}�j�s�j	|�Yd}~yYd}~yd}~wwxYw)N)r�r�)r��
run_eventlooprTr�rU)rYr�r�rvr�s ����r6�run_blocking_portalz2start_blocking_portal.<locals>.run_blocking_portalsY����.�.�0�
.������1��
!�
.��{�{�}��(�(��-�-�%��
.�s�#�	A�!A�AT)�target�daemonr�FNr�)	rr�startrZrTrar�r��join)	r�r�r�r��thread�cancel_remaining_tasksrOrvr�s	```    @@r6r	r	�s������,0�.�.�&,�X�F�
�.�t�$�
G�F�
�L�L�N��!&�������		��L�

����F�K�K�)?�@�	���
���	�%)�"��	�� �
��
��
����F�K�K�)?�@���
��
��	���
�s}�:C-�C�B�B�7C-�
B�B'�	B$�!C�#B$�$C�'C�)C�C�	C�C�C�C�C�C*�*C-c��	tj}|jj�y#t$r
td�d�wxYw)aa
    Check if the cancel scope of the host task's running the current worker thread has
    been cancelled.

    If the host task's current cancel scope has indeed been cancelled, the
    backend-specific cancellation exception will be raised.

    :raises RuntimeError: if the current thread was not spawned by
        :func:`.to_thread.run_sync`

    z>This function can only be called inside an AnyIO worker threadN)r"r)r3r#r9rr4s r6rr3sJ��� ,� :� :��
���'�'�)�����L�
��	��s	�-�A)r5�EventLoopToken | Nonerhr()r=z7Callable[[Unpack[PosArgsT]], Coroutine[Any, Any, T_co]]r>r�r5r�rhr/)r=r�r>r�r5r�rhr.)r�N)r�r�r�r�r�z
str | Nonerhz$Generator[BlockingPortal, Any, None]r�)C�
__future__r�__all__�sys�collections.abcr
rrr
�concurrent.futuresr�
contextlibrrr�dataclassesrr�	functoolsr�inspectr�	threadingrrrr�typesr�typingrrrrr�_core._eventloopr!r"rr��_core._exceptionsr#�_core._synchronizationr$�_core._tasksr%r&�
abc._tasksr'�lowlevelr(r)�version_infor,r-�typing_extensionsr.r/r1r7rrDrsrrr	rrqr@r6�<module>r�s���"����E�E�%���
)���=�=�����3�/�)�8�"�3����w��+�+�6��:����v��&���
�#��
� $(��
A����!��
�	�>$(��
0����!���	�83*�7�4�=�2H�3*�l'�
�'�B6�B6�J�51�51��51�p��-1�8��	8�
�8�*�8��	8�
*�8��8�v*r@