<s> import Queue <EOL> import sys <EOL> import threading <EOL> import logging <EOL> logger = logging . getLogger ( __name__ ) <EOL> logger . addHandler ( logging . NullHandler ( ) ) <EOL> __all__ = [ '<STR_LIT>' ] <EOL> class Pool ( object ) : <EOL> """<STR_LIT>""" <EOL> class WorkerExit ( object ) : <EOL> """<STR_LIT>""" <EOL> def __call__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return None <EOL> class Worker ( threading . Thread ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , pool ) : <EOL> """<STR_LIT>""" <EOL> super ( Pool . Worker , self ) . __init__ ( ) <EOL> self . pool = pool <EOL> def run ( self ) : <EOL> """<STR_LIT>""" <EOL> obj = None <EOL> while obj is not self . pool . sentinel : <EOL> obj , args , kwargs = self . pool . job_queue . get ( ) <EOL> try : <EOL> res = obj ( * args , ** kwargs ) <EOL> if res is not None : <EOL> self . pool . res_queue . put ( res ) <EOL> except Exception as e : <EOL> logger . error ( '<STR_LIT:%s>' , e ) <EOL> finally : <EOL> self . pool . job_queue . task_done ( ) <EOL> sentinel = WorkerExit ( ) <EOL> def __init__ ( self , num_threads = <NUM_LIT:32> , use_daemons = True ) : <EOL> """<STR_LIT>""" <EOL> self . num_threads = num_threads <EOL> self . job_queue = Queue . Queue ( ) <EOL> self . res_queue = Queue . Queue ( ) <EOL> self . threads = [ ] <EOL> self . closed = False <EOL> for _ in xrange ( self . num_threads ) : <EOL> self . threads . append ( Pool . Worker ( self ) ) <EOL> for thread in self . threads : <EOL> thread . daemon = use_daemons <EOL> thread . start ( ) <EOL> def __enter__ ( self ) : <EOL> return self <EOL> def __exit__ ( self , * ignored ) : <EOL> self . close ( ) <EOL> def close ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . closed : <EOL> return <EOL> self . join ( ) <EOL> for _ in xrange ( self . num_threads ) : <EOL> self . push ( self . sentinel ) <EOL> for thread in self . threads : <EOL> thread . join ( ) <EOL> self . closed = True <EOL> def get_results ( self ) : <EOL> """<STR_LIT>""" <EOL> results = [ ] <EOL> while not self . res_queue . empty ( ) : <EOL> results . append ( self . res_queue . get ( ) ) <EOL> self . res_queue . task_done ( ) <EOL> return results <EOL> def push ( self , obj , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if self . closed : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> self . job_queue . put ( ( obj , args , kwargs ) ) <EOL> def join ( self ) : <EOL> """<STR_LIT>""" <EOL> self . job_queue . join ( ) </s>
