summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDamien George <damien@micropython.org>2020-11-30 17:38:30 +1100
committerDamien George <damien@micropython.org>2020-12-02 12:07:06 +1100
commit309dfe39e07d3c3c8ba873ed09116cea9f6f52c0 (patch)
treeb9546223768ef0c32ee54ff890ba4be96097f66c
parentca40eb0fdadd5a963b9a065999b092f029a598d5 (diff)
extmod/uasyncio: Add Task.done() method.
This is added because task.coro==None is no longer the way to detect if a task is finished. Providing a (CPython compatible) function for this allows the implementation to be abstracted away. Signed-off-by: Damien George <damien@micropython.org>
-rw-r--r--extmod/moduasyncio.c9
-rw-r--r--extmod/uasyncio/task.py3
-rw-r--r--tests/extmod/uasyncio_task_done.py66
-rw-r--r--tests/extmod/uasyncio_task_done.py.exp24
4 files changed, 102 insertions, 0 deletions
diff --git a/extmod/moduasyncio.c b/extmod/moduasyncio.c
index e8822c069..fe0f748ca 100644
--- a/extmod/moduasyncio.c
+++ b/extmod/moduasyncio.c
@@ -167,6 +167,12 @@ STATIC mp_obj_t task_make_new(const mp_obj_type_t *type, size_t n_args, size_t n
return MP_OBJ_FROM_PTR(self);
}
+STATIC mp_obj_t task_done(mp_obj_t self_in) {
+ mp_obj_task_t *self = MP_OBJ_TO_PTR(self_in);
+ return mp_obj_new_bool(TASK_IS_DONE(self));
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(task_done_obj, task_done);
+
STATIC mp_obj_t task_cancel(mp_obj_t self_in) {
mp_obj_task_t *self = MP_OBJ_TO_PTR(self_in);
// Check if task is already finished.
@@ -242,6 +248,9 @@ STATIC void task_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
if (self->waiting != mp_const_none && self->waiting != mp_const_false) {
dest[0] = self->waiting;
}
+ } else if (attr == MP_QSTR_done) {
+ dest[0] = MP_OBJ_FROM_PTR(&task_done_obj);
+ dest[1] = self_in;
} else if (attr == MP_QSTR_cancel) {
dest[0] = MP_OBJ_FROM_PTR(&task_cancel_obj);
dest[1] = self_in;
diff --git a/extmod/uasyncio/task.py b/extmod/uasyncio/task.py
index 2420ab719..68ddf496f 100644
--- a/extmod/uasyncio/task.py
+++ b/extmod/uasyncio/task.py
@@ -148,6 +148,9 @@ class Task:
# Set calling task's data to this task that it waits on, to double-link it.
core.cur_task.data = self
+ def done(self):
+ return self.coro is self
+
def cancel(self):
# Check if task is already finished.
if self.coro is self:
diff --git a/tests/extmod/uasyncio_task_done.py b/tests/extmod/uasyncio_task_done.py
new file mode 100644
index 000000000..2700da8c3
--- /dev/null
+++ b/tests/extmod/uasyncio_task_done.py
@@ -0,0 +1,66 @@
+# Test the Task.done() method
+
+try:
+ import uasyncio as asyncio
+except ImportError:
+ try:
+ import asyncio
+ except ImportError:
+ print("SKIP")
+ raise SystemExit
+
+
+async def task(t, exc=None):
+ print("task start")
+ if t >= 0:
+ await asyncio.sleep(t)
+ if exc:
+ raise exc
+ print("task done")
+
+
+async def main():
+ # Task that finishes immediately.
+ print("=" * 10)
+ t = asyncio.create_task(task(-1))
+ print(t.done())
+ await asyncio.sleep(0)
+ print(t.done())
+ await t
+ print(t.done())
+
+ # Task that starts, runs and finishes.
+ print("=" * 10)
+ t = asyncio.create_task(task(0.01))
+ print(t.done())
+ await asyncio.sleep(0)
+ print(t.done())
+ await t
+ print(t.done())
+
+ # Task that raises immediately.
+ print("=" * 10)
+ t = asyncio.create_task(task(-1, ValueError))
+ print(t.done())
+ await asyncio.sleep(0)
+ print(t.done())
+ try:
+ await t
+ except ValueError as er:
+ print(repr(er))
+ print(t.done())
+
+ # Task that raises after a delay.
+ print("=" * 10)
+ t = asyncio.create_task(task(0.01, ValueError))
+ print(t.done())
+ await asyncio.sleep(0)
+ print(t.done())
+ try:
+ await t
+ except ValueError as er:
+ print(repr(er))
+ print(t.done())
+
+
+asyncio.run(main())
diff --git a/tests/extmod/uasyncio_task_done.py.exp b/tests/extmod/uasyncio_task_done.py.exp
new file mode 100644
index 000000000..ddda04c5e
--- /dev/null
+++ b/tests/extmod/uasyncio_task_done.py.exp
@@ -0,0 +1,24 @@
+==========
+False
+task start
+task done
+True
+True
+==========
+False
+task start
+False
+task done
+True
+==========
+False
+task start
+True
+ValueError()
+True
+==========
+False
+task start
+False
+ValueError()
+True