#NEWLINE# def concurrent_map( func , data ) : #NEWLINE# #INDENT# "\n    Similar to the builtin function map(). But spawn a thread for each argument\n    and apply 'func' concurrently.\n\n    Note: unlie map(), we cannot take an iterable argument. 'data' should be an\n    indexable sequence.\n    " #NEWLINE# N = len ( data ) #NEWLINE# result = ( [ None ] * N ) #NEWLINE# def task_wrapper( i ) : #NEWLINE# #INDENT# result [ i ] = func ( data [ i ] ) #NEWLINE# #UNINDENT# threads = [ threading . Thread ( target = task_wrapper , args = ( i , ) ) for i in xrange ( N ) ] #NEWLINE# for t in threads : #NEWLINE# #INDENT# t . start ( ) #NEWLINE# #UNINDENT# for t in threads : #NEWLINE# #INDENT# t . join ( ) #NEWLINE# #UNINDENT# return result