
    i                         d Z dZdZdZddlmZmZ ddlZej                  Z e	       Z
 e	       ZdadadZddZd	 Zdd
Z G d d      Z G d d      ZddZy)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                 @    | r| a n
t               a t        t               ay)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    a/home/obispo/Crisostomo_bridge/mision_env/lib/python3.12/site-packages/pygame/threads/__init__.pyinitr   '   s      ((* l
#C    c                  4    t         j                          da day)zcleans up everything.NF)r
   stopr    r   r   quitr   8   s     HHJ
CLr   c                 6   ddl ddlddl}| sfd}n| }|s*t        d      D cg c]  }j	                  ddd       }}n|}|j                         dz   }d}t        dt
              D ]  }t        |      }	|j                         }
t        d      D ]0  }t        d	t        j                                 t        |||	
       2 |j                         }|	j                          ||
z
  }t        d| d| d       ||k  r|}|}||z
  dkD  s |S  |S c c}w )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                 <    j                   j                  | d      S )N)i   i@  )	transformscale)xpygames    r   doitz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   @   s3     	9 >CBiH6>>*a4HH99;"DK Q 34 %YY[r 	1AM)"8"8":!;<=wR0	1 YY[
	"W
'}GJ<qIJ%KD$q( /. ? Is   Dc                   2    e Zd ZddZd Zd Zd Zd Zd Zy)	r	   c                 R    t               | _        g | _        | j                  |       y N)r   queuepool_setup_workers)selfr/   s     r   __init__zWorkerQueue.__init__   s     W
	K(r   c                     g | _         t        |      D ]1  }| j                   j                  t        | j                               3 | j                   D ]#  }|j                  d       |j                          % y)z]Sets up the worker threads
        NOTE: undefined behaviour if you call this again.
        )targetTN)r9   r#   appendThread
threadloop	setDaemonstart)r;   r/   r2   a_threads       r   r:   zWorkerQueue._setup_workers   sg     	{# 	=AIIV4??;<	= 		 	Ht$NN	r   c                 @    | j                   j                  |||f       y)z-puts a function on a queue for running later.N)r8   put)r;   fargskwArgss       r   dozWorkerQueue.do   s    

4()r   c                     | j                   j                  t               | j                  D ]  }|j	                           y)zAStops the WorkerQueue, waits for all of the threads to finish up.N)r8   rF   STOPr9   join)r;   threads     r   r   zWorkerQueue.stop   s.    

tii 	FKKM	r   c                 T   	 | j                   j                         }|t        u r:| j                   j                  t               | j                   j	                          y	  |d   |d   i |d    | j                   j	                          # | j                   j	                          w xY w)z*Loops until all of the tasks are finished.r   r       N)r8   getrL   rF   	task_done)r;   rH   s     r   rA   zWorkerQueue.threadloop   s    ::>>#Dt|

t$

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

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

$$&s   B B'c                 8    | j                   j                          y)z#waits until all tasks are complete.N)r8   rM   )r;   s    r   waitzWorkerQueue.wait   s    

r   N)r   )	__name__
__module____qualname__r<   r:   rJ   r   rA   rT   r   r   r   r	   r	      s     )
*'r   r	   c                       e Zd ZdZddZd Zy)
FuncResultzsUsed for wrapping up a function call so that the results are stored
    inside the instances result attribute.
    Nc                 J    || _         d| _        d| _        || _        || _        y)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)rG   	exceptionresultcallbackerrback)r;   rG   r]   r^   s       r   r<   zFuncResult.__init__   s'      r   c                    	  | j                   |i || _        | j                  r| j                  | j                         y y # t        $ r=}|| _        | j
                  r | j                  | j                         Y d }~y Y d }~y d }~ww xY wr7   )rG   r\   r]   	Exceptionr[   r^   )r;   rH   kwargses       r   __call__zFuncResult.__call__   sp    	- $&&$1&1DK}}dkk*  	-DN||T^^,, 	-s   >A 	B.BBNN)rU   rV   rW   __doc__r<   rc   r   r   r   rY   rY      s    
	-r   rY   c                    |r|}n)t         rt         }n|dk(  rt        | |      S t        |      }t        |j                        dk(  rt        | |      S g }|D ]1  }|j                  t        |              |j                  |d   |       3 |r|j                          |j                  j                         rt        d      |s]t         sW|j                          |j                  j                         r-|j                  j                         }	|	t        urt        d      |r't        t!        d |            }
|
r|
d   j"                  d |D        S ||gS )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                     | j                   S r7   )r[   )r   s    r   <lambda>ztmap.<locals>.<lambda>
  s
    q{{ r   c              3   4   K   | ]  }|j                     y wr7   )r\   ).0r   s     r   	<genexpr>ztmap.<locals>.<genexpr>  s     *Q*s   )r
   mapr	   lenr9   r?   rY   rJ   rT   r8   qsizeRuntimeErrorr   rQ   rL   listfilterr[   )rG   seq_argsr/   r   rT   stop_on_errorr0   resultssaum
error_oness              r   r)   r)      s3     Ba1h''[)B 277|q1h G z!}%
gbk2 
	 88>>011CGGIxx~~XX\\^T>&'899
 f%:GDEJ m---*'**=r   )r   rd   )r   NTT)re   
__author____version____license__r8   r   r   r'   r@   objectrL   FINISHr
   r   r%   r   r   r   r	   rY   r)   r   r   r   <module>r~      s}    
   
		x	  
  $"<~. .b- -:Br   