Skip to content

Instantly share code, notes, and snippets.

@sattvikc
Created May 22, 2011 06:48
Show Gist options
  • Save sattvikc/985235 to your computer and use it in GitHub Desktop.
Save sattvikc/985235 to your computer and use it in GitHub Desktop.
import threading
class MapThread(threading.Thread):
def initialize(self, function, sequence, *args):
self._function = function
self._sequence = sequence
self._args = args
def run(self):
self._result = map(self._function, self._sequence, *self._args)
def threaded_map(nthreads, function, sequence, *args):
threads = [MapThread() for i in xrange(nthreads)]
n = len(sequence)
seq_lengths = [i * n / nthreads for i in xrange(nthreads+1)]
for i in xrange(nthreads):
seq = sequence[seq_lengths[i]:seq_lengths[i+1]]
nargs = ()
for arg in args:
nargs += (arg[seq_lengths[i]:seq_lengths[i+1]])
threads[i].initialize(function, seq, *nargs)
threads[i].start()
result = []
for thread in threads:
thread.join()
result += thread._result
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment