Last active
December 17, 2015 16:19
-
-
Save hamishmorgan/5637747 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
__author__ = 'hiam20' | |
import time | |
import collections | |
import threading | |
import concurrent.futures | |
INSTANTIATION_TIME = 0.5 | |
EXECUTION_TIME = 0.002 | |
class UppercaseOTron(object): | |
def __init__(self): | |
time.sleep(INSTANTIATION_TIME) | |
self.entry_count = 0 | |
def apply(self, text): | |
# Check we aren't being accessed concurrently | |
assert self.entry_count == 0 | |
self.entry_count += 1 | |
result = '' | |
for x in text: | |
result += x.upper() | |
time.sleep(EXECUTION_TIME) | |
self.entry_count -= 1 | |
return result | |
class ParallelUppercaseOTron(UppercaseOTron): | |
def __init__(self, n_threads=1): | |
self.n_threads = n_threads | |
self.__instances = collections.defaultdict(UppercaseOTron) | |
def apply(self, text): | |
with concurrent.futures.ThreadPoolExecutor(max_workers=self.n_threads) as ex: | |
return ''.join(y for y in ex.map(self.__apply0, (x for x in text))) | |
def __apply0(self, x): | |
return self.__instances[threading.current_thread().name].apply(x) | |
if __name__ == '__main__': | |
lipsum = """Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque eget dui velit. Proin feugiat cursus | |
massa, a laoreet nunc feugiat ac. Maecenas tincidunt purus nec nisl ullamcorper pulvinar. Nulla dictum, sapien vel | |
iaculis placerat, diam nisl molestie nulla, et commodo turpis justo tristique risus. Nullam vel est augue, quis | |
molestie risus. Quisque malesuada est mattis metus posuere eu dignissim ligula lobortis. Donec lacus lacus, placerat | |
id varius lacinia, aliquam non sem. Proin commodo cursus viverra. Proin et nisl libero, ac convallis orci. Nam et est | |
eu ante convallis rutrum hendrerit non turpis. Donec eu velit purus. Aliquam orci arcu, semper at mollis id, tincidunt | |
ac turpis. Nam scelerisque, lectus in dapibus venenatis, lorem nibh sollicitudin purus, eget tincidunt dui dolor sed | |
sapien.""" | |
start = time.time() | |
result = ParallelUppercaseOTron(n_threads=10).apply(lipsum) | |
print 'Done in %f seconds:' % (time.time() - start) | |
print result | |
assert result == lipsum.upper() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment