o
    \"<f                     @   sh   d Z ddlmZ ddlmZ zddlZW n ey!   ddlZY nw G dd deZG dd de	Z
dS )	Pooling    )contextmanager)QueueNc                   @   s0   e Zd ZdZdddZedddZd	d
 ZdS )
ClientPoola?  Client pooling helper.

    This is mostly useful in threaded environments, because a client isn't
    thread-safe at all. Instead, what you want to do is have each thread use
    its own client, but you don't want to reconnect these all the time.

    The solution is a pool, and this class is a helper for that.

    >>> from pylibmc.test import make_test_client
    >>> mc = make_test_client()
    >>> pool = ClientPool()
    >>> pool.fill(mc, 4)
    >>> with pool.reserve() as mc:
    ...     mc.set("hi", "ho")
    ...     mc.delete("hi")
    ... 
    True
    True
    Nr   c                 C   s,   t | | |r|r| || d S d S d S N)r   __init__fill)selfmcn_slots r   D/var/www/html/kck/venv/lib/python3.10/site-packages/pylibmc/pools.pyr       s   zClientPool.__init__Fc              	   c   s0    |  |}z|V  W | | dS | | w )zContext manager for reserving a client from the pool.

        If *block* is given and the pool is exhausted, the pool waits for
        another thread to fill it before returning.
        N)getput)r	   blockr
   r   r   r   reserve%   s
   
zClientPool.reservec                 C   s    t |D ]	}| |  qdS )z/Fill *n_slots* of the pool with clones of *mc*.N)ranger   clone)r	   r
   r   ir   r   r   r   2   s   zClientPool.fill)Nr   )F)__name__
__module____qualname____doc__r   r   r   r   r   r   r   r   r      s    
r   c                       sH   e Zd ZdZ fddZdd Zedd Zedd	 Z	d
d Z
  ZS )ThreadMappedPoola_  Much like the *ClientPool*, helps you with pooling.

    In a threaded environment, you'd most likely want to have a client per
    thread. And there'd be no harm in one thread keeping the same client at all
    times. So, why not map threads to clients? That's what this class does.

    If a client is reserved, this class checks for a key based on the current
    thread, and if none exists, clones the master client and inserts that key.

    Of course this requires that you let the pool know when a thread is done
    with its reserved instance, so therefore ``relinquish`` must be called
    before thread exit.

    >>> from pylibmc.test import make_test_client
    >>> mc = make_test_client()
    >>> pool = ThreadMappedPool(mc)
    >>> with pool.reserve() as mc:
    ...     mc.set("hi", "ho")
    ...     mc.delete("hi")
    ... 
    True
    True
    c                    s   t  | S r   )super__new__)clsmaster	__class__r   r   r   P   s   zThreadMappedPool.__new__c                 C   s
   || _ d S r   )r   )r	   r   r   r   r   r   S   s   
zThreadMappedPool.__init__c                 C   s
   t  jS r   )	threadingcurrent_threadidentr	   r   r   r   current_keyV   s   
zThreadMappedPool.current_keyc              	   c   sF    | j }| |d}|du r| j }z
|V  W || |< dS || |< w )zReserve a client.

        Creates a new client based on the master client if none exists for the
        current thread.
        N)r$   popr   r   )r	   keyr
   r   r   r   r   Z   s   
zThreadMappedPool.reservec                 C   s   |  | jdS )zRelinquish any reserved client for the current context.

        Call this method before exiting a thread if it might potentially use
        this pool.
        N)r%   r$   r#   r   r   r   
relinquishj   s   zThreadMappedPool.relinquish)r   r   r   r   r   r   propertyr$   r   r   r'   __classcell__r   r   r   r   r   7   s    

r   )r   
contextlibr   queuer   r    ImportErrordummy_threadingr   dictr   r   r   r   r   <module>   s    ,