o
    ¨Ýj:4  ã                   @   s  d dl mZ d dlZd dlmZ d dlmZ d dlmZ d dlmZ d dlm	Z	 d dl
mZ d d	lmZ d dlZg d
¢ZejZd)dd„Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZeZdd„ Zdd„ dde	jfdd e	jffD ƒ\ZZd!d"„ ZG d#d$„ d$ejƒZd%d&„ Zd'd(„ Z dS )*é    )ÚdequeN)ÚGreenletExit)Úevent)Úhubs)Úsupport)Útimeout)Útimer)Ú	greenlets)Ú
getcurrentÚsleepÚspawnÚspawn_nÚkillÚspawn_afterÚspawn_after_localÚGreenThreadc                 C   s\   t  ¡ }tƒ }|j|u r| dkrdS tdƒ‚| | |j¡}z| ¡  W | ¡  dS | ¡  w )a	  Yield control to another eligible coroutine until at least *seconds* have
    elapsed.

    *seconds* may be specified as an integer, or a float if fractional seconds
    are desired. Calling :func:`~greenthread.sleep` with *seconds* of 0 is the
    canonical way of expressing a cooperative yield. For example, if one is
    looping over a large list performing an expensive calculation without
    calling any socket methods, it's a good idea to call ``sleep(0)``
    occasionally; otherwise nothing else will run.
    r   Nz0do not call blocking functions from the mainloop)r   Úget_hubr
   ÚgreenletÚRuntimeErrorÚschedule_call_globalÚswitchÚcancel)ÚsecondsÚhubÚcurrentr   © r   úO/var/www/html/chatgpt/venv/lib/python3.10/site-packages/eventlet/greenthread.pyr      s   

r   c                 O   s*   t  ¡ }t|jƒ}| d|j| ||¡ |S )a”  Create a greenthread to run ``func(*args, **kwargs)``.  Returns a
    :class:`GreenThread` object which you can use to get the results of the
    call.

    Execution control returns immediately to the caller; the created greenthread
    is merely scheduled to be run at the next available opportunity.
    Use :func:`spawn_after` to  arrange for greenthreads to be spawned
    after a finite delay.
    r   ©r   r   r   r   r   r   )ÚfuncÚargsÚkwargsr   Úgr   r   r   r   2   s   

r   c                 O   s   t d| ||ƒd S )a°  Same as :func:`spawn`, but returns a ``greenlet`` object from
    which it is not possible to retrieve either a return value or
    whether it raised any exceptions.  This is faster than
    :func:`spawn`; it is fastest if there are no keyword arguments.

    If an exception is raised in the function, spawn_n prints a stack
    trace; the print can be disabled by calling
    :func:`eventlet.debug.hub_exceptions` with False.
    r   é   )Ú_spawn_n)r   r   r    r   r   r   r   B   s   
r   c                 O   ó*   t  ¡ }t|jƒ}| | |j|||¡ |S )ah  Spawns *func* after *seconds* have elapsed.  It runs as scheduled even if
    the current greenthread has completed.

    *seconds* may be specified as an integer, or a float if fractional seconds
    are desired. The *func* will be called with the given *args* and
    keyword arguments *kwargs*, and will be executed within its own greenthread.

    The return value of :func:`spawn_after` is a :class:`GreenThread` object,
    which can be used to retrieve the results of the call.

    To cancel the spawn and prevent *func* from being called,
    call :meth:`GreenThread.cancel` on the return value of :func:`spawn_after`.
    This will not abort the function if it's already started running, which is
    generally the desired behavior.  If terminating *func* regardless of whether
    it's started or not is the desired behavior, call :meth:`GreenThread.kill`.
    r   ©r   r   r   r    r   r!   r   r   r   r   O   ó   
r   c                 O   r$   )a+  Spawns *func* after *seconds* have elapsed.  The function will NOT be
    called if the current greenthread has exited.

    *seconds* may be specified as an integer, or a float if fractional seconds
    are desired. The *func* will be called with the given *args* and
    keyword arguments *kwargs*, and will be executed within its own greenthread.

    The return value of :func:`spawn_after` is a :class:`GreenThread` object,
    which can be used to retrieve the results of the call.

    To cancel the spawn and prevent *func* from being called,
    call :meth:`GreenThread.cancel` on the return value. This will not abort the
    function if it's already started running.  If terminating *func* regardless
    of whether it's started or not is the desired behavior, call
    :meth:`GreenThread.kill`.
    )r   r   r   r   Úschedule_call_localr   r%   r   r   r   r   f   r&   r   c                 O   s"   t jdtdd� t| |||ƒd S )Nzªcall_after_global is renamed to spawn_after, whichhas the same signature and semantics (plus a bit extra).  Please do a quick search-and-replace on your codebase, thanks!é   ©Ú
stacklevelr   )ÚwarningsÚwarnÚDeprecationWarningr#   )r   r   r   r    r   r   r   Úcall_after_global}   s
   ür.   c                 O   sH   t jdtdd� t ¡ }tj||jd�}|j| |jg|¢R i |¤Ž}|S )Nzocall_after_local is renamed to spawn_after_local, whichhas the same signature and semantics (plus a bit extra).r(   r)   ©Úparent)r+   r,   r-   r   r   r   r'   r   )r   Úfunctionr   r    r   r!   Útr   r   r   Úcall_after_local†   s   ýr3   c                 G   sH   t jdtdd� | d u rt | dd„ ¡S t ¡ }|j| tƒ j	g|¢R Ž S )NzJInstead of exc_after, which is deprecated, use Timeout(seconds, exception)r(   r)   c                   S   s   d S ©Nr   r   r   r   r   Ú<lambda>™   s    zexc_after.<locals>.<lambda>)
r+   r,   r-   r   ÚTimerr   r   r'   r
   Úthrow)r   Ú
throw_argsr   r   r   r   Ú	exc_after”   s   þr9   c                 c   s&   � | ]\}}}t  ||¡|ƒV  qd S r4   )r   Zwrap_deprecated)Ú.0ÚoldÚnewZfunr   r   r   Ú	<genexpr>Ÿ   s   € 
ÿr=   zgreenthread.TimeoutErrorÚTimeoutzgreenthread.with_timeoutÚwith_timeoutc                 C   s<   t  ¡ }tj||jd�}|j| |jg|¢R i |¤Ž}||fS )Nr/   )r   r   r   r   r   )r   r   r   r    r   r!   r2   r   r   r   r#   ¦   s   r#   c                   @   sX   e Zd Z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   zéThe GreenThread class is a type of Greenlet which has the additional
    property of being able to retrieve the return value of the main function.
    Do not construct GreenThread objects directly; call :func:`spawn` to get one.
    c                 C   s,   t j  | | j|¡ t ¡ | _d| _d | _d S )NF)r   Ú__init__Úmainr   ÚEventÚ_exit_eventÚ_resolving_linksÚ_exit_funcs)Úselfr0   r   r   r   r@   ³   s   

zGreenThread.__init__c                    sd   ddl m} t ¡ }t||ƒstdƒ‚|j ¡ ‰ ‡fdd„}ˆ  |¡ ‡ fdd„}ˆ 	|¡ ˆ  
¡ S )zS
        Enable ``GreenThread``s to be ``await``ed in ``async`` functions.
        r   )ÚHubzmThis API only works with eventlet's asyncio hub. To use it, set an EVENTLET_HUB=asyncio environment variable.c                    s"   |   ¡ rˆ jsˆ  ¡  d S d S d S r4   )Z	cancelledÚdeadr   ©Úfuture©rF   r   r   Úgot_future_resultÈ   s   þz0GreenThread.__await__.<locals>.got_future_resultc              
      sp   ˆ   ¡ rd S z|  ¡ }ˆ  |¡ W d S  ty    ˆ  ¡  Y d S  ty7 } zˆ  |¡ W Y d }~d S d }~ww r4   )ÚdoneÚwaitZ
set_resultr   r   ÚBaseExceptionZset_exception)ZgthreadÚresultÚerI   r   r   Úgot_gthread_resultÐ   s   €ÿz1GreenThread.__await__.<locals>.got_gthread_result)Zeventlet.hubs.asynciorG   r   r   Ú
isinstancer   ÚloopZcreate_futureZadd_done_callbackÚlinkÚ	__await__)rF   rG   r   rL   rR   r   )rJ   rF   r   rV   ¹   s   
ÿ


zGreenThread.__await__c                 C   s
   | j  ¡ S )aH   Returns the result of the main function of this GreenThread.  If the
        result is a normal return value, :meth:`wait` returns it.  If it raised
        an exception, :meth:`wait` will raise the same exception (though the
        stack trace will unavoidably contain some frames from within the
        greenthread module).)rC   rN   rK   r   r   r   rN   â   s   
zGreenThread.waitc                 O   s>   | j du r	tƒ | _ | j  |||f¡ | j ¡ r|  ¡  dS dS )aÓ   Set up a function to be called with the results of the GreenThread.

        The function must have the following signature::

            def func(gt, [curried args/kwargs]):

        When the GreenThread finishes its run, it calls *func* with itself
        and with the `curried arguments <http://en.wikipedia.org/wiki/Currying>`_ supplied
        at link-time.  If the function wants to retrieve the result of the GreenThread,
        it should call wait() on its first argument.

        Note that *func* is called within execution context of
        the GreenThread, so it is possible to interfere with other linked
        functions by doing things like switching explicitly to another
        greenthread.
        N)rE   r   ÚappendrC   ÚreadyÚ_resolve_links©rF   r   Zcurried_argsZcurried_kwargsr   r   r   rU   ê   s   

ÿzGreenThread.linkc                 O   s8   | j sdS z| j  |||f¡ W dS  ty   Y dS w )zn remove linked function set by :meth:`link`

        Remove successfully return True, otherwise False
        FT)rE   ÚremoveÚ
ValueErrorrZ   r   r   r   Úunlink  s   ÿzGreenThread.unlinkc                 C   sL   z	||i |¤Ž}W n   | j jt ¡ Ž  |  ¡  ‚ | j  |¡ |  ¡  d S r4   )rC   Zsend_exceptionÚsysÚexc_inforY   Úsend)rF   r1   r   r    rP   r   r   r   rA     s   zGreenThread.mainc                 C   sn   | j rd S | js
d S d| _ z%| jr-| j ¡ \}}}|| g|¢R i |¤Ž | jsW d| _ d S W d| _ d S d| _ w )NTF)rD   rE   Úpopleft)rF   ÚfÚcaZckwr   r   r   rY     s   þ
üzGreenThread._resolve_linksc                 G   ó   t | g|¢R Ž S )z²Kills the greenthread using :func:`kill`.  After being killed
        all calls to :meth:`wait` will raise *throw_args* (which default
        to :class:`greenlet.GreenletExit`).©r   ©rF   r8   r   r   r   r   '  s   zGreenThread.killc                 G   rd   )zìKills the greenthread using :func:`kill`, but only if it hasn't
        already started running.  After being canceled,
        all calls to :meth:`wait` will raise *throw_args* (which default
        to :class:`greenlet.GreenletExit`).)r   rf   r   r   r   r   -  s   zGreenThread.cancelN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r@   rV   rN   rU   r]   rA   rY   r   r   r   r   r   r   r   ­   s    )r   c                 G   s   | st | g|¢R Ž  dS dS )z¶Like :func:`kill`, but only terminates the greenthread if it hasn't
    already started execution.  If the grenthread has already started
    execution, :func:`cancel` has no effect.Nre   )r!   r8   r   r   r   r   5  s   ÿr   c                    s‚   | j rdS t ¡ }| s'‡ fdd„}|| _t| tƒr'z	|  |di ¡ W n   Y tƒ }||jur:| 	¡  | 
d|j¡ | jˆ Ž  dS )aê  Terminates the target greenthread by raising an exception into it.
    Whatever that greenthread might be doing; be it waiting for I/O or another
    primitive, it sees an exception right away.

    By default, this exception is GreenletExit, but a specific exception
    may be specified.  *throw_args* should be the same as the arguments to
    raise; either an exception instance or an exc_info tuple.

    Calling :func:`kill` causes the calling greenthread to cooperatively yield.
    Nc                     s   ˆ rˆ d   ˆ d ¡‚t ¡ ‚)Nr"   r(   )Úwith_tracebackr   r   )ÚaÚkw©r8   r   r   Ú
just_raiseO  s   zkill.<locals>.just_raiser   r   )rH   r   r   ÚrunrS   r   rA   r
   r   Zensure_greenletr   r   r7   )r!   r8   r   ro   r   r   rn   r   r   =  s    

r   )r   )!Úcollectionsr   r^   r   r   Zeventletr   r   r   r   Zeventlet.hubsr   Zeventlet.supportr	   r+   Ú__all__r
   r   r   r   r   r   r.   r3   Z
call_afterr9   r>   r?   ÚTimeoutErrorr#   r   r   r   r   r   r   r   Ú<module>   s>    
	

þ
ÿ 	