<s> """<STR_LIT>""" <EOL> import logging <EOL> import multiprocessing <EOL> log = logging . getLogger ( __name__ ) <EOL> class CallbackHandler ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , worker_func , task_count , worker_count = - <NUM_LIT:1> ) : <EOL> if worker_count == - <NUM_LIT:1> : <EOL> worker_count = multiprocessing . cpu_count ( ) <EOL> self . worker_pool = multiprocessing . Pool ( processes = worker_count ) <EOL> self . task_count = task_count <EOL> self . worker_func = worker_func <EOL> self . launched_tasks = <NUM_LIT:0> <EOL> self . skipped_tasks = <NUM_LIT:0> <EOL> def run_callback ( self , * args , ** kwargs ) : <EOL> log . debug ( '<STR_LIT>' ) <EOL> self . launched_tasks = self . launched_tasks + <NUM_LIT:1> <EOL> print '<STR_LIT>' + str ( self . launched_tasks ) <EOL> worker_params = dict ( args = args , <EOL> kwargs = kwargs ) <EOL> self . worker_pool . apply_async ( _callback_worker_func , [ worker_params ] , <EOL> callback = self . _dec_task_count_completed ) <EOL> def skip ( self ) : <EOL> log . debug ( '<STR_LIT>' ) <EOL> self . skipped_tasks = self . skipped_tasks + <NUM_LIT:1> <EOL> print '<STR_LIT>' + str ( self . skipped_tasks ) <EOL> self . _dec_task_count_skipped ( ) <EOL> def join ( self ) : <EOL> log . debug ( '<STR_LIT>' ) <EOL> print "<STR_LIT>" <EOL> while self . task_count > <NUM_LIT:0> : <EOL> pass <EOL> self . worker_pool . close ( ) <EOL> self . worker_pool . join ( ) <EOL> log . debug ( '<STR_LIT>' ) <EOL> print "<STR_LIT>" <EOL> def terminate ( self ) : <EOL> log . debug ( '<STR_LIT>' ) <EOL> print "<STR_LIT>" <EOL> self . worker_pool . terminate ( ) <EOL> self . worker_pool . join ( ) <EOL> log . debug ( '<STR_LIT>' ) <EOL> print '<STR_LIT>' <EOL> def _dec_task_count_completed ( self , retval ) : <EOL> self . task_count = self . task_count - <NUM_LIT:1> <EOL> log . debug ( '<STR_LIT>' , self . task_count ) <EOL> def _dec_task_count_skipped ( self ) : <EOL> self . task_count = self . task_count - <NUM_LIT:1> <EOL> log . debug ( '<STR_LIT>' , self . task_count ) <EOL> def _callback_worker_func ( self , worker_params ) : <EOL> worker_func ( * worker_params [ '<STR_LIT:args>' ] , ** worker_params [ '<STR_LIT>' ] ) <EOL> print '<STR_LIT>' </s>
