aboutsummaryrefslogtreecommitdiffstats
path: root/examples/async/eratosthenes/eratosthenes_trio.py
diff options
context:
space:
mode:
authorAdrian Herrmann <adrian.herrmann@qt.io>2023-02-17 17:47:25 +0100
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2023-03-02 17:38:30 +0000
commit1d537e8b068319ba90a1bbf2e328c8a9d687e04b (patch)
tree3986b9306ca92b159aa98e856cf9643852b5234c /examples/async/eratosthenes/eratosthenes_trio.py
parente48afcb0d6df296cbfd3d320640bc56d5aa85078 (diff)
examples: Improvements to the asyncio examples
- The asyncio AsyncHelper enters a quasi idle loop of switching between the asyncio event loop and the Qt event loop, where the asyncio event loop never receives new work at some point (besides yielding to Qt) and wastes memory and CPU cycles. Remedy this by signaling to AsyncHelper when asyncio's work is done. - Don't pass signals as parameters. - Fix comments in the asyncio examples that mentioned Trio. - Renamed the requirements files as only the Trio examples need them. - Remove unused imports. Task-number: PYSIDE-2169 Change-Id: Ia7197ea3446dd6ae514089b0dd260214c458db6a Reviewed-by: Christian Tismer <tismer@stackless.com> (cherry picked from commit e49516c724a11ac084372b5f0d7d62a35af363cc) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
Diffstat (limited to 'examples/async/eratosthenes/eratosthenes_trio.py')
-rw-r--r--examples/async/eratosthenes/eratosthenes_trio.py14
1 files changed, 7 insertions, 7 deletions
diff --git a/examples/async/eratosthenes/eratosthenes_trio.py b/examples/async/eratosthenes/eratosthenes_trio.py
index 5fb2a35be..862a4fddd 100644
--- a/examples/async/eratosthenes/eratosthenes_trio.py
+++ b/examples/async/eratosthenes/eratosthenes_trio.py
@@ -61,7 +61,7 @@ class MainWindow(QMainWindow):
widget.setPalette(palette)
-class Eratosthenes():
+class Eratosthenes(QObject):
""" This Sieve of Eratosthenes runs on a configurable tick (default
0.1 seconds). At each tick, a new subroutine will be created
@@ -69,6 +69,7 @@ class Eratosthenes():
these subroutines also operates on the same tick. """
def __init__(self, num, window, tick=0.1):
+ super().__init__()
self.num = num
self.sieve = [True] * self.num
self.base = 0
@@ -119,8 +120,6 @@ class Eratosthenes():
class AsyncHelper(QObject):
- trigger_signal = Signal()
-
class ReenterQtObject(QObject):
""" This is a QObject to which an event will be posted, allowing
Trio to resume when the event is handled. event.fn() is the
@@ -138,13 +137,14 @@ class AsyncHelper(QObject):
super().__init__(QEvent.Type(QEvent.User + 1))
self.fn = fn
- def __init__(self, entry=None):
+ def __init__(self, worker, entry):
super().__init__()
self.reenter_qt = self.ReenterQtObject()
self.entry = entry
- def set_entry(self, entry):
- self.entry = entry
+ self.worker = worker
+ if hasattr(self.worker, "start_signal") and isinstance(self.worker.start_signal, Signal):
+ self.worker.start_signal.connect(self.launch_guest_run)
@Slot()
def launch_guest_run(self):
@@ -183,7 +183,7 @@ if __name__ == "__main__":
app = QApplication(sys.argv)
main_window = MainWindow(rows, cols)
eratosthenes = Eratosthenes(num, main_window)
- async_helper = AsyncHelper(entry=eratosthenes.start)
+ async_helper = AsyncHelper(eratosthenes, eratosthenes.start)
# This establishes the entry point for the Trio guest run. It varies
# depending on how and when its event loop is to be triggered, e.g.,