https://t.me/RX1948
Server : Apache
System : Linux iad1-shared-b8-43 6.6.49-grsec-jammy+ #10 SMP Thu Sep 12 23:23:08 UTC 2024 x86_64
User : dh_edsupp ( 6597262)
PHP Version : 8.2.26
Disable Function : NONE
Directory :  /lib/python3/dist-packages/asgiref/__pycache__/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : //lib/python3/dist-packages/asgiref/__pycache__/sync.cpython-310.pyc
o

�6�ahK�
@sTddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
mZmZddl
mZmZmZmZmZddlmZddlmZdd�Zd	ed
efdd�ZGd
d�d�ZGdd�d�ZGdd�d�ZeZe			dd	ddededd
eedefgeffdd��Ze		dd	edefdededd
efdd��Z			ddd�ZdS)�N)�Future�ThreadPoolExecutor)�Any�Callable�Dict�Optional�overload�)�CurrentThreadExecutor)�Localc	CsV|D]&}z|��|�|�kr|�|�|��Wqty(|�|�|��YqwdS�N)�get�set�LookupError)�context�cvar�r�./usr/lib/python3/dist-packages/asgiref/sync.py�_restore_contexts���r�func�returncCsVtjdkr
t�|�St�|�r|j}t�|�st|tj	�r&|j
}t|tj	�st�|�S)N)��)�sys�version_info�asyncio�iscoroutinefunction�inspect�ismethod�__func__�
isinstance�	functools�partialr)rrrr�_iscoroutinefunction_or_partials



��
r#c@s(eZdZdZdd�Zdd�Zdd�ZdS)	�ThreadSensitiveContextaiAsync context manager to manage context for thread sensitive mode

    This context manager controls which thread pool executor is used when in
    thread sensitive mode. By default, a single thread pool executor is shared
    within a process.

    In Python 3.7+, the ThreadSensitiveContext() context manager may be used to
    specify a thread pool per context.

    This context manager is re-entrant, so only the outer-most call to
    ThreadSensitiveContext will set the context.

    Usage:

    >>> import time
    >>> async with ThreadSensitiveContext():
    ...     await sync_to_async(time.sleep, 1)()
    cCs
d|_dSr)�token��selfrrr�__init__?s
zThreadSensitiveContext.__init__c�s6�ztj��W|Stytj�|�|_Y|Swr)�SyncToAsync�thread_sensitive_contextr
rrr%r&rrr�
__aenter__Bs���z!ThreadSensitiveContext.__aenter__c�s8�|jsdStj�|d�}|r|��tj�|j�dSr)r%r)�context_to_thread_executor�pop�shutdownr*�reset)r'�exc�value�tb�executorrrr�	__aexit__Js�z ThreadSensitiveContext.__aexit__N)�__name__�
__module__�__qualname__�__doc__r(r+r4rrrrr$+s
r$c@sNeZdZUdZiZded<e�Zddd�Zdd�Z	d	d
�Z
dd�Zd
d�ZdS)�AsyncToSynca�
    Utility class which turns an awaitable that only works on the thread with
    the event loop into a synchronous callable that works in a subthread.

    If the call stack contains an async loop, the code runs there.
    Otherwise, the code runs in a new loop in a new thread.

    Either way, this thread then pauses and waits to run any thread_sensitive
    code called from further down the call stack using SyncToAsync, before
    finally exiting once the async task returns.
    z,Dict[asyncio.Task[object], threading.Thread]�
launch_mapFcCs�t|�rt|�stjddd�||_z|jj|_Wn	ty"Ynw|r*d|_dSzt�	�|_WdSt
yYttj
dd�}|rS|t��krSttj
dd�|_YdSd|_YdSw)Nz4async_to_sync was passed a non-async-marked callable�)�
stacklevel�main_event_loop_pid�main_event_loop)�callabler#�warnings�warn�	awaitable�__self__�AttributeErrorr>r�get_running_loop�RuntimeError�getattrr)�threadlocal�os�getpid)r'rB�force_new_loopr=rrrr(hs0��
���zAsyncToSync.__init__c
	OsTzt��}Wn	tyYn	w|��rtd��t��g}t�}t��}t	|j
d�r/|j
j}nd}t�}||j
_z[|�
||||t��|�}	|jrM|j��sjt��}
tdd�}|�|j|
|	�}|re|�|�|��n|j�|jj|	�|rz|�|�Wt	|j
d�r�|j
`|r�||j
_t|d�|��St	|j
d�r�|j
`|r�||j
_t|d�w)NznYou cannot use AsyncToSync in the same thread as an async event loop - just await the async function directly.�currentr	��max_workersr)rrErF�
is_running�contextvars�copy_contextr�	threading�current_thread�hasattr�	executorsrLr
�	main_wrapr�exc_infor>�new_event_loopr�submit�_run_event_loop�run_until_future�result�call_soon_threadsafe�create_taskr)
r'�args�kwargs�
event_loopr�call_result�
source_thread�old_current_executor�current_executorrB�loop�
loop_executor�loop_futurerrr�__call__�s^��

�
�

�
��zAsyncToSync.__call__cs�t�|�zm|�|�Wz[t�|���D]}|��q�fdd�}|�|���D]}|��r0q)|��durA|�d|��|d��q)t|d�r[|�|�	��W|�
�t�|j�dSW|�
�t�|j�dS|�
�t�|j�wzYt�|���D]}|��q{�fdd�}|�|���D]}|��r�q�|��dur�|�d|��|d��q�t|d�r�|�|�	��W|�
�t�|j�wW|�
�t�|j�w|�
�t�|j�w)zP
        Runs the given event loop (designed to be called in a thread).
        c�s�tj�ddi�IdHdS)N�return_exceptionsT)r�gatherr��tasksrrrk�s�z+AsyncToSync._run_event_loop.<locals>.gatherNz(unhandled exception during loop shutdown)�message�	exception�task�shutdown_asyncgens)r�set_event_loop�run_until_complete�	all_tasks�cancel�	cancelledro�call_exception_handlerrTrq�closer>)r'rf�cororprkrrlrrZ�sn


���
���

���
��zAsyncToSync._run_event_loopcCst�|j|�}t�||j�S�z*
        Include self for methods
        )r!r"ri�update_wrapperrB)r'�parent�objtyperrrr�__get__�szAsyncToSync.__get__c

�s�|durt|d�t��}||j|<zaz*|dr3z|d�ty2|j|i|��IdH}Ynw|j|i|��IdH}WntyV}	z|�|	�WYd}	~	nd}	~	ww|�|�W|j|=t�	�|d<dSW|j|=t�	�|d<dS|j|=t�	�|d<w)zs
        Wraps the awaitable with something that puts the result into the
        result/exception future.
        Nrr	)
rr)�get_current_taskr:�
BaseExceptionrB�
set_exception�
set_resultrPrQ)
r'r_r`rbrcrWr�current_taskr\�errrrV�s4�
������zAsyncToSync.main_wrapN)F)
r5r6r7r8r:�__annotations__rrUr(rirZr~rVrrrrr9Ts


!E$r9c	@s�eZdZUdZdejvre��Ze�	e
eejd�d��iZde
d<e��Ze
dd�Ze�d�Zde
d<e�d	�Zd
e
d	<e��Zde
d<	
	ddedefdededddfdd�Zdd�Zdd�Zdd�Z e!dd��Z"dS) r)a
    Utility class which turns a synchronous callable into an awaitable that
    runs in a threadpool. It also sets a threadlocal inside the thread so
    calls to AsyncToSync can escape it.

    If thread_sensitive is passed, the code will run in the same thread as any
    outer code. This is needed for underlying Python code that is not
    threadsafe (for example, code which handles SQLite database connections).

    If the outermost program is async (i.e. SyncToAsync is outermost), then
    this will be a dedicated single sub-thread that all sync code runs in,
    one after the other. If the outermost program is sync (i.e. AsyncToSync is
    outermost), this will just be the main thread. This is achieved by idling
    with a CurrentThreadExecutor while AsyncToSync is blocking its sync parent,
    rather than just blocking.

    If executor is passed in, that will be used instead of the loop's default executor.
    In order to pass in an executor, thread_sensitive must be set to False, otherwise
    a TypeError will be raised.
    �ASGI_THREADSrMz,Dict[threading.Thread, asyncio.Task[object]]r:r	r*zcontextvars.ContextVar[str]�deadlock_contextzcontextvars.ContextVar[bool]z5weakref.WeakKeyDictionary[object, ThreadPoolExecutor]r,TNr.�thread_sensitiver3rrcCsxt|�rt|�rtd��||_t�||�||_tjj	|_	|r'|dur'td��||_
z|j|_WdSty;YdSw)Nz4sync_to_async can only be applied to sync functions.z6executor must not be set when thread_sensitive is True)
r?r#�	TypeErrorrr!r{�_thread_sensitiver�
coroutines�
_is_coroutine�	_executorrCrD)r'rr�r3rrrr(Ss
�zSyncToAsync.__init__c	�s`�t��}|jrQttjd�rtjj}nA|jr7|j�d�r7|j��}||j	vr,|j	|}n(t
dd�}||j	|<n|jrD|j�d�rDtd��|j
}|jrP|j�d�n|j}t��}tj|jg|�Ri|��}|j}|f}i}z3|�|tj|j||��t��|g|�Ri|���}	tj|	dd�IdH}
Wt|�|jr�|j�d�|
St|�|jr�|j�d�ww)NrLr	rMFz9Single thread executor already being used, would deadlockT)�timeout)rrEr�rTr9rUrLr*r
r,rr�rF�single_thread_executorrr�rPrQr!r"r�run�run_in_executor�thread_handlerrrrW�wait_forr)r'r_r`rfr3r*r�childr�future�retrrrrigsf�
�


��������zSyncToAsync.__call__cCst�|j|�Srz)r!r"ri)r'r|r}rrrr~�szSyncToAsync.__get__c	Os�||j_t��|j_t��}tj�	|�|krd}n||j|<d}z0|drBz|d�t
yA||i|��YW|r@|j|=SSw||i|��W|rP|j|=SS|rW|j|=w)zE
        Wraps the sync application with exception handling.
        FTr	)rHr>rIrJr=rRrSr9r:r
r�)	r'rf�source_taskrWrr_r`rS�
parent_setrrrr��s,

��
�
zSyncToAsync.thread_handlercCs zt��WStyYdSw)zi
        Implementation of asyncio.current_task()
        that returns None if there is no task.
        N)rr�rFrrrrr�s

�zSyncToAsync.get_current_task�TN)#r5r6r7r8rI�environr�get_event_looprf�set_default_executorr�intr:r�rR�localrHr�rP�
ContextVarr*r��weakref�WeakKeyDictionaryr,rr�boolrr(rir~r��staticmethodrrrrrr)sD

�
��
��
���
�@!r)Tr�r3r.cC�dSrr�rr�r3rrr�
sync_to_async��r�cCr�rrr�rrrr��r�cs$|dur��fdd�St|��d�S)Ncst|��d�S)N�r�r3�r))�f�r3r�rr�<lambda>�s
�zsync_to_async.<locals>.<lambda>r�r�r�rr�rr��s�)NTNr�)�asyncio.coroutinesrrPr!rrIrrRr@r��concurrent.futuresrr�typingrrrrr�current_thread_executorr
r�rrr�r#r$r9r)�
async_to_syncr�rrrr�<module>sd)FC������
����	�

https://t.me/RX1948 - 2025