o
    tVg                     @   s   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d	Zd
d ZdddZG dd dZG dd dZdddZdS )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                 C   s   | r| a nt a tt adS )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_workers r
   K/home/ubuntu/.local/lib/python3.10/site-packages/pygame/threads/__init__.pyinit'   s   r   c                   C   s   t   da dadS )zcleans up everything.NF)r   stopr   r
   r
   r
   r   quit8   s   r   c                    s   ddl  ddl ddl}| s fdd}n| }|s% fddtdD }n|}| d }d}tdtD ]I}t|}| }	td	D ]}
td
t   t	|||d qB| }|
  ||	 }td| d| d ||k rt|}|}|| dkr} |S q4|S )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                    s    j | dS )N)i   i@  )	transformscalexpygamer
   r   doitV   s   zbenchmark_workers.<locals>.doitc                    s   g | ]	}  d ddqS ))   r   r       )Surface.0r   r   r
   r   
<listcomp>]   s    z%benchmark_workers.<locals>.<listcomp>
   i    zactive count:)worker_queueztotal time num_workers:z: time::   )r   pygame.transformtimerangeMAX_WORKERS_TO_TESTr   print	threadingactive_counttmapr   )a_bench_functhe_datar"   r   thedatabestbest_numbernum_workerswqt1_t2
total_timer
   r   r   r   @   s:   r   c                   @   s>   e Zd ZdddZdd Zdd Zdd	 Zd
d Zdd ZdS )r   r   c                 C   s   t  | _g | _| | d S N)r   queuepool_setup_workers)selfr.   r
   r
   r   __init__   s   zWorkerQueue.__init__c                 C   sJ   g | _ t|D ]}| j t| jd q| j D ]}|d |  qdS )z]Sets up the worker threads
        NOTE: undefined behaviour if you call this again.
        )targetTN)r6   r#   appendThread
threadloop	setDaemonstart)r8   r.   r1   a_threadr
   r
   r   r7      s   


zWorkerQueue._setup_workersc                 O   s   | j |||f dS )z-puts a function on a queue for running later.N)r5   put)r8   fargskwArgsr
   r
   r   do   s   zWorkerQueue.doc                 C   s$   | j t | jD ]}|  q	dS )zAStops the WorkerQueue, waits for all of the threads to finish up.N)r5   rA   STOPr6   join)r8   threadr
   r
   r   r      s   

zWorkerQueue.stopc                 C   sf   	 | j  }|tu r| j t | j   dS z|d |d i |d  W | j   n| j   w q)z*Loops until all of the tasks are finished.Tr   r       N)r5   getrF   rA   	task_done)r8   rC   r
   r
   r   r=      s   

zWorkerQueue.threadloopc                 C   s   | j   dS )z#waits until all tasks are complete.N)r5   rG   )r8   r
   r
   r   wait   s   zWorkerQueue.waitN)r   )	__name__
__module____qualname__r9   r7   rE   r   r=   rL   r
   r
   r
   r   r      s    
r   c                   @   s"   e Zd ZdZdddZdd ZdS )
FuncResultzsUsed for wrapping up a function call so that the results are stored
    inside the instances result attribute.
    Nc                 C   s"   || _ d| _d| _|| _|| _dS )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)rB   	exceptionresultcallbackerrback)r8   rB   rS   rT   r
   r
   r   r9      s
   
zFuncResult.__init__c              
   O   s~   z| j |i || _| jr| | j W d S W d S  ty> } z|| _| jr3| | j W Y d }~d S W Y d }~d S d }~ww r4   )rB   rR   rS   	ExceptionrQ   rT   )r8   rC   kwargser
   r
   r   __call__   s   zFuncResult.__call__NN)rM   rN   rO   __doc__r9   rX   r
   r
   r
   r   rP      s    
rP   r   Tc                 C   s   |r|}nt r
t }n|dkrt| |S t|}t|jdkr#t| |S g }|D ]}|t|  ||d | q'|r{|  |j	
 rHtd|sbt sb|  |j	
 rb|j	 }	|	turbtd|rtttdd |}
|
rt|
d jd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                 S   s   | j S r4   )rQ   r   r
   r
   r   <lambda>
  s    ztmap.<locals>.<lambda>c                 s   s    | ]}|j V  qd S r4   )rR   r   r
   r
   r   	<genexpr>  s    ztmap.<locals>.<genexpr>)r   mapr   lenr6   r;   rP   rE   rL   r5   qsizeRuntimeErrorr   rJ   rF   listfilterrQ   )rB   seq_argsr.   r   rL   stop_on_errorr/   resultssaum
error_onesr
   r
   r   r(      s:   





r(   )r   rY   )r   NTT)rZ   
__author____version____license__r5   r   r   r&   r<   objectrF   FINISHr   r   r$   r   r   r   r   rP   r(   r
   r
   r
   r   <module>   s$    


?1