mirror of https://github.com/nocturn9x/giambio.git
Updated cancellation mechanism
This commit is contained in:
parent
d06813cd73
commit
87fd7af36b
|
@ -50,6 +50,7 @@ class EventLoop:
|
||||||
self.running = None # This will always point to the currently running coroutine (Task object)
|
self.running = None # This will always point to the currently running coroutine (Task object)
|
||||||
self.joined = defaultdict(list) # Tasks that want to join
|
self.joined = defaultdict(list) # Tasks that want to join
|
||||||
self.clock = default_timer # Monotonic clock to keep track of elapsed time
|
self.clock = default_timer # Monotonic clock to keep track of elapsed time
|
||||||
|
self.tree = set() # Keep track of coroutines to know their state
|
||||||
|
|
||||||
def loop(self):
|
def loop(self):
|
||||||
"""Main event loop for giambio"""
|
"""Main event loop for giambio"""
|
||||||
|
@ -76,6 +77,10 @@ class EventLoop:
|
||||||
except StopIteration as e:
|
except StopIteration as e:
|
||||||
self.running.ret_value = e.args[0] if e.args else None # Saves the return value
|
self.running.ret_value = e.args[0] if e.args else None # Saves the return value
|
||||||
self.to_run.extend(self.joined.pop(self.running, ())) # Reschedules the parent task
|
self.to_run.extend(self.joined.pop(self.running, ())) # Reschedules the parent task
|
||||||
|
except CancelledError:
|
||||||
|
self.tree.discard(self.running)
|
||||||
|
self.running.cancelled = True # Update the coroutine status
|
||||||
|
raise
|
||||||
except Exception as has_raised:
|
except Exception as has_raised:
|
||||||
if self.running.joined:
|
if self.running.joined:
|
||||||
self.running.exception = has_raised # Errored? Save the exception
|
self.running.exception = has_raised # Errored? Save the exception
|
||||||
|
@ -223,6 +228,6 @@ def join(task: Task):
|
||||||
|
|
||||||
@types.coroutine
|
@types.coroutine
|
||||||
def cancel(task: Task):
|
def cancel(task: Task):
|
||||||
task.cancelled = True
|
|
||||||
yield "want_cancel", task
|
yield "want_cancel", task
|
||||||
|
assert task.cancelled
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue