aboutsummaryrefslogtreecommitdiffstats
path: root/sources/pyside6/PySide6/QtAsyncio/tasks.py
diff options
context:
space:
mode:
authorAdrian Herrmann <adrian.herrmann@qt.io>2023-09-29 14:05:41 +0200
committerAdrian Herrmann <adrian.herrmann@qt.io>2023-09-29 14:49:12 +0200
commit1961d9ad766e59a9d72dfa2eb2317ba34e9e377d (patch)
treebaafcda25e43488556e29f1ccf0c454a432f7421 /sources/pyside6/PySide6/QtAsyncio/tasks.py
parent6ab7d0b384b58c52faf924278f032e796ce42f07 (diff)
QtAsyncio: Miscellaneous improvements to Task
- Use asyncio._register_task() and asyncio._unregister_task() as demanded by the API (extending asyncio, Task lifetime support) - Add some comments and a missing return type in signature Pick-to: 6.6 Task-number: PYSIDE-769 Change-Id: I3ffdf0dc5f7b127c0dd9f2fb63eecb057d123744 Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
Diffstat (limited to 'sources/pyside6/PySide6/QtAsyncio/tasks.py')
-rw-r--r--sources/pyside6/PySide6/QtAsyncio/tasks.py14
1 files changed, 9 insertions, 5 deletions
diff --git a/sources/pyside6/PySide6/QtAsyncio/tasks.py b/sources/pyside6/PySide6/QtAsyncio/tasks.py
index 0ee482f38..d59073e88 100644
--- a/sources/pyside6/PySide6/QtAsyncio/tasks.py
+++ b/sources/pyside6/PySide6/QtAsyncio/tasks.py
@@ -27,6 +27,8 @@ class QAsyncioTask(futures.QAsyncioFuture):
self._cancellation_requests = 0
+ asyncio._register_task(self) # type: ignore[arg-type]
+
def __repr__(self) -> str:
if self._state == futures.QAsyncioFuture.FutureState.PENDING:
state = "Pending"
@@ -60,17 +62,19 @@ class QAsyncioTask(futures.QAsyncioFuture):
def _step(self,
exception_or_future: typing.Union[
- Exception, futures.QAsyncioFuture, None] = None):
+ Exception, futures.QAsyncioFuture, None] = None) -> None:
if self.done():
return
result = None
- asyncio._enter_task(self._loop, self) # type: ignore[arg-type]
-
try:
+ asyncio._enter_task(self._loop, self) # type: ignore[arg-type]
if exception_or_future is None:
result = self._coro.send(None)
elif asyncio.futures.isfuture(exception_or_future):
+ # If the future has an exception set by set_exception(), this will raise it.
+ # If the future has been cancelled, this will raise CancelledError.
+ # If the future's result isn't yet available, this will raise InvalidStateError.
exception_or_future.result()
exception_or_future = None
result = self._coro.send(None)
@@ -92,6 +96,7 @@ class QAsyncioTask(futures.QAsyncioFuture):
else:
self._loop.call_soon(self._step, exception_or_future, context=self._context)
finally:
+ asyncio._leave_task(self._loop, self) # type: ignore[arg-type]
if self._exception:
self._loop.call_exception_handler({
"message": (str(self._exception) if self._exception
@@ -105,10 +110,9 @@ class QAsyncioTask(futures.QAsyncioFuture):
})
if self.done():
self._schedule_callbacks()
+ asyncio._unregister_task(self) # type: ignore[arg-type]
self._loop.stop()
- asyncio._leave_task(self._loop, self) # type: ignore[arg-type]
-
def get_stack(self, *, limit=None) -> typing.List[typing.Any]:
# TODO
raise NotImplementedError("QtTask.get_stack is not implemented")