Repository navigation
Adapt to Task.cancel now being deprecated. #490
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
base: main
Are you sure you want to change the base?
Changes from all commits
765951e
e299bbe
628e0ec
c48e53f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| import os | ||
| import threading | ||
| import time | ||
|
|
||
| from ewokscore import TaskWithProgress | ||
|
|
@@ -24,7 +25,7 @@ def run(self): | |
| period = self.inputs.delay / 40 | ||
| for i in range(40): | ||
| time.sleep(period) | ||
| if self.cancelled: | ||
| if self.stop_requested: | ||
| raise CustomCancelled(f"cancelled after {(i + 1) * period} seconds") | ||
|
|
||
| if self.inputs.fail: | ||
|
|
@@ -37,9 +38,9 @@ def run(self): | |
|
|
||
| self.outputs.result = result | ||
|
|
||
| def cancel(self): | ||
| """Interpretation #2: self.cancelled is the request not the state.""" | ||
| self.cancelled = True | ||
| def _on_stop_requested(self): | ||
| """Nothing to do: `run()` polls `self.stop_requested`.""" | ||
| pass | ||
|
|
||
|
|
||
| class RequestCancelTask( | ||
|
|
@@ -53,15 +54,15 @@ def run(self): | |
| step = 0.05 | ||
| elapsed = 0.0 | ||
| while elapsed < self.inputs.duration: | ||
| if self.cancelled: | ||
| if self.stop_requested: | ||
| return | ||
| time.sleep(step) | ||
| elapsed += step | ||
| self.outputs.result = f"slept {elapsed:.2f}s" | ||
|
|
||
| def cancel(self): | ||
| """Interpretation #2: self.cancelled is the request not the state.""" | ||
| self.cancelled = True | ||
| def _on_stop_requested(self): | ||
| """Nothing to do: `run()` polls `self.stop_requested`.""" | ||
| pass | ||
|
|
||
|
|
||
| class PartialCancelTask( | ||
|
|
@@ -77,60 +78,53 @@ def run(self): | |
| step = 0.05 | ||
| elapsed = 0.0 | ||
| while elapsed < self.inputs.duration: | ||
| if self.cancelled: | ||
| if self.stop_requested: | ||
| return | ||
| time.sleep(step) | ||
| elapsed += step | ||
|
|
||
| self.outputs.second = "second done" | ||
|
|
||
| def cancel(self): | ||
| """Interpretation #2: self.cancelled is the request not the state.""" | ||
| self.cancelled = True | ||
| def _on_stop_requested(self): | ||
| """Nothing to do: `run()` polls `self.stop_requested`.""" | ||
| pass | ||
|
|
||
|
|
||
| class IgnoreCancelTask( | ||
| Task, | ||
| input_names=["duration"], | ||
| output_names=["result"], | ||
| ): | ||
| """Never checks `self.cancelled`; always completes fully despite cancel().""" | ||
| """Never checks `self.stop_requested`; always completes fully despite request_stop().""" | ||
|
|
||
| def run(self): | ||
| time.sleep(self.inputs.duration) | ||
| self.outputs.result = "completed despite abort" | ||
|
|
||
| def cancel(self): | ||
| """Interpretation #2: self.cancelled is the request not the state.""" | ||
| self.cancelled = True | ||
| def _on_stop_requested(self): | ||
| """Nothing to do: `run()` polls `self.stop_requested`.""" | ||
| pass | ||
|
|
||
|
|
||
| class StateCancelTask( | ||
| class InterruptCancelTask( | ||
|
Comment on lines
-108
to
+109
Member
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. Renamed for clarity. I haven't saw any project using it but still breaking the public API. |
||
| Task, | ||
| input_names=["duration"], | ||
| output_names=["result"], | ||
| ): | ||
| """cancel() only records a request; run() itself sets `self.cancelled` | ||
| once stopped.""" | ||
| """Never polls `self.stop_requested`; `_on_stop_requested()` interrupts | ||
| the blocking wait of run() instead.""" | ||
|
|
||
| def __init__(self, **kwargs): | ||
| super().__init__(**kwargs) | ||
| self.__cancel_requested = False | ||
| self.__stop_event = threading.Event() | ||
|
|
||
| def run(self): | ||
| step = 0.05 | ||
| elapsed = 0.0 | ||
| while elapsed < self.inputs.duration: | ||
| if self.__cancel_requested: | ||
| self.cancelled = True | ||
| return | ||
| time.sleep(step) | ||
| elapsed += step | ||
| self.outputs.result = f"slept {elapsed:.2f}s" | ||
| if self.__stop_event.wait(timeout=self.inputs.duration): | ||
| return | ||
| self.outputs.result = f"slept {self.inputs.duration:.2f}s" | ||
|
|
||
| def cancel(self): | ||
| """Interpretation #1: self.cancelled is the state not the request.""" | ||
| self.__cancel_requested = True | ||
| def _on_stop_requested(self): | ||
| self.__stop_event.set() | ||
|
Comment on lines
-121
to
+127
Member
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. Simplification of the cancellation. The origin of the modification is that stop_requested (replacing cancelled) has no more setter. It is set directly when stop_requested is called. Which makes it more robust and clarify the states. This is more 'strict' this way for the best. |
||
|
|
||
|
|
||
| class TimedTask( | ||
|
|
||
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.
We could keep version 5 compatibility but this is a bit cumbersome for the inheritance. So I moved forward. Let me know if there is reason to keep compatibly with version 5.0