<s> '''<STR_LIT>''' <EOL> import threading <EOL> import Queue <EOL> import time <EOL> DEFAULT_SLEEP = <NUM_LIT> <EOL> class Worker ( threading . Thread ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , tasks , logger ) : <EOL> threading . Thread . __init__ ( self ) <EOL> self . tasks = tasks <EOL> self . logger = logger <EOL> self . daemon = True <EOL> self . start ( ) <EOL> def run ( self ) : <EOL> while True : <EOL> func , args , kwargs = self . tasks . get ( ) <EOL> try : <EOL> func ( * args , ** kwargs ) <EOL> except Exception , e : <EOL> error = '<STR_LIT>' <EOL> self . logger . error ( error . format ( func . __name__ , e ) , <EOL> exc_info = True ) <EOL> finally : <EOL> self . tasks . task_done ( ) <EOL> class ThreadPool ( threading . Thread ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , min_workers , logger = None , wait = DEFAULT_SLEEP ) : <EOL> threading . Thread . __init__ ( self ) <EOL> self . tasks = Queue . Queue ( ) <EOL> self . min_workers = min_workers <EOL> self . workers = <NUM_LIT:0> <EOL> self . logger = logger <EOL> self . wait = wait <EOL> self . daemon = True <EOL> self . start ( ) <EOL> def _spawn_worker ( self ) : <EOL> self . workers += <NUM_LIT:1> <EOL> Worker ( self . tasks , self . logger ) <EOL> def enqueue_task ( self , func , * args , ** kwargs ) : <EOL> self . tasks . put ( ( func , args , kwargs ) ) <EOL> @ property <EOL> def too_few_workers ( self ) : <EOL> return self . workers < self . min_workers <EOL> def run ( self ) : <EOL> while True : <EOL> time . sleep ( self . wait ) <EOL> if self . workers < self . min_workers : <EOL> self . _spawn_worker ( ) </s>
