-
Notifications
You must be signed in to change notification settings - Fork 44
Add arg to define thread limit - to throttle concurrent API calls when required #355
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
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 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -50,6 +50,7 @@ coverage.xml | |
| .hypothesis/ | ||
| .pytest_cache/ | ||
| cover/ | ||
| preupgrade_validator*.tgz | ||
|
|
||
| # Translations | ||
| *.mo | ||
|
|
||
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,41 +9,67 @@ | |
|
|
||
|
|
||
| def task1(data=""): | ||
| time.sleep(2.5) | ||
| time.sleep(2) | ||
| if not global_timeout: | ||
| print("Thread task1: Finishing with data {}".format(data)) | ||
|
|
||
|
|
||
| def task2(data=""): | ||
| time.sleep(0.5) | ||
| time.sleep(2.5) | ||
| if not global_timeout: | ||
| print("Thread task2: Finishing with data {}".format(data)) | ||
|
|
||
|
|
||
| def task3(data=""): | ||
| time.sleep(0.2) | ||
| time.sleep(1) | ||
| if not global_timeout: | ||
| print("Thread task3: Finishing with data {}".format(data)) | ||
|
|
||
|
|
||
| def task4(data=""): | ||
| time.sleep(5) | ||
| if not global_timeout: | ||
| print("Thread task4: Finishing with data {}".format(data)) | ||
|
|
||
|
|
||
| def task5(data=""): | ||
| time.sleep(5) | ||
| if not global_timeout: | ||
| print("Thread task5: Finishing with data {}".format(data)) | ||
|
|
||
|
|
||
| def test_ThreadManager(capsys): | ||
| global global_timeout | ||
| tm = script.ThreadManager( | ||
| funcs=[task1, task2, task3], | ||
| funcs=[task1, task2, task3, task4, task5], | ||
| common_kwargs={"data": "common_data"}, | ||
| monitor_timeout=1, | ||
| max_threads=2, | ||
| callback_on_timeout=lambda x: print("Timeout. Abort {}".format(x)) | ||
| ) | ||
| tm.start() | ||
| tm.join() | ||
|
|
||
| # Join each task thread to ensure any in-progress prints complete before | ||
| # capsys.readouterr() is called. Without this there is a race where a | ||
| # thread passes the `if not global_timeout` check and then tries to print | ||
| # after pytest has already torn down the captured stdout fd, causing | ||
| # OSError: [Errno 9] Bad file descriptor. | ||
| for thread in tm.threads: | ||
| try: | ||
| thread.join(timeout=1.5) | ||
| except RuntimeError: | ||
| pass # thread was never started | ||
|
|
||
|
Comment on lines
+53
to
+63
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Handling another scenario where |
||
| if tm.is_timeout(): | ||
| global_timeout = True | ||
|
|
||
| expected_output = """\ | ||
| Thread task3: Finishing with data common_data | ||
| Thread task2: Finishing with data common_data | ||
| Timeout. Abort task1 | ||
| Timeout. Abort task2 | ||
| Thread task1: Finishing with data common_data | ||
| Thread task2: Finishing with data common_data | ||
| Thread task3: Finishing with data common_data | ||
| """ | ||
| captured = capsys.readouterr() | ||
| assert captured.out == expected_output | ||
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
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.
pytests were intermittently failing with
Bad File Descriptor