<s> import Queue <EOL> import threading <EOL> import time <EOL> class ThreadPool ( object ) : <EOL> def __init__ ( self ) : <EOL> self . queue = Queue . Queue ( ) <EOL> def enqueue ( self , func , * args , ** kwargs ) : <EOL> self . queue . put ( ( func , args , kwargs ) ) <EOL> def work ( self , concurrency ) : <EOL> threads = [ ] <EOL> for _ in xrange ( concurrency ) : <EOL> t = threading . Thread ( target = self . thread ) <EOL> t . daemon = True <EOL> t . start ( ) <EOL> threads . append ( t ) <EOL> while True : <EOL> active = False <EOL> for t in threads : <EOL> active = active or t . is_alive ( ) <EOL> if not active : <EOL> break <EOL> time . sleep ( <NUM_LIT> ) <EOL> def thread ( self ) : <EOL> while not self . queue . empty ( ) : <EOL> func , args , kwargs = self . queue . get_nowait ( ) <EOL> func ( * args , ** kwargs ) <EOL> class Flag ( object ) : <EOL> def __init__ ( self , value = False ) : <EOL> self . value = value <EOL> self . lock = threading . RLock ( ) <EOL> def set_value ( self , value ) : <EOL> with self . lock : <EOL> self . value = value </s>
