
    h*                       S r SSKJr  SSKJrJr  SSKJrJrJ	r	J
r
Jr  SSKJrJrJr  SSKJr  SSKJrJr  SSKJr  SS	KJr  SS
KJr  \" S5      r\" S5      r " S S\5      r " S S\5      r\ " 5       r! " S S5      r"g)z
twisted.python.threadpool: a pool of threads to which we dispatch tasks.

In most cases you can just use C{reactor.callInThread} and friends
instead of creating a thread pool directly.
    )annotations)Threadcurrent_thread)AnyCallableListOptionalTypeVar)	ParamSpecProtocol	TypedDict)pool)contextlog)
deprecated)Failure)Version_P_Rc                      \ rS rSrSS jrSrg)_SupportsQsize   c                    g N selfs    `/root/1688_scrapy/alibaba-scraper/venv/lib/python3.13/site-packages/twisted/python/threadpool.pyqsize_SupportsQsize.qsize   s        r   Nreturnint__name__
__module____qualname____firstlineno__r   __static_attributes__r   r!   r   r   r      s    r!   r   c                  *    \ rS rSr% S\S'   S\S'   Srg)_State"   r$   minmaxr   N)r&   r'   r(   r)   __annotations__r*   r   r!   r   r,   r,   "   s    	H	Hr!   r,   c            
         \ rS rSrSrSrSrSrSrSr	\
r\" \" \" SSS	S
5      SS9" \5      5      r\" \5      r S     SS jjr\S S j5       r\S!S j5       r\S!S j5       r\S"S j5       r\rS#S jrS#S jrS$S jrS#S jrS%S jrS&S jr        S'S jr          S(S jr S#S jr! S)     S*S jjr"S#S jr#Sr$g)+
ThreadPool*   a  
This class (hopefully) generalizes the functionality of a pool of threads
to which work can be dispatched.

L{callInThread} and L{stop} should only be called from a single thread.

@ivar started: Whether or not the thread pool is currently running.
@type started: L{bool}

@ivar threads: List of workers currently running in this thread pool.
@type threads: L{list}

@ivar _pool: A hook for testing.
@type _pool: callable compatible with L{_pool}
      FNTwisted      r   zthreading.current_thread)versionreplacementc                   ^  US:  d   S5       eX::  d   S5       eUT l         UT l        UT l        / T l        SU 4S jjnSU 4S jjnT R	                  XT5      T l        g)	a#  
Create a new threadpool.

@param minthreads: minimum number of threads in the pool
@type minthreads: L{int}

@param maxthreads: maximum number of threads in the pool
@type maxthreads: L{int}

@param name: The name to give this threadpool; visible in log messages.
@type name: native L{str}
r   minimum is negativeminimum is greater than maximumc                    > TR                   " U STR                  5       0UD6nTR                  R                  U5        U$ )Nname)threadFactory_generateNamethreadsappend)akwthreadr   s      r   trackingThreadFactory2ThreadPool.__init__.<locals>.trackingThreadFactory`   sH    ''++-13F LL'Mr!   c                 @   > T R                   (       d  gT R                  $ )Nr   )startedr/   r   s   r   currentLimit)ThreadPool.__init__.<locals>.currentLimitg   s    <<88Or!   N)rD   r   rE   r   r#   r   r"   )r.   r/   r?   rB   _pool_team)r   
minthreads
maxthreadsr?   rG   rK   s   `     r   __init__ThreadPool.__init__J   sa     Q5 55'J)JJ'	%'		
 ZZD
r!   c                h    U R                   R                  5       nUR                  UR                  -   $ )z
For legacy compatibility purposes, return a total number of workers.

@return: the current number of workers, both idle and busy (but not
    those that have been quit by L{ThreadPool.adjustPoolsize})
@rtype: L{int}
)rN   
statisticsidleWorkerCountbusyWorkerCount)r   statss     r   workersThreadPool.workersn   s-     

%%'$$u'<'<<<r!   c                R    S/U R                   R                  5       R                  -  $ )z
For legacy compatibility purposes, return the number of busy workers as
expressed by a list the length of that number.

@return: the number of workers currently processing a work item.
@rtype: L{list} of L{None}
N)rN   rT   rV   r   s    r   workingThreadPool.workingz   s$     v

--/????r!   c                R    S/U R                   R                  5       R                  -  $ )z
For legacy compatibility purposes, return the number of idle workers as
expressed by a list the length of that number.

@return: the number of workers currently alive (with an allocated
    thread) but waiting for new work.
@rtype: L{list} of L{None}
N)rN   rT   rU   r   s    r   waitersThreadPool.waiters   s$     v

--/????r!   c                ,   ^   " U 4S jS5      nU" 5       $ )z
For legacy compatibility purposes, return an object with a C{qsize}
method that indicates the amount of work not yet allocated to a worker.

@return: an object with a C{qsize} method.
c                  &   > \ rS rSrSU 4S jjrSrg)$ThreadPool._queue.<locals>.NotAQueue   c                L   > TR                   R                  5       R                  $ )z
Pretend to be a Python threading Queue and return the
number of as-yet-unconsumed tasks.

@return: the amount of backlogged work not yet dispatched to a
    worker.
@rtype: L{int}
)rN   rT   backloggedWorkCount)qr   s    r   r   *ThreadPool._queue.<locals>.NotAQueue.qsize   s     zz,,.BBBr!   r   Nr"   r%   r   s   r   	NotAQueuerb      s    	C 	Cr!   rh   r   )r   rh   s   ` r   _queueThreadPool._queue   s    
	C 
	C {r!   c                    SU l         SU l        U R                  5         U R                  R	                  5       R
                  nU(       a  U R                  R                  U5        gg)z
Start the threadpool.
FTN)joinedrJ   adjustPoolsizerN   rT   re   grow)r   backlogs     r   startThreadPool.start   sN     **'')==JJOOG$ r!   c                :    U R                   R                  S5        g)zt
Increase the number of available workers for the thread pool by 1, up
to the maximum allowed by L{ThreadPool.max}.
r8   N)rN   rn   r   s    r   startAWorkerThreadPool.startAWorker   s    
 	

r!   c                ^    SU R                   =(       d    [        U 5       SU R                   3$ )zk
Generate a name for a new pool thread.

@return: A distinctive name for the thread.
@rtype: native L{str}
zPoolThread--)r?   idrX   r   s    r   rA   ThreadPool._generateName   s)     TYY2"T(31T\\NCCr!   c                :    U R                   R                  S5        g)zV
Decrease the number of available workers by 1, by quitting one as soon
as it's idle.
r8   N)rN   shrinkr   s    r   stopAWorkerThreadPool.stopAWorker   s    
 	

!r!   c                r    [        U SU5        [        R                  X R                  U R                  5        g )N__dict__)setattrr2   rQ   r.   r/   )r   states     r   __setstate__ThreadPool.__setstate__   s&    j%(D((DHH5r!   c                >    [        U R                  U R                  S9$ )N)r.   r/   )r,   r.   r/   r   s    r   __getstate__ThreadPool.__getstate__   s    $((11r!   c                2    U R                   " SU/UQ70 UD6  g)z
Call a callable object in a separate thread.

@param func: callable object to be called in separate thread

@param args: positional arguments to be passed to C{func}

@param kw: keyword args to be passed to C{func}
N)callInThreadWithCallback)r   funcargsrE   s       r   callInThreadThreadPool.callInThread   s     	%%dD>4>2>r!   c                   ^^^^^ U R                   (       a  g[        R                  R                  5       R                  S   mSU4S jjmUUUU4S jTl        UTl        U R                  R                  T5        g)a|  
Call a callable object in a separate thread and call C{onResult} with
the return value, or a L{twisted.python.failure.Failure} if the
callable raises an exception.

The callable is allowed to block, but the C{onResult} function must not
block and should perform as little work as possible.

A typical action for C{onResult} for a threadpool used with a Twisted
reactor would be to schedule a L{twisted.internet.defer.Deferred} to
fire in the main reactor thread using C{.callFromThread}.  Note that
C{onResult} is called inside the separate thread, not inside the
reactor thread.

@param onResult: a callable with the signature C{(success, result)}.
    If the callable returns normally, C{onResult} is called with
    C{(True, result)} where C{result} is the return value of the
    callable.  If the callable throws an exception, C{onResult} is
    called with C{(False, failure)}.

    Optionally, C{onResult} may be L{None}, in which case it is not
    called at all.

@param func: callable object to be called in separate thread

@param args: positional arguments to be passed to C{func}

@param kw: keyword arguments to be passed to C{func}
Nc                    >  TR                  5       n SnS Tl         TR                  b  TR                  X5        S Tl        g U(       d  [        R
                  " U 5        g g ! [         a    [        5       n Sn Ndf = f)NTF)theWorkBaseExceptionr   onResultr   err)resultok	inContexts     r   r   6ThreadPool.callInThreadWithCallback.<locals>.inContext  sw    "**,
 !%I!!-""2.%)	"  !  s   A! !A:9A:c                 :   > [         R                  " TT/T Q70 TD6$ r   )r   call)r   ctxr   rE   s   r   <lambda>5ThreadPool.callInThreadWithCallback.<locals>.<lambda>  s#    GLL%
%
 "%
r!   r#   None)	rl   r   theContextTrackercurrentContextcontextsr   r   rN   do)r   r   r   r   rE   r   r   s     ```@@r   r   #ThreadPool.callInThreadWithCallback   sX    H ;;''668AA"E	 $
	 &	

i r!   c                    SU l         SU l        U R                  R                  5         U R                   H  nUR                  5         M     g)z)
Shutdown the threads in the threadpool.
TFN)rl   rJ   rN   quitrB   join)r   rF   s     r   stopThreadPool.stop$  s7     

llFKKM #r!   c                   Uc  U R                   nUc  U R                  nUS:  d   S5       eX::  d   S5       eXl         X l        U R                  (       d  gU R                  U R                  :  a2  U R                  R                  U R                  U R                  -
  5        U R                  U R                   :  a3  U R                  R                  U R                   U R                  -
  5        gg)z
Adjust the number of available threads by setting C{min} and C{max} to
new values.

@param minthreads: The new value for L{ThreadPool.min}.

@param maxthreads: The new value for L{ThreadPool.max}.
Nr   r<   r=   )r.   r/   rJ   rX   rN   rz   rn   )r   rO   rP   s      r   rm   ThreadPool.adjustPoolsize.  s     JJQ5 55'J)JJ'|| <<$(("JJdllTXX56<<$(("JJOODHHt||34 #r!   c                    [         R                  " SU R                   35        [         R                  " SU R                   35        [         R                  " SU R                   35        g)z_
Dump some plain-text informational messages to the log about the state
of this L{ThreadPool}.
z	waiters: z	workers: ztotal: N)r   msgr^   r[   rB   r   s    r   	dumpStatsThreadPool.dumpStatsM  sI    
 	)DLL>*+)DLL>*+'$,,()r!   )rN   rl   r/   r.   r?   rJ   rB   )r4   r5   N)rO   r$   rP   r$   r?   zOptional[str]r"   )r#   z
list[None])r#   r   r   )r#   str)r   r,   r#   r   )r#   r,   )r   zCallable[_P, object]r   _P.argsrE   	_P.kwargsr#   r   )
r   z&Optional[Callable[[bool, _R], object]]r   zCallable[_P, _R]r   r   rE   r   r#   r   )NN)rO   Optional[int]rP   r   r#   r   )%r&   r'   r(   r)   __doc__r.   r/   rl   rJ   r?   r   r@   staticmethodr   r   r   currentThreadrM   rQ   propertyrX   r[   r^   ri   rf   rp   rs   rA   r{   r   r   r   r   r   rm   r   r*   r   r!   r   r2   r2   *   s     C
CFGDM Ir1a02	
 	M E PT"E"E/2"E?L"EH 	= 	= @ @ 	@ 	@  , 	A
%D62?(?18?@I?	??!8?! ?! 	?!
 ?! 
?!B MQ5'5<I5	5>*r!   r2   N)#r   
__future__r   	threadingr   r   typingr   r   r   r	   r
   typing_extensionsr   r   r   twisted._threadsr   rM   twisted.pythonr   r   twisted.python.deprecater   twisted.python.failurer   twisted.python.versionsr   r   r   r   r,   object
WorkerStopr2   r   r!   r   <module>r      sl   
 # , 9 9 < < * ' / * +t_T]X 
Y 
 X
j* j*r!   