diff options
| author | Damien George | 2020-11-30 17:38:30 +1100 |
|---|---|---|
| committer | Damien George | 2020-12-02 12:07:06 +1100 |
| commit | 309dfe39e07d3c3c8ba873ed09116cea9f6f52c0 (patch) | |
| tree | b9546223768ef0c32ee54ff890ba4be96097f66c /extmod | |
| parent | ca40eb0fdadd5a963b9a065999b092f029a598d5 (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>
Diffstat (limited to 'extmod')
| -rw-r--r-- | extmod/moduasyncio.c | 9 | ||||
| -rw-r--r-- | extmod/uasyncio/task.py | 3 |
2 files changed, 12 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: |
