
    &h                         S r SrSrSrSSKJrJr  SSKr\R                  r\	" 5       r
\	" 5       rSqSqSrSS jrS	 rSS
 jr " S S5      r " S S5      rSS jrg)z
* Experimental *

Like the map function, but can use a pool of threads.

Really easy to use threads.  eg.  tmap(f, alist)

If you know how to use the map function, you can use threads.
zRene Dudfieldz0.3.0zPython license    )QueueEmptyN@   c                 J    U (       a  U q O
[        5       q [        [         5      qg)zDoes a little test to see if threading is worth it.
  Sets up a global worker queue if it's worth it.

Calling init() is not required, but is generally better to do.
N)_use_workersbenchmark_workersWorkerQueue_wq)number_of_workerss    O/var/www/auris/envauris/lib/python3.13/site-packages/pygame/threads/__init__.pyinitr   '   s      ((* l
#C    c                  4    [         R                  5         Sq Sqg)zcleans up everything.NF)r
   stopr    r   r   quitr   8   s     HHJ
CLr   c                 R  ^ SSK mSSKmSSKnU (       d  U4S jnOU nU(       d-  [        S5       Vs/ s H  nTR	                  SSS5      PM     nnOUnUR                  5       S-   nSn[        S[
        5       H  n[        U5      n	UR                  5       n
[        S5       H.  n[        S	[        R                  " 5        35        [        X5U	S
9  M0     UR                  5       nU	R                  5         X-
  n[        SU SU S35        X:  a  UnUnX-
  S:  d  M    U$    U$ s  snf )a#  does a little test to see if workers are at all faster.
Returns the number of workers which works best.
Takes a little bit of time to run, so you should only really call
  it once.
You can pass in benchmark data, and functions if you want.
a_bench_func - f(data)
the_data - data to work on.
r   Nc                 <   > TR                   R                  U S5      $ )N)i   i@  )	transformscale)xpygames    r   doitbenchmark_workers.<locals>.doitV   s    ##))!Z88r   
   )   r       i    zactive count:)worker_queueztotal time num_workers:z: time::   )r   pygame.transformtimerangeSurfaceMAX_WORKERS_TO_TESTr	   print	threadingactive_counttmapr   )a_bench_functhe_datar#   r   r   thedatabestbest_numbernum_workerswqt1_t2
total_timer   s                 @r   r   r   @   s$     	9 >CBiHi6>>*a4iH99;"DK Q 34%YY[rAM)"8"8":!;<=R0  YY[
	W
'}GJ<qIJ%KD$q( / 5. ? Is   D$c                   <    \ rS rSrS
S jrS rS rS rS rS r	Sr
g	)r	      c                 R    [        5       U l        / U l        U R                  U5        g N)r   queuepool_setup_workers)selfr0   s     r   __init__WorkerQueue.__init__   s     W
	K(r   c                     / U l         [        U5       H/  nU R                   R                  [        U R                  S95        M1     U R                    H$  nUR                  S5        UR                  5         M&     g)zMSets up the worker threads
NOTE: undefined behaviour if you call this again.
)targetTN)r;   r$   appendThread
threadloop	setDaemonstart)r=   r0   r3   a_threads       r   r<   WorkerQueue._setup_workers   s]     	{#AIIV4??;< $ 		Ht$NN "r   c                 >    U R                   R                  XU45        g)z-puts a function on a queue for running later.N)r:   put)r=   fargskwArgss       r   doWorkerQueue.do   s    

()r   c                     U R                   R                  [        5        U R                   H  nUR	                  5         M     g)zAStops the WorkerQueue, waits for all of the threads to finish up.N)r:   rJ   STOPr;   join)r=   threads     r   r   WorkerQueue.stop   s)    

tiiFKKM  r   c                 X    U R                   R                  5       nU[        L a:  U R                   R                  [        5        U R                   R	                  5         g US   " US   0 US   D6  U R                   R	                  5         M  ! U R                   R	                  5         f = f)z*Loops until all of the tasks are finished.r   r!      N)r:   getrQ   rJ   	task_done)r=   rL   s     r   rD   WorkerQueue.threadloop   s    ::>>#Dt|

t$

$$&'Qa,DG, 

$$&  

$$&s    B B)c                 8    U R                   R                  5         g)z#waits until all tasks are complete.N)r:   rR   )r=   s    r   waitWorkerQueue.wait   s    

r   )r;   r:   N)r   )__name__
__module____qualname____firstlineno__r>   r<   rN   r   rD   r[   __static_attributes__r   r   r   r	   r	      s     )
*'r   r	   c                   (    \ rS rSrSrSS jrS rSrg)
FuncResult   zkUsed for wrapping up a function call so that the results are stored
inside the instances result attribute.
Nc                 D    Xl         SU l        SU l        X l        X0l        g)zf - is the function we that we call
callback(result) - this is called when the function(f) returns
errback(exception) - this is called when the function(f) raises
                       an exception.
N)rK   	exceptionresultcallbackerrback)r=   rK   rh   ri   s       r   r>   FuncResult.__init__   s!      r   c                 *    U R                   " U0 UD6U l        U R                  (       a  U R                  U R                  5        g g ! [         aA  nX0l        U R
                  (       a   U R                  U R                  5         S nAg  S nAg S nAff = fr9   )rK   rg   rh   	Exceptionrf   ri   )r=   rL   kwargses       r   __call__FuncResult.__call__   sl    	-&&$1&1DK}}dkk*  	-N||T^^,, 	-s   AA 
B2BB)rh   ri   rf   rK   rg   NN)r]   r^   r_   r`   __doc__r>   ro   ra   r   r   r   rc   rc      s    
	-r   rc   c                    U(       a  UnO.[         (       a  [         nOUS:X  a  [        X5      $ [        U5      n[        UR                  5      S:X  a  [        X5      $ / nU H2  nUR                  [        U 5      5        UR                  US   U5        M4     U(       a  UR                  5         UR                  R                  5       (       a  [        S5      eU(       dh  [         (       d]  UR                  5         UR                  R                  5       (       a.  UR                  R                  5       n	U	[        La  [        S5      eU(       a,  [        [!        S U5      5      n
U
(       a  U
S   R"                  eS U 5       $ Xg/$ )a  like map, but uses a thread pool to execute.
num_workers - the number of worker threads that will be used.  If pool
                is passed in, then the num_workers arg is ignored.
worker_queue - you can optionally pass in an existing WorkerQueue.
wait - True means that the results are returned when everything is finished.
       False means that we return the [worker_queue, results] right away instead.
       results, is returned as a list of FuncResult instances.
stop_on_error -
r   zbuggy threadmapc                     U R                   $ r9   )rf   )r   s    r   <lambda>tmap.<locals>.<lambda>
  s    q{{r   c              3   8   #    U  H  oR                   v   M     g 7fr9   )rg   ).0r   s     r   	<genexpr>tmap.<locals>.<genexpr>  s     *'Q's   )r
   mapr	   lenr;   rB   rc   rN   r[   r:   qsizeRuntimeErrorr   rW   rQ   listfilterrf   )rK   seq_argsr0   r   r[   stop_on_errorr1   resultssaum
error_oness              r   r*   r*      s2     3Ba1''[)B 277|q1 Gz!}%
gbk2  
	 88>>011CCGGIxx~~XX\\^T>&'899
 f%:GDEJ m---*'**=r   )r   rq   )r   NTT)rr   
__author____version____license__r:   r   r   r(   rC   objectrQ   FINISHr
   r   r&   r   r   r   r	   rc   r*   r   r   r   <module>r      s}    
   
		x	  
  $"<~. .b- -:Br   