
    !ni:4                     V   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Zd Zd Zd Zd Zd Zd ZeZd Zd dde	j4                  fdde	j6                  ffD        \  ZZd Z G d dej                        Zd Zd Z y)    )dequeN)GreenletExit)event)hubs)support)timeout)timer)	greenlets)
getcurrentsleepspawnspawn_nkillspawn_afterspawn_after_localGreenThreadc                 &   t        j                         }t               }|j                  |u r| dk  ryt	        d      |j                  | |j                        }	 |j                           |j                          y#  |j                          w xY 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	   s       T/home/homepc/tiktok-worker/venv/lib/python3.12/site-packages/eventlet/greenthread.pyr   r      su     ,,.ClG
||wa< MNN$$Wgnn=E

s   A= =Bc                     t        j                         }t        |j                        }|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   gs        r   r   r   2   s<     ,,.CCLL!AQ$f=H    c                 $    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"   s      r   r   r   B   s     AtT6*1--r$   c                     t        j                         }t        |j                        }|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#   s         r   r   r   O   s<    " ,,.CCLL!AWahhdFCHr$   c                     t        j                         }t        |j                        }|j	                  | |j
                  |||       |S )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)   s         r   r   r   f   s<    " ,,.CCLL!AGQXXtT6BHr$   c                 \    t        j                  dt        d       t        | |||      d   S )Nzcall_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"   s       r   call_after_globalr3   }   s2    MM	> 	q	*
 GT4033r$   c                     t        j                  dt        d       t        j                         }t        j
                  ||j
                        } |j                  | |j                  g|i |}|S )Nzocall_after_local is renamed to spawn_after_local, whichhas the same signature and semantics (plus a bit extra).r-   r.   parent)r0   r1   r2   r   r   r   r+   r   )r   functionr!   r"   r   r#   ts          r   call_after_localr9      sc    MM	Cq* ,,.C(3<<8ACDCFCAHr$   c                     t        j                  dt        d       | t        j                  | d       S t        j                         } |j                  | t               j                  g| S )NzJInstead of exc_after, which is deprecated, use Timeout(seconds, exception)r-   r.   c                       y N r=   r$   r   <lambda>zexc_after.<locals>.<lambda>   s    r$   )
r0   r1   r2   r	   Timerr   r   r+   r   throw)r   
throw_argsr   s      r   	exc_afterrB      s]    MM 0$4 {{7L11
,,.C"3""7JL,>,>LLLr$   c              #   \   K   | ]$  \  }}} t        j                  ||      |       & y wr<   )r   wrap_deprecated).0oldnewfuns       r   	<genexpr>rI      s0      /<sC%GC%c*s   *,zgreenthread.TimeoutErrorTimeoutzgreenthread.with_timeoutwith_timeoutc                     t        j                         }t        j                  ||j                        } |j                  | |j                  g|i |}||fS )Nr5   )r   r   r   r   r   )r   r    r!   r"   r   r#   r8   s          r   r'   r'      sN    
,,.C$s||4A   !((DTDVDAa4Kr$   c                   F    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zy)r   zThe 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                     t         j                   j                  | | j                  |       t        j                         | _        d| _        d | _        y )NF)r   __init__mainr   Event_exit_event_resolving_links_exit_funcs)selfr6   s     r   rO   zGreenThread.__init__   s<    ""4F; ;;= %r$   c                     ddl m} t        j                         }t	        ||      st        d      |j                  j                          fd}j                  |       fd} j                  |       j                         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                 b    | j                         rj                  sj                          y y y r<   )	cancelleddeadr   )futurerU   s    r   got_future_resultz0GreenThread.__await__.<locals>.got_future_result   s&    !$))		 +4!r$   c                     j                         ry 	 | j                         }j                  |       y # t        $ r j	                          Y y t
        $ r}j                  |       Y d }~y d }~ww xY wr<   )donewait
set_resultr   r   BaseExceptionset_exception)gthreadresulter[   s      r   got_gthread_resultz1GreenThread.__await__.<locals>.got_gthread_result   s_    {{}( !!&)    ($$Q''(s   !6 A4A4A//A4)eventlet.hubs.asynciorW   r   r   
isinstancer   loopcreate_futureadd_done_callbacklink	__await__)rU   rW   r   r\   rf   r[   s   `    @r   rm   zGreenThread.__await__   s}     	.lln#s#Q 
 '')	
 	  !23	( 			$%!!r$   c                 6    | j                   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).)rR   r_   )rU   s    r   r_   zGreenThread.wait   s     $$&&r$   c                     | j                   t               | _         | j                   j                  |||f       | j                  j	                         r| j                          yy)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)rT   r   appendrR   ready_resolve_linksrU   r    curried_argscurried_kwargss       r   rl   zGreenThread.link   sX    " #$wD|^ DE!!#! $r$   c                 z    | j                   sy	 | j                   j                  |||f       y# t        $ r Y yw xY w)zn remove linked function set by :meth:`link`

        Remove successfully return True, otherwise False
        FT)rT   remove
ValueErrorrs   s       r   unlinkzGreenThread.unlink  sE    
 	##T<$HI 		s   . 	::c                     	  ||i |}| j                   j                  |       | j                          y #   | j                   j                  t	        j
                           | j                           xY wr<   )rR   sendrr   send_exceptionsysexc_info)rU   r7   r!   r"   rd   s        r   rP   zGreenThread.main  sj    	"t.v.F !!&)!	+D++S\\^<!s	   6 >A4c                     | j                   ry | j                  sy d| _         	 | j                  r7| j                  j                         \  }}} || g|i | | j                  r7d| _         y # d| _         w xY w)NTF)rS   rT   popleft)rU   fcackws       r   rr   zGreenThread._resolve_links  sz       $	*""!--557
2s$##s# "" %*D!ED!s   AA. .	A7c                     t        | g| S )zKills the greenthread using :func:`kill`.  After being killed
        all calls to :meth:`wait` will raise *throw_args* (which default
        to :class:`greenlet.GreenletExit`).r   rU   rA   s     r   r   zGreenThread.kill'  s     D&:&&r$   c                     t        | g| S )zKills 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   r   s     r   r   zGreenThread.cancel-  s    
 d(Z((r$   N)__name__
__module____qualname____doc__rO   rm   r_   rl   ry   rP   rr   r   r   r=   r$   r   r   r      s5    
 '"R'".	"*')r$   r   c                 "    | st        | g|  yy)zLike :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.Nr   )r#   rA   s     r   r   r   5  s     Q r$   c                 `   | j                   ryt        j                         }| s0fd}|| _        t	        | t
              r	 | j                  |di        t               }||j                  ur,|j                          |j                  d|j                          | j                    y#  Y XxY w)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                  ^    rd   j                  d         t        j                         )Nr&   r-   )with_tracebackr   r   )akwrA   s     r   
just_raisezkill.<locals>.just_raiseO  s.     m22:a=AA++--r$   r=   r   )rZ   r   r   runrh   r   rP   r   r   ensure_greenletr   r   r@   )r#   rA   r   r   r   s    `   r   r   r   =  s     	vv
,,.C	.
 a%z2r* lGcll"  GNN3AGGZs   B) )B-)r   )!collectionsr   r}   r   r   eventletr   r   r   r   eventlet.hubsr	   eventlet.supportr
   r0   __all__r   r   r   r   r   r   r3   r9   
call_afterrB   rJ   rK   TimeoutErrorr'   r   r   r   r=   r$   r   <module>r      s     
 !      2 >   
: 
...4 
M	#Y@	#^W5I5IJA lE)(## E)P$r$   