Added some comments and try/except blocks

This commit is contained in:
nocturn9x 2020-07-07 14:09:31 +00:00
parent eb8770d0bf
commit 52ed68f35b
1 changed files with 34 additions and 24 deletions

View File

@ -63,23 +63,32 @@ class AsyncScheduler:
break
if not self.tasks:
if self.paused: # If there are no actively running tasks, we try to schedule the asleep ones
try:
self.check_sleeping()
except BaseException as error:
self.current_task.exc = error
self.reschedule_parent(self.current_task)
raise # Maybe find a better way to propagate errors?
if self.selector.get_map():
try:
self.check_io()
except BaseException as error:
self.current_task.exc = error
self.reschedule_parent(self.current_task)
raise # Maybe find a better way to propagate errors?
while self.tasks: # While there are tasks to run
self.current_task = self.tasks.popleft() # Sets the currently running task
try:
if self.current_task.status == "cancel": # Deferred cancellation
self.current_task.cancelled = True
self.current_task.throw(CancelledError)
else:
method, *args = self.current_task.run(self.current_task._notify) # Run a single step with the calculation (and awake event-waiting tasks if any)
self.current_task.status = "run"
try:
method, *args = self.current_task.run(self.current_task._notify) # Run a single step with the calculation
getattr(self, method)(*args) # Sneaky method call, thanks to David Beazley for this ;)
if self.event_waiting:
self.check_events()
except CancelledError as cancelled:
self.tasks.remove(cancelled.args[0])
self.tasks.remove(cancelled.args[0]) # Remove the dead task
except StopIteration as e: # Coroutine ends
self.current_task.result = e.args[0] if e.args else None
self.current_task.finished = True
@ -189,6 +198,7 @@ class AsyncScheduler:
if child.finished:
self.tasks.append(self.current_task)
else:
if child not in self.joined:
self.joined[child] = self.current_task
else: