-
Notifications
You must be signed in to change notification settings - Fork 1
387 owewokswidgetwithtaskstack provide api to cancel a task 3 add example #411
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
woutdenolf
merged 4 commits into
387-owewokswidgetwithtaskstack-provide-api-to-cancel-a-task-3
from
387-owewokswidgetwithtaskstack-provide-api-to-cancel-a-task-3-add_example
Jul 17, 2026
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 |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| import logging | ||
| import sys | ||
| from concurrent.futures import ProcessPoolExecutor | ||
| from concurrent.futures import ThreadPoolExecutor | ||
| from functools import partial | ||
|
|
||
| from AnyQt.QtWidgets import QApplication | ||
| from AnyQt.QtWidgets import QGridLayout | ||
| from AnyQt.QtWidgets import QHBoxLayout | ||
| from AnyQt.QtWidgets import QLabel | ||
| from AnyQt.QtWidgets import QListWidget | ||
| from AnyQt.QtWidgets import QListWidgetItem | ||
| from AnyQt.QtWidgets import QPushButton | ||
| from AnyQt.QtWidgets import QTextEdit | ||
| from AnyQt.QtWidgets import QVBoxLayout | ||
| from AnyQt.QtWidgets import QWidget | ||
| from ewokscore.tests.examples.tasks.sumtask import SumTask | ||
|
|
||
| from ewoksorange.gui.concurrency.executor import EwoksExecutor | ||
| from ewoksorange.gui.concurrency.executor import SubmitPolicy | ||
|
|
||
|
|
||
| class Window(QWidget): | ||
| def __init__(self): | ||
| super().__init__() | ||
|
|
||
| self.setWindowTitle("EwoksExecutor Demo") | ||
|
|
||
| self.log = QTextEdit(readOnly=True) | ||
|
|
||
| self.pending = QListWidget() | ||
| self.pending_items = {} | ||
|
|
||
| self.executors = { | ||
| "T DROP": EwoksExecutor( | ||
| ThreadPoolExecutor(max_workers=1), | ||
| SubmitPolicy.DROP_IF_BUSY, | ||
| ), | ||
| "T QUEUE": EwoksExecutor( | ||
| ThreadPoolExecutor(max_workers=1), | ||
| SubmitPolicy.ALWAYS, | ||
| ), | ||
| "T PARALLEL": EwoksExecutor( | ||
| ThreadPoolExecutor(max_workers=4), | ||
| SubmitPolicy.ALWAYS, | ||
| ), | ||
| "P DROP": EwoksExecutor( | ||
| ProcessPoolExecutor(max_workers=1), | ||
| SubmitPolicy.DROP_IF_BUSY, | ||
| ), | ||
| "P QUEUE": EwoksExecutor( | ||
| ProcessPoolExecutor(max_workers=1), | ||
| SubmitPolicy.ALWAYS, | ||
| ), | ||
| "P PARALLEL": EwoksExecutor( | ||
| ProcessPoolExecutor(max_workers=4), | ||
| SubmitPolicy.ALWAYS, | ||
| ), | ||
| } | ||
|
|
||
| for name, executor in self.executors.items(): | ||
| executor.submitted.connect(partial(self.on_submitted, name)) | ||
| executor.ignored.connect(partial(self.on_ignored, name)) | ||
| executor.succeeded.connect(partial(self.on_succeeded, name)) | ||
| executor.failed.connect(partial(self.on_failed, name)) | ||
|
|
||
| layout = QVBoxLayout(self) | ||
| grid = QGridLayout() | ||
|
|
||
| buttons = [ | ||
| ("Submit x5 (Thread Drop)", "T DROP"), | ||
| ("Submit x5 (Process Drop)", "P DROP"), | ||
| ("Submit x5 (Thread Queue)", "T QUEUE"), | ||
| ("Submit x5 (Process Queue)", "P QUEUE"), | ||
| ("Submit x5 (Thread Parallel)", "T PARALLEL"), | ||
| ("Submit x5 (Process Parallel)", "P PARALLEL"), | ||
| ] | ||
|
|
||
| for i, (text, key) in enumerate(buttons): | ||
| button = QPushButton(text) | ||
| button.clicked.connect(partial(self.on_button_clicked, key)) | ||
| grid.addWidget(button, i // 2, i % 2) | ||
|
|
||
| layout.addLayout(grid) | ||
|
|
||
| body = QHBoxLayout() | ||
|
|
||
| log_box = QVBoxLayout() | ||
| log_box.addWidget(QLabel("Logs")) | ||
| log_box.addWidget(self.log) | ||
| body.addLayout(log_box, 2) | ||
|
|
||
| pending_box = QVBoxLayout() | ||
| pending_box.addWidget(QLabel("Pending jobs")) | ||
| pending_box.addWidget(self.pending) | ||
| body.addLayout(pending_box, 1) | ||
|
|
||
| layout.addLayout(body) | ||
|
|
||
| self.counter = 1 | ||
|
|
||
| def on_submitted(self, name, future): | ||
| if future is None: | ||
| self.log.append(f"[{name}] Submitted -> None") | ||
| else: | ||
| self.log.append(f"[{name}] Submitted -> {id(future)}") | ||
| item = QListWidgetItem(f"[{name}] {id(future)}") | ||
| self.pending.addItem(item) | ||
| self.pending_items[id(future)] = item | ||
|
|
||
| def on_ignored(self, name): | ||
| self.log.append(f"[{name}] ⚠ Ignored") | ||
|
|
||
| def on_succeeded(self, name, future, result): | ||
| values = {k: v.value for k, v in result.items()} | ||
| self.log.append(f"[{name}] ✅ {id(future)} -> {values}") | ||
| self.remove_pending(future) | ||
|
|
||
| def on_failed(self, name, future, exc): | ||
| self.log.append(f"[{name}] ❌ {id(future)} -> {exc}") | ||
| self.remove_pending(future) | ||
|
|
||
| def remove_pending(self, future): | ||
| item = self.pending_items.pop(id(future), None) | ||
| if item is not None: | ||
| self.pending.takeItem(self.pending.row(item)) | ||
|
|
||
| def on_button_clicked(self, key, _checked=False): | ||
| self.submit_many(self.executors[key]) | ||
|
|
||
| def submit_many(self, executor): | ||
| for _ in range(5): | ||
| executor.submit_task( | ||
| SumTask, inputs={"a": self.counter, "b": self.counter, "delay": 2} | ||
| ) | ||
| self.counter += 1 | ||
|
|
||
| def closeEvent(self, event): | ||
| for executor in self.executors.values(): | ||
| executor.shutdown() | ||
| super().closeEvent(event) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| logging.basicConfig(level=logging.INFO) | ||
|
|
||
| app = QApplication(sys.argv) | ||
|
|
||
| window = Window() | ||
| window.resize(900, 500) | ||
| window.show() | ||
|
|
||
| sys.exit(app.exec()) |
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 |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| from .EwoksExecutor import EwoksExecutor # noqa F401 | ||
| from .EwoksExecutor import SubmitPolicy # noqa F401 | ||
|
Member
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. 👍 |
||
| from .TaskFuture import TaskFuture # noqa F401 | ||
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.
I don't see why the abort should always be set. Should it be more upper (line 54 and replacing line 50?)
Uh oh!
There was an error while loading. Please reload this page.
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 is the stop the
_watch_abortthread which is waiting onself._abort_event.wait(). See above, it has a comment.