<s> '''<STR_LIT>''' <EOL> import time <EOL> import concurrent . futures <EOL> from python_toolbox import sequence_tools <EOL> class BaseCuteExecutor ( concurrent . futures . Executor ) : <EOL> '''<STR_LIT>''' <EOL> def filter ( self , filter_function , iterable , timeout = None , <EOL> as_completed = False ) : <EOL> '''<STR_LIT>''' <EOL> if timeout is not None : <EOL> end_time = timeout + time . time ( ) <EOL> def make_future ( item ) : <EOL> future = self . submit ( filter_function , item ) <EOL> future . _item = item <EOL> return future <EOL> futures = tuple ( map ( make_future , iterable ) ) <EOL> futures_iterator = concurrent . futures . as_completed ( futures ) if as_completed else futures <EOL> def result_iterator ( ) : <EOL> try : <EOL> for future in futures_iterator : <EOL> if timeout is None : <EOL> result = future . result ( ) <EOL> else : <EOL> result = future . result ( end_time - time . time ( ) ) <EOL> if result : <EOL> yield future . _item <EOL> finally : <EOL> for future in futures : <EOL> future . cancel ( ) <EOL> return result_iterator ( ) <EOL> def map ( self , function , iterable , timeout = None , as_completed = False ) : <EOL> '''<STR_LIT>''' <EOL> iterables = ( iterable , ) <EOL> if timeout is not None : <EOL> end_time = timeout + time . time ( ) <EOL> futures = [ self . submit ( function , * args ) for args in zip ( * iterables ) ] <EOL> futures_iterator = concurrent . futures . as_completed ( futures ) if as_completed else futures <EOL> def result_iterator ( ) : <EOL> try : <EOL> for future in futures_iterator : <EOL> if timeout is None : <EOL> yield future . result ( ) <EOL> else : <EOL> yield future . result ( end_time - time . time ( ) ) <EOL> finally : <EOL> for future in futures : <EOL> future . cancel ( ) <EOL> return result_iterator ( ) <EOL> class CuteThreadPoolExecutor ( concurrent . futures . ThreadPoolExecutor , <EOL> BaseCuteExecutor ) : <EOL> '''<STR_LIT>''' <EOL> class CuteProcessPoolExecutor ( concurrent . futures . ProcessPoolExecutor , <EOL> BaseCuteExecutor ) : <EOL> '''<STR_LIT>''' </s>
