aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAdrian Herrmann <adrian.herrmann@qt.io>2022-10-06 13:58:54 +0200
committerAdrian Herrmann <adrian.herrmann@qt.io>2022-10-07 08:49:48 +0000
commite81f61f8515f065a9d76302d60fa974ce617e9cd (patch)
tree87366d815fc1376ce29179df07511e1cda72fead
parentf7855e44da80e172adc7eec5d92ebd7278677b30 (diff)
examples: Fix docstr in async/eratosthenes re:tick
In the "eratosthenes" async example, the tick is not actually coordinated between subroutines as claimed, update docstring to reflect this (and streamline code slightly in the process). Task-number: PYSIDE-769 Change-Id: I5f040b558851481355650c08ab15ac10e5b1a88c Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io> (cherry picked from commit 87a9cd7472b7da57f86299dc58f0e24fc30f4878) Reviewed-by: Christian Tismer <tismer@stackless.com>
-rw-r--r--examples/async/eratosthenes/eratosthenes.py17
1 files changed, 6 insertions, 11 deletions
diff --git a/examples/async/eratosthenes/eratosthenes.py b/examples/async/eratosthenes/eratosthenes.py
index 2b7581206..e57c3c3bc 100644
--- a/examples/async/eratosthenes/eratosthenes.py
+++ b/examples/async/eratosthenes/eratosthenes.py
@@ -103,8 +103,7 @@ class Eratosthenes():
""" This Sieve of Eratosthenes runs on a configurable tick (default
0.1 seconds). At each tick, a new subroutine will be created
that will check multiples of the next prime number. Each of
- these subroutines also operates on the same second tick. The
- tick is coordinated through the trio event loop's internal clock. """
+ these subroutines also operates on the same tick. """
def __init__(self, num, window, tick=0.1):
self.num = num
@@ -116,21 +115,18 @@ class Eratosthenes():
self.done = False
self.nursery = None
- def get_tick(self):
- return trio.lowlevel.current_clock().current_time() + self.tick
-
async def start(self):
async with trio.open_nursery() as self.nursery:
self.nursery.start_soon(self.update_text)
while self.base <= self.num / 2:
- await trio.sleep_until(self.get_tick())
+ await trio.sleep(self.tick)
for i in range(self.base + 1, self.num):
if self.sieve[i]:
self.base = i
break
self.nursery.start_soon(self.mark_number, self.base + 1)
while sum(self.coroutines) > 0:
- await trio.sleep_until(self.get_tick())
+ await trio.sleep(self.tick)
self.done = True
async def mark_number(self, base):
@@ -141,14 +137,13 @@ class Eratosthenes():
if self.sieve[i - 1]:
self.sieve[i - 1] = False
self.window.set_num.emit(i, color)
- await trio.sleep_until(self.get_tick())
+ await trio.sleep(self.tick)
self.coroutines[id] = 0
async def update_text(self):
while not self.done:
- tick = self.get_tick()
- await trio.sleep_until(tick)
- if int(tick) % 2:
+ await trio.sleep(self.tick)
+ if int(trio.lowlevel.current_clock().current_time() + self.tick) % 2:
text = "⚙️ ...Calculating prime numbers... ⚙️"
else:
text = "👩‍💻 ...Hacking the universe... 👩‍💻"