Created
May 22, 2011 06:48
-
-
Save sattvikc/985235 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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