structio/structio/__init__.py

169 lines
3.5 KiB
Python
Raw Normal View History

2023-05-15 18:25:02 +02:00
from structio.core import run as _run
from typing import Coroutine, Any, Callable
from structio.core.kernels.fifo import FIFOKernel
from structio.core.managers.io.simple import SimpleIOManager
from structio.core.managers.signals.sigint import SigIntManager
from structio.core.time.clock import DefaultClock
from structio.core.syscalls import sleep, suspend as _suspend
2023-05-15 18:25:02 +02:00
from structio.core.context import TaskPool, TaskScope
from structio.exceptions import Cancelled, TimedOut, ResourceClosed
from structio.core import task
from structio.core.task import Task, TaskState
2023-06-19 17:34:44 +02:00
from structio.sync import (
Event,
Queue,
MemoryChannel,
Semaphore,
Lock,
RLock,
emit,
on_event,
register_event,
)
from structio.abc import Channel, Stream, ChannelReader, ChannelWriter
from structio.io import socket
from structio.io.socket import AsyncSocket
2023-05-22 09:22:37 +02:00
from structio.io.files import (
open_file,
wrap_file,
aprint,
stdout,
stderr,
stdin,
ainput,
)
from structio.core.run import current_loop, current_task
from structio import thread, parallel
2023-05-22 09:22:37 +02:00
from structio.path import Path
2023-06-19 17:34:44 +02:00
from structio.signals import set_signal_handler, get_signal_handler
from structio import signals as _signals
from structio import util
2023-05-22 09:22:37 +02:00
def run(
func: Callable[[Any, Any], Coroutine[Any, Any, Any]],
*args,
restrict_ki_to_checkpoints: bool = False,
tools: list | None = None,
):
2023-07-09 18:41:18 +02:00
result = None
try:
result = _run.run(
func,
FIFOKernel,
SimpleIOManager(),
[SigIntManager()],
DefaultClock(),
tools,
restrict_ki_to_checkpoints,
*args,
)
finally:
# Bunch of cleanup
_signals._sig_handlers.clear()
_signals._sig_data.clear()
2023-05-15 18:25:02 +02:00
return result
run.__doc__ = _run.run.__doc__
2023-05-15 18:25:02 +02:00
def create_pool() -> TaskPool:
"""
Creates a new task pool
"""
return TaskPool()
def skip_after(timeout) -> TaskScope:
"""
Creates a new task scope with the
specified timeout. No error is raised
when the timeout expires
"""
2023-06-28 14:16:01 +02:00
return TaskScope(timeout=timeout, silent=True)
2023-05-15 18:25:02 +02:00
def with_timeout(timeout) -> TaskScope:
"""
Creates a new task scope with the
specified timeout. TimeoutError is raised
when the timeout expires
"""
2023-06-28 14:16:01 +02:00
return TaskScope(timeout=timeout)
2023-05-15 18:25:02 +02:00
def clock():
"""
Returns the current clock time of
the event loop
"""
return _run.current_loop().clock.current_time()
async def _join(self: Task):
if self.done():
return self.result
await _suspend()
2023-06-28 14:16:01 +02:00
assert self.done()
if self.state == TaskState.CRASHED:
raise self.exc
return self.result
def _cancel(self: Task):
_run.current_loop().cancel_task(self)
task._joiner = _join
_cancel.__name__ = Task.cancel.__name__
_cancel.__doc__ = Task.cancel.__doc__
Task.cancel = _cancel
2023-05-22 09:22:37 +02:00
__all__ = [
"run",
"sleep",
"create_pool",
"clock",
"Cancelled",
"skip_after",
"with_timeout",
"Event",
"Queue",
"MemoryChannel",
"Channel",
"Stream",
"ChannelReader",
"ChannelWriter",
"Semaphore",
"TimedOut",
"Task",
"TaskState",
"TaskScope",
"TaskPool",
"ResourceClosed",
"Lock",
"RLock",
"thread",
"open_file",
"wrap_file",
"aprint",
"stderr",
"stdin",
"stdout",
"ainput",
"current_loop",
"current_task",
"Path",
2023-06-19 17:34:44 +02:00
"parallel",
"get_signal_handler",
"set_signal_handler",
"util",
2023-05-22 09:22:37 +02:00
]