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
|
2023-05-16 15:48:19 +02:00
|
|
|
from structio.core.syscalls import sleep, suspend as _suspend
|
2023-05-15 18:25:02 +02:00
|
|
|
from structio.core.context import TaskPool, TaskScope
|
2024-02-23 13:11:14 +01:00
|
|
|
from structio.exceptions import (
|
|
|
|
Cancelled,
|
|
|
|
TimedOut,
|
|
|
|
ResourceClosed,
|
|
|
|
ResourceBroken,
|
|
|
|
ResourceBusy,
|
|
|
|
WouldBlock,
|
|
|
|
)
|
2023-05-16 15:48:19 +02:00
|
|
|
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,
|
|
|
|
)
|
2023-05-18 18:20:50 +02:00
|
|
|
from structio.abc import Channel, Stream, ChannelReader, ChannelWriter
|
2023-06-06 11:31:30 +02:00
|
|
|
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,
|
|
|
|
)
|
2023-05-18 22:21:04 +02:00
|
|
|
from structio.core.run import current_loop, current_task
|
2023-06-15 16:39:08 +02:00
|
|
|
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
|
2024-03-10 19:35:00 +01:00
|
|
|
from structio.parallel import Process
|
|
|
|
from structio.pools import AsyncProcessPool
|
2023-06-19 19:10:00 +02:00
|
|
|
from structio import signals as _signals
|
2023-06-27 17:58:12 +02:00
|
|
|
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
|
2023-06-19 19:10:00 +02:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2023-08-20 12:38:07 +02:00
|
|
|
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()
|
|
|
|
|
|
|
|
|
2023-05-16 15:48:19 +02:00
|
|
|
async def _join(self: Task):
|
2023-06-19 16:33:58 +02:00
|
|
|
if self.done():
|
|
|
|
return self.result
|
2023-05-16 15:48:19 +02:00
|
|
|
await _suspend()
|
2023-06-28 14:16:01 +02:00
|
|
|
assert self.done()
|
2023-05-16 15:48:19 +02:00
|
|
|
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",
|
2023-06-28 14:33:50 +02:00
|
|
|
"util",
|
2023-09-04 18:35:27 +02:00
|
|
|
"ResourceBusy",
|
|
|
|
"ResourceBroken",
|
2024-02-23 13:11:14 +01:00
|
|
|
"WouldBlock",
|
2024-03-10 19:35:00 +01:00
|
|
|
"Process",
|
|
|
|
"AsyncProcessPool",
|
|
|
|
"AsyncSocket",
|
2023-05-22 09:22:37 +02:00
|
|
|
]
|