mirror of https://github.com/nocturn9x/giambio.git
Minor changes
This commit is contained in:
parent
800a173aa6
commit
b8ee9945c1
|
@ -22,7 +22,7 @@ __version__ = (0, 0, 1)
|
|||
|
||||
from giambio import exceptions, socket, context, core, task, io
|
||||
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.util import debug
|
||||
|
||||
|
@ -34,6 +34,8 @@ __all__ = [
|
|||
"sleep",
|
||||
"Event",
|
||||
"Queue",
|
||||
"Channel",
|
||||
"NetworkChannel",
|
||||
"MemoryChannel",
|
||||
"run",
|
||||
"clock",
|
||||
|
|
|
@ -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
|
||||
limitations under the License.
|
||||
"""
|
||||
from abc import ABC, abstractmethod
|
||||
import abc
|
||||
from collections import deque
|
||||
from typing import Any, Optional
|
||||
from giambio.traps import event_wait, event_set
|
||||
|
@ -145,7 +147,60 @@ class Queue:
|
|||
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
|
||||
on giambio's queueing mechanism. Operations on this
|
||||
|
@ -158,16 +213,16 @@ class MemoryChannel:
|
|||
Public object constructor
|
||||
"""
|
||||
|
||||
super().__init__(maxsize)
|
||||
# We use a queue as our buffer
|
||||
self.buffer = Queue(maxsize=maxsize)
|
||||
self.maxsize = maxsize
|
||||
self.closed = False
|
||||
|
||||
|
||||
async def write(self, data: str):
|
||||
"""
|
||||
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:
|
||||
|
@ -186,7 +241,7 @@ class MemoryChannel:
|
|||
async def close(self):
|
||||
"""
|
||||
Closes the memory channel. Any underlying
|
||||
data is left for clients to read
|
||||
data is left for other tasks to read
|
||||
"""
|
||||
|
||||
self.closed = True
|
||||
|
@ -198,3 +253,10 @@ class MemoryChannel:
|
|||
"""
|
||||
|
||||
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
|
||||
"""
|
||||
|
|
|
@ -31,7 +31,7 @@ class BaseDebugger(ABC):
|
|||
loop starts executing
|
||||
"""
|
||||
|
||||
raise NotImplementedError
|
||||
return NotImplemented
|
||||
|
||||
@abstractmethod
|
||||
def on_exit(self):
|
||||
|
@ -40,7 +40,7 @@ class BaseDebugger(ABC):
|
|||
loop exits entirely (all tasks completed)
|
||||
"""
|
||||
|
||||
raise NotImplementedError
|
||||
return NotImplemented
|
||||
|
||||
@abstractmethod
|
||||
def on_task_schedule(self, task: Task, delay: float):
|
||||
|
@ -56,7 +56,7 @@ class BaseDebugger(ABC):
|
|||
:type delay: float
|
||||
"""
|
||||
|
||||
raise NotImplementedError
|
||||
return NotImplemented
|
||||
|
||||
@abstractmethod
|
||||
def on_task_spawn(self, task: Task):
|
||||
|
@ -69,7 +69,7 @@ class BaseDebugger(ABC):
|
|||
:type task: :class: giambio.objects.Task
|
||||
"""
|
||||
|
||||
raise NotImplementedError
|
||||
return NotImplemented
|
||||
|
||||
@abstractmethod
|
||||
def on_task_exit(self, task: Task):
|
||||
|
@ -81,7 +81,7 @@ class BaseDebugger(ABC):
|
|||
:type task: :class: giambio.objects.Task
|
||||
"""
|
||||
|
||||
raise NotImplementedError
|
||||
return NotImplemented
|
||||
|
||||
@abstractmethod
|
||||
def before_task_step(self, task: Task):
|
||||
|
@ -94,7 +94,7 @@ class BaseDebugger(ABC):
|
|||
:type task: :class: giambio.objects.Task
|
||||
"""
|
||||
|
||||
raise NotImplementedError
|
||||
return NotImplemented
|
||||
|
||||
@abstractmethod
|
||||
def after_task_step(self, task: Task):
|
||||
|
@ -107,7 +107,7 @@ class BaseDebugger(ABC):
|
|||
:type task: :class: giambio.objects.Task
|
||||
"""
|
||||
|
||||
raise NotImplementedError
|
||||
return NotImplemented
|
||||
|
||||
@abstractmethod
|
||||
def before_sleep(self, task: Task, seconds: float):
|
||||
|
@ -123,7 +123,7 @@ class BaseDebugger(ABC):
|
|||
:type seconds: int
|
||||
"""
|
||||
|
||||
raise NotImplementedError
|
||||
return NotImplemented
|
||||
|
||||
@abstractmethod
|
||||
def after_sleep(self, task: Task, seconds: float):
|
||||
|
@ -139,7 +139,7 @@ class BaseDebugger(ABC):
|
|||
:type seconds: float
|
||||
"""
|
||||
|
||||
raise NotImplementedError
|
||||
return NotImplemented
|
||||
|
||||
@abstractmethod
|
||||
def before_io(self, timeout: float):
|
||||
|
@ -153,7 +153,7 @@ class BaseDebugger(ABC):
|
|||
:type timeout: float
|
||||
"""
|
||||
|
||||
raise NotImplementedError
|
||||
return NotImplemented
|
||||
|
||||
@abstractmethod
|
||||
def after_io(self, timeout: float):
|
||||
|
@ -167,7 +167,7 @@ class BaseDebugger(ABC):
|
|||
:type timeout: float
|
||||
"""
|
||||
|
||||
raise NotImplementedError
|
||||
return NotImplemented
|
||||
|
||||
@abstractmethod
|
||||
def before_cancel(self, task: Task):
|
||||
|
@ -180,7 +180,7 @@ class BaseDebugger(ABC):
|
|||
:type task: :class: giambio.objects.Task
|
||||
"""
|
||||
|
||||
raise NotImplementedError
|
||||
return NotImplemented
|
||||
|
||||
@abstractmethod
|
||||
def after_cancel(self, task: Task) -> object:
|
||||
|
@ -193,7 +193,7 @@ class BaseDebugger(ABC):
|
|||
:type task: :class: giambio.objects.Task
|
||||
"""
|
||||
|
||||
raise NotImplementedError
|
||||
return NotImplemented
|
||||
|
||||
@abstractmethod
|
||||
def on_exception_raised(self, task: Task, exc: BaseException):
|
||||
|
@ -208,4 +208,4 @@ class BaseDebugger(ABC):
|
|||
:type exc: BaseException
|
||||
"""
|
||||
|
||||
raise NotImplementedError
|
||||
return NotImplemented
|
||||
|
|
Loading…
Reference in New Issue