a cq@@sddlZddlmZddlmZmZmZmZddlm Z m Z ddl m Z ddl mZmZmZmZmZmZmZmZmZmZmZmZmZmZmZddlmZdd lm Z dd l!m"Z"m#Z#m$Z$dd l%m&Z&dd l'm(Z(m)Z)dd l*m+Z+edZ,edZ-edeeee,ffe.e,dddZ/edeeee,ffe.e,dddZ0ede,fe.e,dddZ1ede,fe.e,dddZ2Gddde Z3Gddde+Z4GdddZ5e5d d!d"Z6e d'e7eee7efee5edfd$d%d&Z8dS)(N) iscoroutine)FIRST_COMPLETEDFutureThreadPoolExecutorwait)AbstractContextManagercontextmanager) TracebackType)AnyAsyncContextManagerCallableContextManager CoroutineDict GeneratorIterableOptionalTupleTypeTypeVarUnioncastoverload)warn) _eventloop) get_asynclibget_cancelled_exc_class threadlocals)Event) CancelScopecreate_task_group) TaskStatusT_RetvalT_co.funcargsreturncGs8z tj}Wnty$tdYn0|j|g|RS)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 thread)rcurrent_async_moduleAttributeError RuntimeErrorrun_async_from_threadr&r'Zasynclibr/^/workspaces/shunt/resources/test-fastapi/venv/lib/python3.9/site-packages/anyio/from_thread.pyrun#s   r1cGstdtt|g|RS)NzPrun_async_from_thread() has been deprecated, use anyio.from_thread.run() instead)rDeprecationWarningr1r&r'r/r/r0r-4s r-cGs8z tj}Wnty$tdYn0|j|g|RS)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))rr*r+r,run_sync_from_threadr.r/r/r0run_sync>s   r5cGstdtt|g|RS)NzTrun_sync_from_thread() has been deprecated, use anyio.from_thread.run_sync() instead)rr2r5r3r/r/r0r4Os r4c@seZdZUeed<eed<eed<dZeee e ee ee fed<e e dddd Zeed d d Ze d d dZee e ee ee eedddZdS)_BlockingAsyncContextManager _enter_future _exit_future _exit_event)NNN_exit_exc_infoBlockingPortal)async_cmportalcCs||_||_dSN) _async_cm_portal)selfr<r=r/r/r0__init___sz%_BlockingAsyncContextManager.__init__r(c szt|_|jIdH}Wn2tyN}z|j|WYd}~nd}~00|j|z*|jIdHW|jj |j IdH}|S|jj |j IdH}|YS0dSr>) rr9r? __aenter__ BaseExceptionr7 set_exception set_resultr __aexit__r:)rAvalueexcresultr/r/r0 run_async_cmcs  z)_BlockingAsyncContextManager.run_async_cmcCs,t|_|j|j|_|j}tt|Sr>) rr7r@start_task_soonrLr8rKrr$rAcmr/r/r0 __enter__zs z&_BlockingAsyncContextManager.__enter__)&_BlockingAsyncContextManager__exc_type'_BlockingAsyncContextManager__exc_value'_BlockingAsyncContextManager__tracebackr(cCs&|||f|_|j|jj|jSr>)r:r@callr9setr8rK)rArQrRrSr/r/r0__exit__s z%_BlockingAsyncContextManager.__exit__N)__name__ __module__ __qualname__r__annotations__rr:rrrrEr r r$rBboolrLrPrVr/r/r/r0r6Ws  r6c@s,eZdZedddZdeddddZdS) _BlockingPortalTaskStatusfuturecCs ||_dSr>)_future)rAr^r/r/r0rBsz"_BlockingPortalTaskStatus.__init__N)rIr(cCs|j|dSr>)r_rG)rArIr/r/r0startedsz!_BlockingPortalTaskStatus.started)N)rWrXrYrrBobjectr`r/r/r/r0r\sr\c@seZdZdZddddZddddZdddd Zeee ee ee ee d d d Z ddd dZ ddddZd2e ddddZeeeeefeddddZeeeeefeeddddZeedeeeeffeedddZeedefeedddZedeeeeefeffeedd dZedd!edeeeeffeed"d#d$d%Zedd!edefeed"d#d&d%Zdd!edeeeeefeffeed"d#d'd%Zedd!edeeeeffeed"d#d(d)Zedd!edefeed"d#d*d)Zdd!edeeeeefeffeed"d#d+d)Zdd!edeeeeffeee d,efd#d-d.Z!e"e#e$e#d/d0d1Z%dS)3r;zLAn object that lets external threads run code in an asynchronous event loop.rCcCs tSr>)rr;)clsr/r/r0__new__szBlockingPortal.__new__NcCs&t|_t|_t|_t|_dSr>) threading get_ident_event_loop_thread_idr _stop_eventr! _task_groupr_cancelled_exc_classrAr/r/r0rBs zBlockingPortal.__init__cs|jIdH|Sr>)rhrDrjr/r/r0rDszBlockingPortal.__aenter__)exc_typeexc_valexc_tbr(cs$|IdH|j|||IdHSr>)stoprhrH)rArkrlrmr/r/r0rHszBlockingPortal.__aexit__cCs,|jdurtd|jtkr(tddS)NzThis portal is not runningz7This method cannot be called from the event loop thread)rfr,rdrerjr/r/r0_check_runnings  zBlockingPortal._check_runningcs|jIdHdS)z#Sleep until :meth:`stop` is called.N)rgrrjr/r/r0sleep_until_stoppedsz"BlockingPortal.sleep_until_stoppedF)cancel_remainingr(cs$d|_|j|r |jjdS)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)rfrgrUrhZ cancel_scopecancel)rArqr/r/r0rns  zBlockingPortal.stop)r&r'kwargsr^r(c stddfdd }zzf||i|}t|r|t6|rJn |||IdH}Wdn1sr0YWn\jy|YnTty}z*|s||t |t sƂWYd}~nd}~00|s| |Wdnd0dS)N)fr(cs*|r&jdtfvr&jdSr>) cancelledrfrdrerTrr)rtZscoperAr/r0callbacks  z+BlockingPortal._call_func..callback) rrr rurradd_done_callbackrirErF isinstance ExceptionrG)rAr&r'rsr^rwretvalrJr/rvr0 _call_funcs&  ,    zBlockingPortal._call_func)r&r'rsnamer^r(cCstdS)a% Spawn a new task using the given callable. Implementors 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 N)NotImplementedError)rAr&r'rsr}r^r/r/r0_spawn_task_from_threadsz&BlockingPortal._spawn_task_from_thread.r%cGsdSr>r/rAr&r'r/r/r0rTszBlockingPortal.callcGsdSr>r/rr/r/r0rTscGstt|j|g|RS)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#rMrKrr/r/r0rT s)r}zFuture[T_Retval])r&r'r}r(cGsdSr>r/rAr&r}r'r/r/r0 spawn_taskszBlockingPortal.spawn_taskcGsdSr>r/rr/r/r0r&scGs"tdt|j|g|Rd|iS)a Start a task in the portal's task group. :param func: the target coroutine 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 .. versionadded:: 2.1 .. deprecated:: 3.0 Use :meth:`start_task_soon` instead. If your code needs AnyIO 2 compatibility, you can keep using this until AnyIO 4. z;spawn_task() is deprecated -- use start_task_soon() insteadr})rr2rMrr/r/r0r,s cGsdSr>r/rr/r/r0rMIszBlockingPortal.start_task_sooncGsdSr>r/rr/r/r0rMRscGs$|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 coroutine 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 .. versionadded:: 3.0 )rorr)rAr&r}r'rtr/r/r0rMXsz Future[Any]cs\tddfdd }|tt}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:`TaskGroup.start`. :param func: the target coroutine 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 .. versionadded:: 3.0 N)r^r(csHsD|rn*|r2|ntd}|dS)Nz1Task exited without calling task_status.started())donerurr exceptionrFr,)r^rJZtask_status_futurer/r0 task_dones z,BlockingPortal.start_task..task_done task_status)rror\rxrrK)rAr&r}r'rrrtr/rr0 start_taskts  zBlockingPortal.start_task)rOr(cCs 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 )r6rNr/r/r0wrap_async_context_managersz)BlockingPortal.wrap_async_context_manager)F)&rWrXrY__doc__rcrBrDrrrEr r[rHrorprnr tuplerstrr rr|rarrrr#rTrrrMrrr r$r rr/r/r/r0r;s   %        *r;rCcCstdttS)aL Create a portal for running functions in the event loop thread from external threads. Use this function in asynchronous code when you need to allow external threads access to the event loop where your asynchronous code is currently running. .. deprecated:: 3.0 Use :class:`.BlockingPortal` directly. z_create_blocking_portal() has been deprecated -- use anyio.from_thread.BlockingPortal() directly)rr2r;r/r/r/r0create_blocking_portals rasyncio)backendbackend_optionsr(c #sddfdd }ttd}|jtj|||d}ztttt|gtdWn$t yx | Yn0 rƈ }z |VWn"t y| |jdYn0| |jd | Wdn1s0YdS) 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. NrCc s^t4IdH6}r0||IdHWdIdHqZ1IdHsP0YdSr>)r;Zset_running_or_notify_cancelrGrp)Zportal_r]r/r0 run_portals z)start_blocking_portal..run_portalr)rr)Z return_whenTF)rrZsubmitrr1rrrrrErrrrKrTrn)rrrexecutorZ run_futurer=r/r]r0start_blocking_portals6     r)rN)9rdrrconcurrent.futuresrrrr contextlibrrtypesr typingr r r r rrrrrrrrrrrwarningsrZ_corerZ_core._eventlooprrrZ_core._synchronizationrZ _core._tasksr r!Z abc._tasksr"r#r$rar1r-r5r4r6r\r;rrrr/r/r/r0s>  D    $ 4