U
    @f+&                     @   sd   d dl Z d dlZd dlmZ d dlmZ ddgZdZG dd dZG dd dZ	G d	d
 d
e	Z
dS )    N)queue)	greenlets	GreenPool	GreenPileTc                   @   sz   e Zd ZdZdddZdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd ZdS )r   z4The GreenPool class is a pool of green threads.
      c              
   C   s   zt |}W n> tk
rJ } z dt|t|}t|W 5 d }~X Y nX |dk rjdt|}t||| _t | _	t
|| _t
 | _d S )Nz-GreenPool() expect size :: int, actual: {} {}r   z(GreenPool() expect size >= 0, actual: {})int
ValueErrorformattypestr	TypeErrorreprsizesetcoroutines_runningeventlet	SemaphoresemEventno_coros_running)selfr   emsg r   L/var/www/html/chatgpt/venv/lib/python3.8/site-packages/eventlet/greenpool.py__init__   s    zGreenPool.__init__c                 C   s$   || j  }| j j|7  _|| _ dS )a   Change the max number of greenthreads doing work at any given time.

        If resize is called when there are more than *new_size* greenthreads
        already working on tasks, they will be allowed to complete but no new
        tasks will be allowed to get launched until enough greenthreads finish
        their tasks to drop the overall quantity below *new_size*.  Until
        then, the return value of free() will be negative.
        N)r   r   counter)r   new_sizeZ
size_deltar   r   r   resize   s    	
zGreenPool.resizec                 C   s
   t | jS )zd Returns the number of greenthreads that are currently executing
        functions in the GreenPool.)lenr   r   r   r   r   running+   s    zGreenPool.runningc                 C   s   | j jS )z Returns the number of greenthreads available for use.

        If zero or less, the next call to :meth:`spawn` or :meth:`spawn_n` will
        block the calling greenthread until a slot becomes available.)r   r   r    r   r   r   free0   s    zGreenPool.freec                 O   s   t  }| j r:|| jkr:t j|}|||| |S | j  t j	|f||}| jsft 
 | _| j| || j |S )a
  Run the *function* with its arguments in its own green thread.
        Returns the :class:`GreenThread <eventlet.GreenThread>`
        object that is running the function, which can be used to retrieve the
        results.

        If the pool is currently at capacity, ``spawn`` will block until one of
        the running greenthreads completes its task and frees up a slot.

        This function is reentrant; *function* can call ``spawn`` on the same
        pool without risk of deadlocking the whole thing.
        )r   
getcurrentr   lockedr   ZgreenthreadZGreenThreadmainacquirespawnr   r   addlink_spawn_done)r   functionargskwargscurrentgtr   r   r   r'   7   s    

zGreenPool.spawnc                 C   sv   d zHz||| W n4 tttjfk
r0    Y n   trBt	  Y nX W 5 |d kr\  d S t  }| | X  d S N)
r   r#   r*   KeyboardInterrupt
SystemExitgreenletZGreenletExitDEBUG	traceback	print_exc)r   funcr,   r-   coror   r   r   _spawn_n_implT   s    zGreenPool._spawn_n_implc                 O   sl   t  }| j r.|| jkr.| |||d n:| j  t | j|||d}| js\t  | _	| j
| dS )zCreate a greenthread to run the *function*, the same as
        :meth:`spawn`.  The difference is that :meth:`spawn_n` returns
        None; the results of *function* are not retrievable.
        NT)r   r#   r   r$   r   r9   r&   spawn_nr   r   r(   )r   r+   r,   r-   r.   gr   r   r   r:   d   s    
   
zGreenPool.spawn_nc                 C   s,   t  | jkstd|  r(| j  dS )z>Waits until all greenthreads in the pool are finished working.zWCalling waitall() from within one of the GreenPool's greenthreads will never terminate.N)r   r#   r   AssertionErrorr!   r   waitr    r   r   r   waitallw   s
    zGreenPool.waitallc                 C   s<   | j   |d k	r| j| | j j| jkr8| jd  d S r0   )r   releaser   removebalancer   r   send)r   r8   r   r   r   r*      s
    
zGreenPool._spawn_donec                 C   s   | j jdk r| j j S dS dS )z<Return the number of greenthreads waiting to spawn.
        r   N)r   rA   r    r   r   r   waiting   s    
zGreenPool.waitingc                 C   s&   |D ]}|j |f|  q|  d S r0   )r'   done_spawning)r   r7   itgir,   r   r   r   _do_map   s    zGreenPool._do_mapc                 C   s0   |dkrdd }t | j}t| j||| |S )au  This is the same as :func:`itertools.starmap`, except that *func* is
        executed in a separate green thread for each item, with the concurrency
        limited by the pool's size. In operation, starmap consumes a constant
        amount of memory, proportional to the size of the pool, and is thus
        suited for iterating over extremely long input lists.
        Nc                  W   s   | S r0   r   )ar   r   r   <lambda>       z#GreenPool.starmap.<locals>.<lambda>)GreenMapr   r   r:   rG   )r   r+   iterablerF   r   r   r   starmap   s
    

zGreenPool.starmapc                 G   s   |  |t| S )a  This is the same as :func:`itertools.imap`, and has the same
        concurrency and memory behavior as :meth:`starmap`.

        It's quite convenient for, e.g., farming out jobs from a file::

           def worker(line):
               return do_something(line)
           pool = GreenPool()
           for result in pool.imap(worker, open("filename", 'r')):
               print(result)
        )rM   zip)r   r+   	iterablesr   r   r   imap   s    zGreenPool.imapN)r   )__name__
__module____qualname____doc__r   r   r!   r"   r'   r9   r:   r>   r*   rC   rG   rM   rP   r   r   r   r   r      s   
	c                   @   s>   e Zd ZdZdddZdd Zdd Zd	d
 ZeZdd Z	dS )r   a  GreenPile is an abstraction representing a bunch of I/O-related tasks.

    Construct a GreenPile with an existing GreenPool object.  The GreenPile will
    then use that pool's concurrency as it processes its jobs.  There can be
    many GreenPiles associated with a single GreenPool.

    A GreenPile can also be constructed standalone, not associated with any
    GreenPool.  To do this, construct it with an integer size parameter instead
    of a GreenPool.

    It is not advisable to iterate over a GreenPile in a different greenthread
    than the one which is calling spawn.  The iterator will exit early in that
    situation.
    r   c                 C   s0   t |tr|| _n
t|| _t | _d| _d S )Nr   )
isinstancer   poolr   
LightQueuewaitersr   r   Zsize_or_poolr   r   r   r      s
    


zGreenPile.__init__c                 O   sT   |  j d7  _ z$| jj|f||}| j| W n   |  j d8  _  Y nX dS )znRuns *func* in its own green thread, with the result available by
        iterating over the GreenPile object.   N)r   rV   r'   rX   put)r   r7   r,   kwr/   r   r   r   r'      s    zGreenPile.spawnc                 C   s   | S r0   r   r    r   r   r   __iter__   s    zGreenPile.__iter__c                 C   s   | j dkrt |  S )zWait for the next result, suspending the current greenthread until it
        is available.  Raises StopIteration when there are no more results.r   )r   StopIteration_nextr    r   r   r   next   s    
zGreenPile.nextc              	   C   s(   z| j  W S |  j d8  _ X d S )NrZ   )r   rX   getr=   r    r   r   r   r_      s    zGreenPile._nextN)r   )
rQ   rR   rS   rT   r   r'   r]   r`   __next__r_   r   r   r   r   r      s   
c                       s0   e Zd Z fddZdd Zdd ZeZ  ZS )rK   c                    s"   t  | tj| jjd| _d S )N)maxsize)superr   r   rW   rV   r   rX   rY   	__class__r   r   r      s    zGreenMap.__init__c                 C   s   |  dd  d S )Nc                   S   s   t  S r0   )r^   r   r   r   r   rI      rJ   z(GreenMap.done_spawning.<locals>.<lambda>)r'   r    r   r   r   rD      s    zGreenMap.done_spawningc                 C   s    |   }t|tr|n|S d S r0   )r_   rU   r^   )r   valr   r   r   r`      s    
zGreenMap.next)rQ   rR   rS   r   rD   r`   rb   __classcell__r   r   re   r   rK      s   rK   )r5   r   r   Zeventlet.supportr   r3   __all__r4   r   r   rK   r   r   r   r   <module>   s    /8