Added 2 more attributed to the Task object

This commit is contained in:
nocturn9x 2020-03-20 10:26:42 +01:00
parent 84118f3990
commit f7fadabc5e
1 changed files with 14 additions and 14 deletions

View File

@ -5,7 +5,7 @@ from selectors import DefaultSelector, EVENT_READ, EVENT_WRITE
from inspect import iscoroutine from inspect import iscoroutine
from functools import wraps from functools import wraps
import socket import socket
from .exceptions import GiambioError from .exceptions import GiambioError, AlreadyJoinedError
import traceback import traceback
return_values = {} # Saves the return values from coroutines return_values = {} # Saves the return values from coroutines
@ -29,6 +29,8 @@ class Task:
self.coroutine = coroutine self.coroutine = coroutine
self.status = False # Not ran yet self.status = False # Not ran yet
self.joined = False self.joined = False
self.ret_val = None # Return value is saved here
self.exception = None # If errored, the exception is saved here
def run(self): def run(self):
self.status = True self.status = True
@ -37,6 +39,7 @@ class Task:
def __repr__(self): def __repr__(self):
return f"<giambio.core.Task({self.coroutine}, {self.status}, {self.joined})" return f"<giambio.core.Task({self.coroutine}, {self.status}, {self.joined})"
class EventLoop: class EventLoop:
"""Implementation of an event loop, alternates between execution of coroutines (asynchronous functions) """Implementation of an event loop, alternates between execution of coroutines (asynchronous functions)
@ -69,16 +72,15 @@ class EventLoop:
meth, *args = self.running.run() # Sneaky method call, thanks to David Beazley for this ;) meth, *args = self.running.run() # Sneaky method call, thanks to David Beazley for this ;)
getattr(self, meth)(*args) getattr(self, meth)(*args)
except StopIteration as e: except StopIteration as e:
return_values[self.running] = 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.waitlist.pop(self.running, ())) # Reschedules the parent task self.to_run.extend(self.waitlist.pop(self.running, ())) # Reschedules the parent task
except Exception as has_raised: except Exception as has_raised:
if self.running.joined: if self.running.joined:
exceptions[self.running] = has_raised # Errored? Save the exception self.running.exception = has_raised # Errored? Save the exception
else: # If the task is not joined, the exception would disappear, but not in giambio else: # If the task is not joined, the exception would disappear, but not in giambio
raise GiambioError from has_raised raise GiambioError from has_raised
self.to_run.extend(self.waitlist.pop(self.running, ())) self.to_run.extend(self.waitlist.pop(self.running, ()))
def spawn(self, coroutine: types.coroutine): def spawn(self, coroutine: types.coroutine):
"""Schedules a task for execution, appending it to the call stack""" """Schedules a task for execution, appending it to the call stack"""
@ -131,7 +133,6 @@ class EventLoop:
raise AlreadyJoinedError("Joining the same task multiple times is not allowed!") raise AlreadyJoinedError("Joining the same task multiple times is not allowed!")
class AsyncSocket(object): class AsyncSocket(object):
"""Abstraction layer for asynchronous sockets""" """Abstraction layer for asynchronous sockets"""
@ -202,17 +203,16 @@ def join(task: Task):
task.joined = True task.joined = True
yield "want_join", task yield "want_join", task
has_raised = exceptions.pop(task, None) if task.exception:
if has_raised:
print("Traceback (most recent call last):") print("Traceback (most recent call last):")
traceback.print_tb(has_raised.__traceback__) traceback.print_tb(task.exception.__traceback__)
ename = type(has_raised).__name__ ename = type(task.exception).__name__
if str(has_raised): if str(task.exception):
print(f"{ename}: {has_raised}") print(f"{ename}: {task.exception}")
else: else:
print(has_raised) print(task.exception)
raise GiambioError from has_raised raise GiambioError from task.exception
return return_values.pop(task, None) return task.ret_val