Format: application/x-python-code
1 o
2
3 -h� � @ s� d dl Z d dlZd dlZd dlmZmZ d dlmZmZm Z m
4 Z
5 mZ ejdkr.d dlm
6 Z
7 nd dlm
8 Z
9 e
10 d�Ze
11 d�Ze
12 d�ZG d d
13 � d
14 �ZG dd� de�ZdS )
15 � N)�Executor�Future)�
16 TYPE_CHECKING�Any�Callable�TypeVar�Union)� �
17 )� ParamSpec�_T�_P�_Rc @ s@ e Zd ZdZdddeeef dejdejfdd�Z d
18 dd�Z
19 d
20 S )� _WorkItemz�
21 Represents an item needing to be run in the executor.
22 Copied from ThreadPoolExecutor (but it's private, so we're not going to rely on importing it)
23 �future�
24 Future[_R]�fn�args�kwargsc O s || _ || _|| _|| _d S �N)r r r r )�selfr r r r � r �UC:\Users\shubh\Videos\agent\nova\Lib\site-packages\asgiref/current_thread_executor.py�__init__ s
25 z_WorkItem.__init__�returnNc
26 C sr d}| j �� s d S z| j| ji | j��}W n ty0 } z| j �|� d } W Y d }~d S d }~ww | j �|� d S )NT)r �set_running_or_notify_cancelr r r �
27 BaseException�
28 set_exception�
29 set_result)r Z__traceback_hide__�result�excr r r �run# s
30 ��z
31 _WorkItem.run�r N)�__name__�
32 __module__�__qualname__�__doc__r r
33 r r r r r! r r r r r s �
34 ��
35 �r c @ sZ e Zd ZdZddd�Zddd �Zd
36 eeef dej dej
37 dd
38 fdd�Zes+dd� Z
39 dS dS )�CurrentThreadExecutorz�
40 An Executor that actually runs code in the thread it is instantiated in.
41 Passed to other threads running async code, so they can run sync code in
42 the thread they came from.
43 r Nc C s t �� | _t�� | _d| _d S )NF)� threading�current_thread�_work_thread�queue�Queue�_work_queue�_broken)r r r r r 8 s
44
45
46 zCurrentThreadExecutor.__init__r �Future[Any]c C sh t �� | jkrtd��|�| jj� z | j�� }||u r#W d| _dS t |t
47 �s*J �|�� ~qd| _w )z�
48 Runs the code in the work queue until a result is available from the future.
49 Should be run from the thread the executor is initialised in.
50 z<You cannot run CurrentThreadExecutor from a different threadTN)r( r) r* �RuntimeError�add_done_callbackr- �put�getr. �
51 isinstancer r! )r r � work_itemr r r �run_until_future= s �
52
53 �� z&CurrentThreadExecutor.run_until_futurer r r r c O sR t �� | jkrtd��| jrtd��t� }t||g|�R i |��}| j�|� |S )Nz@You cannot submit onto CurrentThreadExecutor from its own threadz/CurrentThreadExecutor already quit or is broken) r( r) r* r0 r. r r r- r2 )r r r r �fr5 r r r �_submitV s �zCurrentThreadExecutor._submitc O s | j |g|�R i |��S r )r8 )r r r r r r r �submitr s zCurrentThreadExecutor.submitr"