summaryrefslogtreecommitdiff
path: root/extmod/uasyncio/task.py
diff options
context:
space:
mode:
Diffstat (limited to 'extmod/uasyncio/task.py')
-rw-r--r--extmod/uasyncio/task.py31
1 files changed, 11 insertions, 20 deletions
diff --git a/extmod/uasyncio/task.py b/extmod/uasyncio/task.py
index 68ddf496f..26df7b172 100644
--- a/extmod/uasyncio/task.py
+++ b/extmod/uasyncio/task.py
@@ -123,6 +123,7 @@ class Task:
def __init__(self, coro, globals=None):
self.coro = coro # Coroutine of this Task
self.data = None # General data for queue it is waiting on
+ self.state = True # None, False, True or a TaskQueue instance
self.ph_key = 0 # Pairing heap
self.ph_child = None # Paring heap
self.ph_child_last = None # Paring heap
@@ -130,30 +131,30 @@ class Task:
self.ph_rightmost_parent = None # Paring heap
def __iter__(self):
- if self.coro is self:
- # Signal that the completed-task has been await'ed on.
- self.waiting = None
- elif not hasattr(self, "waiting"):
- # Lazily allocated head of linked list of Tasks waiting on completion of this task.
- self.waiting = TaskQueue()
+ if not self.state:
+ # Task finished, signal that is has been await'ed on.
+ self.state = False
+ elif self.state is True:
+ # Allocated head of linked list of Tasks waiting on completion of this task.
+ self.state = TaskQueue()
return self
def __next__(self):
- if self.coro is self:
+ if not self.state:
# Task finished, raise return value to caller so it can continue.
raise self.data
else:
# Put calling task on waiting queue.
- self.waiting.push_head(core.cur_task)
+ self.state.push_head(core.cur_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
+ return not self.state
def cancel(self):
# Check if task is already finished.
- if self.coro is self:
+ if not self.state:
return False
# Can't cancel self (not supported yet).
if self is core.cur_task:
@@ -172,13 +173,3 @@ class Task:
core._task_queue.push_head(self)
self.data = core.CancelledError
return True
-
- def throw(self, value):
- # This task raised an exception which was uncaught; handle that now.
- # Set the data because it was cleared by the main scheduling loop.
- self.data = value
- if not hasattr(self, "waiting"):
- # Nothing await'ed on the task so call the exception handler.
- core._exc_context["exception"] = value
- core._exc_context["future"] = self
- core.Loop.call_exception_handler(core._exc_context)