o
    OZh                     @   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)Znumber_of_workers r	   F/var/www/auris/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@  )Z	transformscalexpygamer	   r
   doitV   s   zbenchmark_workers.<locals>.doitc                    s   g | ]	}  d ddqS ))   r   r       )Z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   Zpygame.transformtimerangeMAX_WORKERS_TO_TESTr   print	threadingactive_counttmapr   )Za_bench_funcZthe_datar   r   ZthedatabestZ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)r.   r   appendThread
threadloop	setDaemonstart)r0   r&   r)   Za_threadr	   r	   r
   r/      s   


zWorkerQueue._setup_workersc                 O   s   | j |||f dS )z-puts a function on a queue for running later.N)r-   put)r0   fargsZ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)r-   r8   STOPr.   join)r0   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)r-   getr<   r8   	task_done)r0   r:   r	   r	   r
   r5      s   

zWorkerQueue.threadloopc                 C   s   | j   dS )z#waits until all tasks are complete.N)r-   r=   )r0   r	   r	   r
   wait   s   zWorkerQueue.waitN)r   )	__name__
__module____qualname__r1   r/   r;   r   r5   rB   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)r9   	exceptionresultcallbackerrback)r0   r9   rI   rJ   r	   r	   r
   r1      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 r,   )r9   rH   rI   	ExceptionrG   rJ   )r0   r:   kwargser	   r	   r
   __call__   s   zFuncResult.__call__NN)rC   rD   rE   __doc__r1   rN   r	   r	   r	   r
   rF      s    
rF   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 r,   )rG   r   r	   r	   r
   <lambda>
  s    ztmap.<locals>.<lambda>c                 s   s    | ]}|j V  qd S r,   )rH   r   r	   r	   r
   	<genexpr>  s    ztmap.<locals>.<genexpr>)r   mapr   lenr.   r3   rF   r;   rB   r-   qsizeRuntimeErrorr   r@   r<   listfilterrG   )r9   Zseq_argsr&   r   rB   Zstop_on_errorr'   resultssaZumZ
error_onesr	   r	   r
   r$      s:   





r$   )r   rO   )r   NTT)rP   
__author____version____license__r-   r   r   r"   r4   objectr<   ZFINISHr   r   r    r   r   r   r   rF   r$   r	   r	   r	   r
   <module>   s$    


?1