<s> """<STR_LIT>""" <EOL> from threading import Thread <EOL> from acrylamid import log <EOL> from acrylamid . compat import PY2K , text_type as str <EOL> if PY2K : <EOL> from Queue import Queue <EOL> else : <EOL> from queue import Queue <EOL> class Worker ( Thread ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , tasks ) : <EOL> Thread . __init__ ( self ) <EOL> self . tasks = tasks <EOL> self . daemon = True <EOL> self . start ( ) <EOL> def run ( self ) : <EOL> while True : <EOL> func , args , kargs = self . tasks . get ( ) <EOL> try : <EOL> func ( * args , ** kargs ) <EOL> except Exception as e : <EOL> log . exception ( '<STR_LIT>' % ( e . __class__ . __name__ , str ( e ) ) ) <EOL> self . tasks . task_done ( ) <EOL> class Threadpool : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , num_threads , wait = True ) : <EOL> self . tasks = Queue ( num_threads if wait else <NUM_LIT:0> ) <EOL> self . wait = wait <EOL> for _ in range ( num_threads ) : <EOL> Worker ( self . tasks ) <EOL> def add_task ( self , func , * args , ** kargs ) : <EOL> """<STR_LIT>""" <EOL> self . tasks . put ( ( func , args , kargs ) , self . wait ) <EOL> def wait_completion ( self ) : <EOL> """<STR_LIT>""" <EOL> self . tasks . join ( ) </s>
