Minor changes

This commit is contained in:
Nocturn9x 2022-02-27 14:01:07 +01:00
parent 800a173aa6
commit b8ee9945c1
3 changed files with 102 additions and 38 deletions

View File

@ -22,7 +22,7 @@ __version__ = (0, 0, 1)
from giambio import exceptions, socket, context, core, task, io from giambio import exceptions, socket, context, core, task, io
from giambio.traps import sleep, current_task from giambio.traps import sleep, current_task
from giambio.sync import Event, Queue, MemoryChannel from giambio.sync import Event, Queue, Channel, MemoryChannel, NetworkChannel
from giambio.runtime import run, clock, create_pool, get_event_loop, new_event_loop, with_timeout, skip_after from giambio.runtime import run, clock, create_pool, get_event_loop, new_event_loop, with_timeout, skip_after
from giambio.util import debug from giambio.util import debug
@ -34,6 +34,8 @@ __all__ = [
"sleep", "sleep",
"Event", "Event",
"Queue", "Queue",
"Channel",
"NetworkChannel",
"MemoryChannel", "MemoryChannel",
"run", "run",
"clock", "clock",

View File

@ -15,6 +15,8 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
""" """
from abc import ABC, abstractmethod
import abc
from collections import deque from collections import deque
from typing import Any, Optional from typing import Any, Optional
from giambio.traps import event_wait, event_set from giambio.traps import event_wait, event_set
@ -145,7 +147,60 @@ class Queue:
self.putters.clear() self.putters.clear()
class MemoryChannel: class Channel(ABC):
"""
A generic, two-way, full-duplex communication channel between
tasks. This is just an abstract base class!
"""
def __init__(self, maxsize: Optional[int] = None):
"""
Public object constructor
"""
self.maxsize = maxsize
self.closed = False
@abstractmethod
async def write(self, data: str):
"""
Writes data to the channel. Blocks if the internal
queue is full until a spot is available. Does nothing
if the channel has been closed
"""
return NotImplemented
@abstractmethod
async def read(self):
"""
Reads data from the channel. Blocks until
a message arrives or returns immediately if
one is already waiting
"""
return NotImplemented
@abstractmethod
async def close(self):
"""
Closes the memory channel. Any underlying
data is left for other tasks to read
"""
return NotImplemented
@abstractmethod
async def pending(self):
"""
Returns if there's pending
data to be read
"""
return NotImplemented
class MemoryChannel(Channel):
""" """
A two-way communication channel between tasks based A two-way communication channel between tasks based
on giambio's queueing mechanism. Operations on this on giambio's queueing mechanism. Operations on this
@ -158,16 +213,16 @@ class MemoryChannel:
Public object constructor Public object constructor
""" """
super().__init__(maxsize)
# We use a queue as our buffer # We use a queue as our buffer
self.buffer = Queue(maxsize=maxsize) self.buffer = Queue(maxsize=maxsize)
self.maxsize = maxsize
self.closed = False
async def write(self, data: str): async def write(self, data: str):
""" """
Writes data to the channel. Blocks if the internal Writes data to the channel. Blocks if the internal
queue is full until a spot is available queue is full until a spot is available. Does nothing
if the channel has been closed
""" """
if self.closed: if self.closed:
@ -186,7 +241,7 @@ class MemoryChannel:
async def close(self): async def close(self):
""" """
Closes the memory channel. Any underlying Closes the memory channel. Any underlying
data is left for clients to read data is left for other tasks to read
""" """
self.closed = True self.closed = True
@ -198,3 +253,10 @@ class MemoryChannel:
""" """
return bool(len(self.buffer)) return bool(len(self.buffer))
class NetworkChannel(Channel):
"""
A two-way communication channel between tasks based
on giambio's I/O mechanisms. This variant of a channel
"""

View File

@ -31,7 +31,7 @@ class BaseDebugger(ABC):
loop starts executing loop starts executing
""" """
raise NotImplementedError return NotImplemented
@abstractmethod @abstractmethod
def on_exit(self): def on_exit(self):
@ -40,7 +40,7 @@ class BaseDebugger(ABC):
loop exits entirely (all tasks completed) loop exits entirely (all tasks completed)
""" """
raise NotImplementedError return NotImplemented
@abstractmethod @abstractmethod
def on_task_schedule(self, task: Task, delay: float): def on_task_schedule(self, task: Task, delay: float):
@ -56,7 +56,7 @@ class BaseDebugger(ABC):
:type delay: float :type delay: float
""" """
raise NotImplementedError return NotImplemented
@abstractmethod @abstractmethod
def on_task_spawn(self, task: Task): def on_task_spawn(self, task: Task):
@ -69,7 +69,7 @@ class BaseDebugger(ABC):
:type task: :class: giambio.objects.Task :type task: :class: giambio.objects.Task
""" """
raise NotImplementedError return NotImplemented
@abstractmethod @abstractmethod
def on_task_exit(self, task: Task): def on_task_exit(self, task: Task):
@ -81,7 +81,7 @@ class BaseDebugger(ABC):
:type task: :class: giambio.objects.Task :type task: :class: giambio.objects.Task
""" """
raise NotImplementedError return NotImplemented
@abstractmethod @abstractmethod
def before_task_step(self, task: Task): def before_task_step(self, task: Task):
@ -94,7 +94,7 @@ class BaseDebugger(ABC):
:type task: :class: giambio.objects.Task :type task: :class: giambio.objects.Task
""" """
raise NotImplementedError return NotImplemented
@abstractmethod @abstractmethod
def after_task_step(self, task: Task): def after_task_step(self, task: Task):
@ -107,7 +107,7 @@ class BaseDebugger(ABC):
:type task: :class: giambio.objects.Task :type task: :class: giambio.objects.Task
""" """
raise NotImplementedError return NotImplemented
@abstractmethod @abstractmethod
def before_sleep(self, task: Task, seconds: float): def before_sleep(self, task: Task, seconds: float):
@ -123,7 +123,7 @@ class BaseDebugger(ABC):
:type seconds: int :type seconds: int
""" """
raise NotImplementedError return NotImplemented
@abstractmethod @abstractmethod
def after_sleep(self, task: Task, seconds: float): def after_sleep(self, task: Task, seconds: float):
@ -139,7 +139,7 @@ class BaseDebugger(ABC):
:type seconds: float :type seconds: float
""" """
raise NotImplementedError return NotImplemented
@abstractmethod @abstractmethod
def before_io(self, timeout: float): def before_io(self, timeout: float):
@ -153,7 +153,7 @@ class BaseDebugger(ABC):
:type timeout: float :type timeout: float
""" """
raise NotImplementedError return NotImplemented
@abstractmethod @abstractmethod
def after_io(self, timeout: float): def after_io(self, timeout: float):
@ -167,7 +167,7 @@ class BaseDebugger(ABC):
:type timeout: float :type timeout: float
""" """
raise NotImplementedError return NotImplemented
@abstractmethod @abstractmethod
def before_cancel(self, task: Task): def before_cancel(self, task: Task):
@ -180,7 +180,7 @@ class BaseDebugger(ABC):
:type task: :class: giambio.objects.Task :type task: :class: giambio.objects.Task
""" """
raise NotImplementedError return NotImplemented
@abstractmethod @abstractmethod
def after_cancel(self, task: Task) -> object: def after_cancel(self, task: Task) -> object:
@ -193,7 +193,7 @@ class BaseDebugger(ABC):
:type task: :class: giambio.objects.Task :type task: :class: giambio.objects.Task
""" """
raise NotImplementedError return NotImplemented
@abstractmethod @abstractmethod
def on_exception_raised(self, task: Task, exc: BaseException): def on_exception_raised(self, task: Task, exc: BaseException):
@ -208,4 +208,4 @@ class BaseDebugger(ABC):
:type exc: BaseException :type exc: BaseException
""" """
raise NotImplementedError return NotImplemented