-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Parallelize get_movie Calls #35
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
from threading import Thread | ||
from datetime import datetime | ||
|
||
DEFAULT_TIMEOUT = 10 | ||
|
||
""" | ||
Class which extends Thread to expose a result from the target (input) function. | ||
""" | ||
class ResultThread(Thread): | ||
def __init__(self, **kwargs): | ||
super(ResultThread, self).__init__() | ||
self.target = kwargs.get('target') | ||
def run(self): | ||
self.result = self.target() | ||
|
||
def get_now(): | ||
return datetime.now().timestamp() | ||
|
||
def call_one_func_parallel(inputs, func, timeout=DEFAULT_TIMEOUT): | ||
""" | ||
Takes in a list of inputs and one function, which is run asynchronously across all inputs. | ||
Tuples of (input, result) are returned. | ||
Doctest below. | ||
|
||
>>> from time import sleep | ||
|
||
>>> def sleep_func(timeout): | ||
... sleep(timeout) | ||
... return timeout | ||
|
||
>>> call_one_func_parallel([7, 1, 1, 2], sleep_func) | ||
[(7, 7), (1, 1), (1, 1), (2, 2)] | ||
|
||
>>> call_one_func_parallel([7, 1, 1, 2], sleep_func, timeout=3) | ||
Traceback (most recent call last): | ||
... | ||
RuntimeError: ('Service call took longer than max timeout of :', 3, 'seconds') | ||
""" | ||
funcs = [func] * len(inputs) | ||
return call_parallel(inputs, funcs, timeout) | ||
|
||
def call_parallel(inputs, funcs, timeout=DEFAULT_TIMEOUT): | ||
""" | ||
Takes in lists of inputs and functions, which are then run asynchronously, and returns a list of their results. | ||
Doctest below. | ||
|
||
>>> from time import sleep | ||
|
||
>>> def sleep_func(timeout): | ||
... sleep(timeout) | ||
... return timeout | ||
|
||
>>> call_parallel([], []) | ||
[] | ||
|
||
>>> call_parallel([5, 1, 3, 5], [sleep_func] * 4) | ||
[(5, 5), (1, 1), (3, 3), (5, 5)] | ||
|
||
>>> call_parallel([5, 1, 3, 5], [sleep_func] * 4, timeout=2) | ||
Traceback (most recent call last): | ||
... | ||
RuntimeError: ('Service call took longer than max timeout of :', 2, 'seconds') | ||
""" | ||
if len(inputs) != len(funcs): | ||
raise RuntimeError("# of inputs (", len(inputs), ") do not match # of functions (", len(funcs), ")") | ||
threads = [] | ||
results = [] | ||
start_time = get_now() | ||
for input, func in zip(inputs, funcs): | ||
new_thread = ResultThread(target=lambda: func(input), daemon=True) | ||
threads.append(new_thread) | ||
new_thread.start() | ||
for thread in threads: | ||
time_elapsed = get_now() - start_time | ||
thread.join(timeout - time_elapsed) | ||
if thread.is_alive(): | ||
raise RuntimeError("Service call took longer than max timeout of :", timeout, "seconds") | ||
else: | ||
results.append(thread.result) | ||
return list(zip(inputs, results)) | ||
|
||
if __name__ == "__main__": | ||
import doctest | ||
doctest.testmod() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How important is the extra few milliseconds here? I think there's a bit less complexity if we just pass in timeout, even if technically the thread gets to run for a few extra milliseconds. Then we don't need to worry about
get_now()
, recording start time, etc.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thread.join's first argument is the amount of time we're actually waiting for the thread to finish since calling thread.join on that thread. As an extreme example, if there are 20 threads, and each one exceeds our maximum timeout of 10 seconds, we will wait for 200 seconds. Keeping the 'start time' keeps this timeout relative to when we actually kicked them all off.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ooooh, interesting. Makes sense.