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>
This commit is contained in:
Damien George 2020-11-30 17:38:30 +11:00
parent ca40eb0fda
commit 309dfe39e0
4 changed files with 102 additions and 0 deletions

View File

@ -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); 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) { STATIC mp_obj_t task_cancel(mp_obj_t self_in) {
mp_obj_task_t *self = MP_OBJ_TO_PTR(self_in); mp_obj_task_t *self = MP_OBJ_TO_PTR(self_in);
// Check if task is already finished. // 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) { if (self->waiting != mp_const_none && self->waiting != mp_const_false) {
dest[0] = self->waiting; 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) { } else if (attr == MP_QSTR_cancel) {
dest[0] = MP_OBJ_FROM_PTR(&task_cancel_obj); dest[0] = MP_OBJ_FROM_PTR(&task_cancel_obj);
dest[1] = self_in; dest[1] = self_in;

View File

@ -148,6 +148,9 @@ class Task:
# Set calling task's data to this task that it waits on, to double-link it. # Set calling task's data to this task that it waits on, to double-link it.
core.cur_task.data = self core.cur_task.data = self
def done(self):
return self.coro is self
def cancel(self): def cancel(self):
# Check if task is already finished. # Check if task is already finished.
if self.coro is self: if self.coro is self:

View File

@ -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())

View File

@ -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