-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Allow to inject a context manager around TaskProcess.run #2449
Merged
Merged
Changes from all commits
Commits
Show all changes
2 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,70 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright 2012-2015 Spotify AB | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
from helpers import LuigiTestCase, temporary_unloaded_module | ||
import luigi | ||
from luigi.worker import Worker | ||
import multiprocessing | ||
|
||
|
||
class ContextManagedTaskProcessTest(LuigiTestCase): | ||
|
||
def _test_context_manager(self, force_multiprocessing): | ||
CONTEXT_MANAGER_MODULE = b''' | ||
class MyContextManager(object): | ||
def __init__(self, task_process): | ||
self.task = task_process.task | ||
def __enter__(self): | ||
assert not self.task.run_event.is_set(), "the task should not have run yet" | ||
self.task.enter_event.set() | ||
return self | ||
def __exit__(self, exc_type=None, exc_value=None, traceback=None): | ||
assert self.task.run_event.is_set(), "the task should have run" | ||
self.task.exit_event.set() | ||
''' | ||
|
||
class DummyEventRecordingTask(luigi.Task): | ||
def __init__(self, *args, **kwargs): | ||
self.enter_event = multiprocessing.Event() | ||
self.exit_event = multiprocessing.Event() | ||
self.run_event = multiprocessing.Event() | ||
super(DummyEventRecordingTask, self).__init__(*args, **kwargs) | ||
|
||
def run(self): | ||
assert self.enter_event.is_set(), "the context manager should have been entered" | ||
assert not self.exit_event.is_set(), "the context manager should not have been exited yet" | ||
assert not self.run_event.is_set(), "the task should not have run yet" | ||
self.run_event.set() | ||
|
||
def complete(self): | ||
return self.run_event.is_set() | ||
|
||
with temporary_unloaded_module(CONTEXT_MANAGER_MODULE) as module_name: | ||
t = DummyEventRecordingTask() | ||
w = Worker(task_process_context=module_name + '.MyContextManager', | ||
force_multiprocessing=force_multiprocessing) | ||
w.add(t) | ||
self.assertTrue(w.run()) | ||
self.assertTrue(t.complete()) | ||
self.assertTrue(t.enter_event.is_set()) | ||
self.assertTrue(t.exit_event.is_set()) | ||
|
||
def test_context_manager_without_multiprocessing(self): | ||
self._test_context_manager(False) | ||
|
||
def test_context_manager_with_multiprocessing(self): | ||
self._test_context_manager(True) |
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
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.
Did you consider making this a "list" by making a comma-separate name of classnames?
But I guess users can make their single context-manager itself have multiple ones.
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.
Yes, exactly, that would be the next step I could think of; about to add in a separate PR.
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.
This will throw a warning because
None
is not allowed in string parameter: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.
I wonder why can't it be defaulted to
""
instead, like in@ulzha any thoughts?
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.
Alternatively, you can leave the None default and make it an
OptionalParameter