#NEWLINE# def _get_results( FetcherClass , services , kwargs , num_results = None , fast = 0 , verbose = False , timeout = None ) : #NEWLINE# #INDENT# '\n    Does the fetching in multiple threads of needed. Used by paranoid and fast mode.\n    ' #NEWLINE# results = [ ] #NEWLINE# if ( ( not num_results ) or fast ) : #NEWLINE# #INDENT# num_results = len ( services ) #NEWLINE# #UNINDENT# with futures . ThreadPoolExecutor ( max_workers = len ( services ) ) as executor : #NEWLINE# #INDENT# fetches = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# for service in services [ : num_results ] : #NEWLINE# #INDENT# tail = [ x for x in services if ( x is not service ) ] #NEWLINE# random . shuffle ( tail ) #NEWLINE# srv = FetcherClass ( services = ( [ service ] + tail ) , verbose = verbose , timeout = timeout ) #NEWLINE# fetches [ executor . submit ( srv . action , ** kwargs ) ] = srv #NEWLINE# #UNINDENT# if ( fast == 1 ) : #NEWLINE# #INDENT# raise NotImplementedError #NEWLINE# ( to_iterate , still_going ) = futures . wait ( fetches , return_when = futures . FIRST_COMPLETED ) #NEWLINE# for x in still_going : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# x . result ( timeout = 1.001 ) #NEWLINE# #UNINDENT# except futures . _base . TimeoutError : #NEWLINE# #INDENT# pass #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( fast > 1 ) : #NEWLINE# #INDENT# raise Exception ( 'fast level greater than 1 not yet implemented' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# to_iterate = futures . as_completed ( fetches ) #NEWLINE# #UNINDENT# #UNINDENT# for future in to_iterate : #NEWLINE# #INDENT# service = num_results [ future ] #NEWLINE# results . append ( [ service , future . result ( ) ] ) #NEWLINE# #UNINDENT# #UNINDENT# return results