ó
ì[Jc           @   s~  d  Z  d Z d Z d Z d d l Z d d l Z d d l m Z e j d d k rv d d	 l	 m
 Z d d
 l m Z ni e j d d k r¿ e j d d k  r¿ d d l m Z d d
 l m Z n  d d l m Z d d
 l 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 d „ Z d e f d „  ƒ  YZ d d d „  ƒ  YZ d d e e d „ Z d S(   sº   
* 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.
s   Rene Dudfields   0.3.0s   Python licenseiÿÿÿÿN(   t   geterrori    i   (   t   JoinableQueue(   t   Emptyi   i   i   (   t   Queuei@   c         C   s(   |  r |  a  n	 t ƒ  a  t t  ƒ a d S(   s»    Does 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(   t   _use_workerst   benchmark_workerst   WorkerQueuet   _wq(   t   number_of_workers(    (    sD   c:\mingw\msys\1.0\newbuild\install\python\pygame\threads\__init__.pyt   init4   s    		c           C   s   t  j ƒ  d a  t a d S(   s    cleans up everything.
    N(   R   t   stopt   Nonet   FalseR   (    (    (    sD   c:\mingw\msys\1.0\newbuild\install\python\pygame\threads\__init__.pyt   quitG   s    
c            s{  d d l  ‰  d d l ‰  d d l } |  s< ‡  f d †  } n |  } | s„ g  } x9 t d ƒ D]" } | j ˆ  j d d d ƒ ƒ q[ Wn | } | j ƒ  d } d } d } xÎ t d t ƒ D]½ }	 t |	 ƒ }
 | j ƒ  } x8 t d	 ƒ D]* } d
 t j	 ƒ  GHt
 | | d |
 ƒ} qá W| j ƒ  } |
 j ƒ  | | } d |	 | f GH| | k  r_| } |	 } | } n  |	 | d k r¶ Pq¶ q¶ W| S(   sX   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.
    iÿÿÿÿNc            s   ˆ  j  j |  d ƒ S(   Ni   i@  (   i   i@  (   t	   transformt   scale(   t   x(   t   pygame(    sD   c:\mingw\msys\1.0\newbuild\install\python\pygame\threads\__init__.pyt   doiti   s    i
   i›   i    i    i áõi   s   active count:%st   worker_queues#   total time num_workers:%s: time:%s:i   (   i›   i›   (   R   t   pygame.transformt   timet   ranget   appendt   Surfacet   MAX_WORKERS_TO_TESTR   t	   threadingt   activeCountt   tmapR
   (   t   a_bench_funct   the_dataR   R   t   thedataR   t   bestt   best_numbert	   last_bestt   num_workerst   wqt   t1t   xxt   resultst   t2t
   total_time(    (   R   sD   c:\mingw\msys\1.0\newbuild\install\python\pygame\threads\__init__.pyR   P   s>    #

	R   c           B   sA   e  Z d  d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   i   c         C   s&   t  ƒ  |  _ g  |  _ |  j | ƒ d  S(   N(   R   t   queuet   poolt   _setup_workers(   t   selfR#   (    (    sD   c:\mingw\msys\1.0\newbuild\install\python\pygame\threads\__init__.pyt   __init__š   s    	c         C   sk   g  |  _  x0 t | ƒ D]" } |  j  j t d |  j ƒ ƒ q Wx( |  j  D] } | j t ƒ | j ƒ  qF Wd S(   sd    Sets up the worker threads
              NOTE: undefined behaviour if you call this again.
        t   targetN(   R+   R   R   t   Threadt
   threadloopt	   setDaemont   Truet   start(   R-   R#   t   _t   a_thread(    (    sD   c:\mingw\msys\1.0\newbuild\install\python\pygame\threads\__init__.pyR,   Ÿ   s    	 c         O   s   |  j  j | | | f ƒ d S(   s7    puts a function on a queue for running later.
        N(   R*   t   put(   R-   t   ft   argst   kwArgs(    (    sD   c:\mingw\msys\1.0\newbuild\install\python\pygame\threads\__init__.pyt   do­   s    c         C   s2   |  j  j t ƒ x |  j D] } | j ƒ  q Wd S(   sK    Stops the WorkerQueue, waits for all of the threads to finish up.
        N(   R*   R7   t   STOPR+   t   join(   R-   t   thread(    (    sD   c:\mingw\msys\1.0\newbuild\install\python\pygame\threads\__init__.pyR
   ³   s    c         C   s{   xt t  rv |  j j ƒ  } | t k rE |  j j t ƒ |  j j ƒ  Pq z | d | d | d Ž  Wd |  j j ƒ  Xq Wd S(   s4    Loops until all of the tasks are finished.
        i    i   i   N(   R3   R*   t   getR<   R7   t	   task_done(   R-   R9   (    (    sD   c:\mingw\msys\1.0\newbuild\install\python\pygame\threads\__init__.pyR1   »   s    	c         C   s   |  j  j ƒ  d S(   s-    waits until all tasks are complete.
        N(   R*   R=   (   R-   (    (    sD   c:\mingw\msys\1.0\newbuild\install\python\pygame\threads\__init__.pyt   waitÍ   s    (   t   __name__t
   __module__R.   R,   R;   R
   R1   RA   (    (    (    sD   c:\mingw\msys\1.0\newbuild\install\python\pygame\threads\__init__.pyR   ˜   s   				t
   FuncResultc           B   s&   e  Z d  Z d d d „ Z d „  Z RS(   sy    Used for wrapping up a function call so that the results are stored
         inside the instances result attribute.
    c         C   s(   | |  _  d |  _ | |  _ | |  _ d S(   sö    f - 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(   R8   R   t	   exceptiont   callbackt   errback(   R-   R8   RF   RG   (    (    sD   c:\mingw\msys\1.0\newbuild\install\python\pygame\threads\__init__.pyR.   Ö   s    			c         O   su   y5 |  j  | | Ž  |  _ |  j r4 |  j |  j ƒ n  Wn9 t k
 rp t ƒ  |  _ |  j rq |  j |  j ƒ qq n Xd  S(   N(   R8   t   resultRF   t	   ExceptionR    RE   RG   (   R-   R9   t   kwargs(    (    sD   c:\mingw\msys\1.0\newbuild\install\python\pygame\threads\__init__.pyt   __call__á   s    		N(   RB   RC   t   __doc__R   R.   RK   (    (    (    sD   c:\mingw\msys\1.0\newbuild\install\python\pygame\threads\__init__.pyRD   Ò   s   i   c         C   sw  | r | } n4 t  r t  } n% | d k r7 t |  | ƒ St | ƒ } t | j ƒ d k re t |  | ƒ Sg  } x5 | D]- } | j t |  ƒ ƒ | j | d | ƒ qr W| ri| j ƒ  | j	 j
 ƒ  rÑ t d ƒ ‚ n  | r(t  r(| j ƒ  | j	 j
 ƒ  r(| j	 j ƒ  }	 |	 t k	 r%t d ƒ ‚ q%q(n  | rYt d „  | ƒ }
 |
 rY|
 d j ‚ qYn  t d „  | ƒ S| | g Sd S(   s'   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 - 
    i    iÿÿÿÿs   buggy threadmapc         S   s   |  j  S(   N(   RE   (   R   (    (    sD   c:\mingw\msys\1.0\newbuild\install\python\pygame\threads\__init__.pyt   <lambda>0  s    c         S   s   |  j  S(   N(   RH   (   R   (    (    sD   c:\mingw\msys\1.0\newbuild\install\python\pygame\threads\__init__.pyRM   4  s    N(   R   t   mapR   t   lenR+   R   RD   R;   RA   R*   t   qsizeRI   R
   R?   R<   t   filterRE   (   R8   t   seq_argsR#   R   RA   t   stop_on_errorR$   R'   t   sat   umt
   error_ones(    (    sD   c:\mingw\msys\1.0\newbuild\install\python\pygame\threads\__init__.pyR   î   s:    		

(    (   RL   t
   __author__t   __version__t   __license__t	   tracebackt   syst   pygame.compatR    t   version_infot   multiprocessingR   R   R*   R   t	   Py25QueueR   R0   t   objectR<   t   FINISHR   R   R   R   R	   R   R   R   RD   R3   R   (    (    (    sD   c:\mingw\msys\1.0\newbuild\install\python\pygame\threads\__init__.pyt   <module>	   s4   &					H: