o
    ߥi(                     @   s   d dl Z d dlZd dlmZ d dlmZ d dlmZm	Z	m
Z
mZ ddgZe
dZG dd deZd	e	d
ef dededefddZdS )    N)ThreadPoolExecutor)copy_context)AnyCallableTypeVarcastr   run_in_threadpoolTc                       s    e Zd ZdZ fddZ  ZS )r   z\
    Thread pool with ContextVars

    - https://github.com/python/cpython/issues/78195
    c                    s&   t  jtt j|g|R i |S )N)supersubmit	functoolspartialr   run)self_ThreadPoolExecutor__fnargskwargs	__class__ E/home/ubuntu/.local/lib/python3.10/site-packages/baize/concurrency.pyr      s   zThreadPoolExecutor.submit)__name__
__module____qualname____doc__r   __classcell__r   r   r   r   r      s    __fn.r   r   returnc                    sL   t  }t }tj|j| g|R i |}|dttg t	f |I dH S )a  
    Asynchronously run function fn in a separate thread.
    Any *args and **kwargs supplied for this function are directly passed
    to fn. Also, the current :class:`contextvars.Context` is propogated,
    allowing context variables from the main thread to be accessed in the
    separate thread.

    https://github.com/python/cpython/blob/0f56263e62ba91d0baae40fb98947a3a98034a73/Lib/asyncio/threads.py
    N)
asyncioget_running_loopr   r   r   r   run_in_executorr   r   r	   )r   r   r   loopctx	func_callr   r   r   r      s
   
 )r   r   concurrent.futuresr   _ThreadPoolExecutorcontextvarsr   typingr   r   r   r   __all__r	   r   r   r   r   r   <module>   s    &