� u��gFD��6�ddlmZddlZddlmZmZmZddlmZddl m Z m Z m Z ddl mZmZddlmZddlmZmZmZdd lmZdd lmZmZmZmZmZd d lmZd d l m!Z!m"Z"m#Z#d dl$m%Z%d dl&m'Z'm(Z(d dl)m*Z*d dl+m,Z,ej-dkr ddlm.Z.m/Z/nddl0m.Z.m/Z/ed��Z1edd���Z2e.d��Z3d2d�Z4d3d �Z5Gd!�d"ee2e ��Z6Gd#�d$e,��Z7Gd%�d&��Z8eGd'�d(����Z9e d4d5d/���Z:d6d1�Z;dS)7�)� annotationsN)� Awaitable�Callable� Generator)�Future)�AbstractAsyncContextManager�AbstractContextManager�contextmanager)� dataclass�field)� isawaitable)�Lock�Thread� get_ident)� TracebackType)�Any�Generic�TypeVar�cast�overload�)� _eventloop)�get_async_backend�get_cancelled_exc_class� threadlocals)�Event)� CancelScope�create_task_group)� AsyncBackend)� TaskStatus)�� )� TypeVarTuple�Unpack�T_Retval�T_coT)� covariant�PosArgsT�func�1Callable[[Unpack[PosArgsT]], Awaitable[T_Retval]]�args�Unpack[PosArgsT]�returnc�� tj}tj}n#t$rt d��d�wxYw|�|||���S)z� Call a coroutine function from a worker thread. :param func: a coroutine function :param args: positional arguments for the callable :return: the return value of the coroutine function �9This function can only be run from an AnyIO worker threadN��token)r�current_async_backend� current_token�AttributeError� RuntimeError�run_async_from_thread�r)r+� async_backendr1s �a/home/asafur/pinokio/api/open-webui.git/app/env/lib/python3.11/site-packages/anyio/from_thread.py�runr:(si���$�:� ��*���� ����� G� � �� ����� � .� .�t�T�� .� G� G�G���6�&Callable[[Unpack[PosArgsT]], T_Retval]c�� tj}tj}n#t$rt d��d�wxYw|�|||���S)z� Call a function in the event loop thread from a worker thread. :param func: a callable :param args: positional arguments for the callable :return: the return value of the callable r/Nr0)rr2r3r4r5�run_sync_from_threadr7s r9�run_syncr?>si���$�:� ��*���� ����� G� � �� ����� � -� -�d�D�� -� F� F�Fr;c�\�eZdZUded<ded<ded<dZded <dd�Zdd�Zdd�Zdd�ZdS) �_BlockingAsyncContextManagerz Future[T_co]� _enter_futurezFuture[bool | None]� _exit_futurer� _exit_event)NNNzMtuple[type[BaseException] | None, BaseException | None, TracebackType | None]�_exit_exc_info�async_cm�!AbstractAsyncContextManager[T_co]�portal�BlockingPortalc�"�||_||_dS�N)� _async_cm�_portal)�selfrFrHs r9�__init__z%_BlockingAsyncContextManager.__init__\s��"����� � � �r-� bool | Nonec��K� t��|_|j����d{V��}|j�|��n-#t $r }|j�|���d}~wwxYw |j����d{V��|jj |j ��d{V��}|S#|jj |j ��d{V��}|ccYSxYwrK) rrDrL� __aenter__rB� set_result� BaseException� set_exception�wait� __aexit__rE)rN�value�exc�results r9� run_async_cmz)_BlockingAsyncContextManager.run_async_cmbs*���� 1�$�w�w�D� ��.�3�3�5�5�5�5�5�5�5�5�E� � � )� )�%� 0� 0� 0� 0�� � � � � � � ,� ,�S� 1� 1� 1� ����� ���� � �"�'�'�)�)� )� )� )� )� )� )� )� 4�4�>�3�T�5H�I�I�I�I�I�I�I�F��M��4�4�>�3�T�5H�I�I�I�I�I�I�I�F��M�M�M�M�M�M�M�Ms#�2A� A;�A6�6A;�?B:�:Cr&c��t��|_|j�|j��|_|j���SrK)rrBrM�start_task_soonr\rCr[�rNs r9� __enter__z&_BlockingAsyncContextManager.__enter__ys>��#�X�X��� �L�8�8��9J�K�K����!�(�(�*�*�*rP�&_BlockingAsyncContextManager__exc_type�type[BaseException] | None�'_BlockingAsyncContextManager__exc_value�BaseException | None�'_BlockingAsyncContextManager__traceback�TracebackType | Nonec��|||f|_|j�|jj��|j���SrK)rErM�callrD�setrCr[)rNrarcres r9�__exit__z%_BlockingAsyncContextManager.__exit__~sD�� )�+�{�B��� � ���$�*�.�/�/�/�� �'�'�)�)�)rPN)rFrGrHrI)r-rQ)r-r&)rarbrcrdrerfr-rQ) �__name__� __module__� __qualname__�__annotations__rErOr\r`rj�rPr9rArATs������������%�%�%�%����� ���������� ����.+�+�+�+� *�*�*�*�*�*rPrAc� �eZdZd d�Zd d d �ZdS) �_BlockingPortalTaskStatus�futurerc��||_dSrK)�_future)rNrrs r9rOz"_BlockingPortalTaskStatus.__init__�s ���� � � rPNrY�objectr-�Nonec�:�|j�|��dSrK)rtrT)rNrYs r9�startedz!_BlockingPortalTaskStatus.started�s�� � ����&�&�&�&�&rP)rrrrK)rYrur-rv)rkrlrmrOrxrorPr9rqrq�sA����������'�'�'�'�'�'�'rPrqc���eZdZdZd4d�Zd5d�Zd4d�Zd6d�Zd5d�Zd5d�Z d7d8d�Z d9d�Z d:d �Z e d;d$���Ze d<d&���Zd=d'�Ze d(d)�d>d*���Ze d(d)�d?d+���Zd(d)�d@d,�Zd(d)�dAd/�ZdBd3�Zd(S)CrIzLAn object that lets external threads run code in an asynchronous event loop.r-c�B�t�����SrK)r�create_blocking_portal)�clss r9�__new__zBlockingPortal.__new__�s�� �"�"�9�9�;�;�;rPrvc��t��|_t��|_t ��|_t ��|_dSrK)r�_event_loop_thread_idr� _stop_eventr� _task_groupr�_cancelled_exc_classr_s r9rOzBlockingPortal.__init__�s<��1:����"� �7�7���,�.�.���$;�$=�$=��!�!�!rPc��HK�|j����d{V��|SrK)r�rSr_s r9rSzBlockingPortal.__aenter__�s2������)�)�+�+�+�+�+�+�+�+�+�� rP�exc_typerb�exc_valrd�exc_tbrfrQc��~K�|����d{V��|j�|||���d{V��SrK)�stopr�rX)rNr�r�r�s r9rXzBlockingPortal.__aexit__�sU���� �i�i�k�k���������%�/�/��'�6�J�J�J�J�J�J�J�J�JrPc�~�|j�td���|jt��krtd���dS)NzThis portal is not runningz7This method cannot be called from the event loop thread)rr5rr_s r9�_check_runningzBlockingPortal._check_running�sI�� � %� -��;�<�<� <� � %���� 4� 4��I��� � 5� 4rPc��HK�|j����d{V��dS)z#Sleep until :meth:`stop` is called.N)r�rWr_s r9�sleep_until_stoppedz"BlockingPortal.sleep_until_stopped�s3������#�#�%�%�%�%�%�%�%�%�%�%�%rPF�cancel_remaining�boolc��K�d|_|j���|r |jj���dSdS)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 N)rr�rir�� cancel_scope�cancel)rNr�s r9r�zBlockingPortal.stop�sU����&*��"� ������� � 3� � � )� 0� 0� 2� 2� 2� 2� 2� 3� 3rPr)�<Callable[[Unpack[PosArgsT]], Awaitable[T_Retval] | T_Retval]r+�tuple[Unpack[PosArgsT]]�kwargs�dict[str, Any]rr�Future[T_Retval]c���� K�d� �fd� } ||i|��}t|��rmt��5� |���r� ���n|�|��|�d{V��}ddd��n #1swxYwYn|}|���s|�|��n�#�j$r+|���|���YnUt$rI}|���s|� |��t|t��s�Yd}~nd}~wwxYwd� dS#d� wxYw)N�fr�r-rvc���|���r3�jdt��fvr���j��dSdSdSrK)� cancelledrrrhr�)r��scoperNs ��r9�callbackz+BlockingPortal._call_func.<locals>.callback�sb����{�{�}�}� (��!;��� � �D�"�"�� � �%�,�'�'�'�'�'�  (� (�"�"rP)r�r�r-rv) r rr�r��add_done_callbackrTr��set_running_or_notify_cancelrUrV� isinstance� Exception) rNr)r+r�rrr��retval_or_awaitable�retvalrZr�s ` @r9� _call_funczBlockingPortal._call_func�s������� (� (� (� (� (� (� (� �"&�$��"7��"7�"7� ��.�/�/� -� �]�]�7�e��'�'�)�)�;�� � ������0�0��:�:�:�#6�6�6�6�6�6�6�F� 7�7�7�7�7�7�7�7�7�7�7����7�7�7�7��-���#�#�%�%� *��!�!�&�)�)�)����(� 2� 2� 2� �M�M�O�O�O� � /� /� 1� 1� 1� 1� 1�� � � ��#�#�%�%� *��$�$�S�)�)�)��c�9�-�-� �� � � � � �����  �����E�E�E��D�E�L�L�L�Ls_�%B=�AB�9 B=�B � B=� B � B=�*E �=2E�/E �1 E�:?D>�9E �>E�E � E�nameruc��t�)a% Spawn a new task using the given callable. Implementers must ensure that the future is resolved when the task finishes. :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 )�NotImplementedError)rNr)r+r�r�rrs r9�_spawn_task_from_threadz&BlockingPortal._spawn_task_from_thread�s ��*"�!rPr*r,r%c��dSrKro�rNr)r+s r9rhzBlockingPortal.calls �� �3rPr<c��dSrKror�s r9rhzBlockingPortal.calls ���3rPc�h�tt|j|g|�R������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^r[r�s r9rhzBlockingPortal.calls6���H�2�d�2�4�?�$�?�?�?�F�F�H�H�I�I�IrPN)r�c��dSrKro�rNr)r�r+s r9r^zBlockingPortal.start_task_soon$� �� �3rPc��dSrKror�s r9r^zBlockingPortal.start_task_soon,r�rPc�|�|���t��}|�||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�)rNr)r�r+r�s r9r^zBlockingPortal.start_task_soon4s@��0 ������$�h�h�� �$�$�T�4��T�1�=�=�=��rP�"Callable[..., Awaitable[T_Retval]]�tuple[Future[T_Retval], Any]c���d�fd� }|���t���t���}t��}|�|��|�||d|i||��|����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 rrr�r-rvc�J������s�|���r����dS|���r)��|�����dSt d��}��|��dSdS)Nz1Task exited without calling task_status.started())�doner�r�� exceptionrVr5)rrrZ�task_status_futures �r9� task_donez,BlockingPortal.start_task.<locals>.task_donehs����%�*�*�,�,� :��#�#�%�%�:�&�-�-�/�/�/�/�/��%�%�'�'�:�&�4�4�V�5E�5E�5G�5G�H�H�H�H�H�&�K���C�'�4�4�S�9�9�9�9�9� :� :rP� task_status)rrr�r-rv)r�rrqr�r�r[)rNr)r�r+r�r�r�r�s @r9� start_taskzBlockingPortal.start_taskQs����. :� :� :� :� :� :� ������%+�X�X��/�0B�C�C� ��H�H�� ���I�&�&�&� �$�$�T�4�-��1M�t�UV�W�W�W��$�+�+�-�-�-�-rP�cmrG�AbstractContextManager[T_co]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 )rA)rNr�s r9�wrap_async_context_managerz)BlockingPortal.wrap_async_context_manager|s��,�B��5�5�5rP�r-rI�r-rv)r�rbr�rdr�rfr-rQ)F)r�r�r-rv) r)r�r+r�r�r�rrr�r-rv) r)r�r+r�r�r�r�rurrr�r-rv�r)r*r+r,r-r%�r)r<r+r,r-r%)r)r�r+r,r-r%)r)r*r+r,r�rur-r�)r)r<r+r,r�rur-r�)r)r�r+r,r�rur-r�)r)r�r+rur�rur-r�)r�rGr-r�)rkrlrm�__doc__r}rOrSrXr�r�r�r�r�rrhr^r�r�rorPr9rIrI�s�������V�V�<�<�<�<�>�>�>�>� ����K�K�K�K�����&�&�&�&�3�3�3�3�3� (�(�(�(�T"�"�"�"�.�����X�� �����X��J�J�J�J�"� � ������X��� � ������X��� ������B� ).�).�).�).�).�).�V6�6�6�6�6�6rPrIc���eZdZUdZdZded<dZded<ede� ��Z d ed <edd � ��Z ded<ed���Z ded<edd� ��Z ded<dd�Z d d�ZdS)!�BlockingPortalProvidera= 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�rIrMz-AbstractContextManager[BlockingPortal] | None� _portal_cmr-c���|j5|j�=t|j|j��|_|j���|_|xjdz c_|jcddd��S#1swxYwYdS)Nr)r�r��start_blocking_portalr�r�r`rMr�r_s r9r`z BlockingPortalProvider.__enter__�s��� �Z� � ���&�"7��L�$�"6�#�#��� $��8�8�:�:�� � �L�L�A� �L�L��<� � � � � � � � � � � � ���� � � � � � s�AA0�0A4�7A4r�rbr�rdr�rfrvc���d}|j5|jsJ�|jdksJ�|xjdzc_|js|j}d|_|`ddd��n #1swxYwY|r|�ddd��dSdS)Nrr)r�r�r�rMrj)rNr�r�r�� portal_cms r9rjzBlockingPortalProvider.__exit__�s��� DH� � �Z� !� !��?� "� "�?��<�!�#�#�#�#� �L�L�A� �L�L��<� !� �O� �"&����L� !� !� !� !� !� !� !� !� !� !� !���� !� !� !� !� � 1� � � �t�T�4� 0� 0� 0� 0� 0� 1� 1s�>A�A�Ar�)r�rbr�rdr�rfr-rv)rkrlrmr�r�rnr�r rr�r�rMr�r`rjrorPr9r�r��s�������� � ��G�����-1�O�1�1�1�1��%�U�D�9�9�9�E�9�9�9�9��5�e�Q�/�/�/�G�/�/�/�/�#�e��/�/�/�G�/�/�/�/�@E�� �D�A�A�A�J�����  �  �  �  �1�1�1�1�1�1rPr�r�r�r�r�r��$Generator[BlockingPortal, Any, None]c#� ����K�d �fd� �d ����fd� }t���t|d���}|��� d}����} |V�n#t$rd}�wxYw |�|j|��nA#t$rYn5wxYw# |�|j|��w#t$rYwwxYwxYw|���dS#|���wxYw) 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 :return: a context manager that yields a blocking portal .. versionchanged:: 3.0 Usage as a context manager is now required. r-rvc����K�t��4�d{V��}��|��|����d{V��ddd���d{V��dS#1�d{V��swxYwYdSrK)rIrTr�)�portal_rrs �r9� run_portalz)start_blocking_portal.<locals>.run_portal�s�����!�#�#� 0� 0� 0� 0� 0� 0� 0�w� � � �g� &� &� &��-�-�/�/� /� /� /� /� /� /� /� 0� 0� 0� 0� 0� 0� 0� 0� 0� 0� 0� 0� 0� 0� 0� 0� 0� 0� 0� 0� 0� 0� 0� 0���� 0� 0� 0� 0� 0� 0s�0A� A%�(A%c��������ra tj������dS#t$r:}����s��|��Yd}~dSYd}~dSd}~wwxYwdS)N)r�r�)r�rr:rUr�rV)rZr�r�rrr�s ����r9�run_blocking_portalz2start_blocking_portal.<locals>.run_blocking_portal�s���� � .� .� 0� 0� .� .�������������!� .� .� .��{�{�}�}�.��(�(��-�-�-�-�-�-�-�-�-�.�.�.�.�.�.����� .����  .� .s�0� A4�)A/�/A4T)�target�daemonFNr�) rr�startr[rUrhr�r5�join)r�r�r��thread�cancel_remaining_tasksrHrrr�s`` @@r9r�r��sx��������$0�0�0�0�0�0� .�.�.�.�.�.�.�.�.�&,�X�X�F� �.�t� <� <� <�F� �L�L�N�N�N��!&�������� ��L�L�L�L��� � � �%)� "� � ���� �  �� � �F�K�)?�@�@�@�@��� � � ��� ����� �� � �F�K�)?�@�@�@�@��� � � ��� ������� � � � � � � � ��� � � � � � ���sx� C,�!A&�%B%�&A4�4B%�9B�C,� B"�C,�!B"�"C,�%C�'C�C� C� C�C�C�C,�,Drvc�� tj}n#t$rtd��d�wxYw|���dS)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` r/N)rr2r4r5�check_cancelled)r8s r9r�r��s^���&2�&H� � �� ����� G� � �� ����� �!�!�#�#�#�#�#s� �*r�r�)r�N)r�r�r�r�r-r�r�)<� __future__r�sys�collections.abcrrr�concurrent.futuresr� contextlibrr r � dataclassesr r �inspectr � threadingrrr�typesr�typingrrrrr�_corer�_core._eventlooprrr�_core._synchronizationr� _core._tasksrr�abcr� abc._tasksr � version_infor#r$�typing_extensionsr%r&r(r:r?rArqrIr�r�r�rorPr9�<module>r�sP��"�"�"�"�"�"� � � � �:�:�:�:�:�:�:�:�:�:�%�%�%�%�%�%����������� )�(�(�(�(�(�(�(�������-�-�-�-�-�-�-�-�-�-���������������������������V�V�V�V�V�V�V�V�V�V�)�)�)�)�)�)�8�8�8�8�8�8�8�8�������"�"�"�"�"�"���w���+�+�+�+�+�+�+�+�+�6�6�6�6�6�6�6�6� �7�:� � ���w�v��&�&�&�� �<� � #� #��H�H�H�H�,G�G�G�G�,2*�2*�2*�2*�2*�7�4�=�2H�2*�2*�2*�j'�'�'�'�'� �'�'�'�z6�z6�z6�z6�z6�z6�z6�z6�z �51�51�51�51�51�51�51� ��51�p�GK�1�1�1�1���1�h$�$�$�$�$�$rP
Memory