From ce1583e9c2c4c4fae001c013c20f086dd4b8034c Mon Sep 17 00:00:00 2001 From: Mattia Giambirtone Date: Wed, 19 Oct 2022 19:52:04 +0200 Subject: [PATCH] Minor style changes --- aiosched/__init__.py | 2 +- aiosched/io.py | 30 +++++++++++++++++++++++++----- aiosched/kernel.py | 11 +++++++++-- aiosched/socket.py | 2 +- tests/context_wait.py | 2 +- tests/events.py | 2 +- tests/memory_channel.py | 4 +++- tests/network_channel.py | 4 +++- 8 files changed, 44 insertions(+), 13 deletions(-) diff --git a/aiosched/__init__.py b/aiosched/__init__.py index 5b7c833..d77430f 100644 --- a/aiosched/__init__.py +++ b/aiosched/__init__.py @@ -40,5 +40,5 @@ __all__ = [ "MemoryChannel", "checkpoint", "NetworkChannel", - "socket" + "socket", ] diff --git a/aiosched/io.py b/aiosched/io.py index 6b98c20..c3a1e74 100644 --- a/aiosched/io.py +++ b/aiosched/io.py @@ -22,7 +22,12 @@ import warnings import os import aiosched from aiosched.errors import ResourceClosed -from aiosched.internals.syscalls import wait_writable, wait_readable, io_release, closing +from aiosched.internals.syscalls import ( + wait_writable, + wait_readable, + io_release, + closing, +) try: @@ -42,7 +47,13 @@ class AsyncStream: is OS-dependent """ - def __init__(self, fd: int, open_fd: bool = True, close_on_context_exit: bool = True, **kwargs): + def __init__( + self, + fd: int, + open_fd: bool = True, + close_on_context_exit: bool = True, + **kwargs, + ): self._fd = fd self.stream = None if open_fd: @@ -126,7 +137,9 @@ class AsyncStream: os.set_blocking(self._fd, False) os.close(self._fd) except OSError as e: - warnings.warn(f"An exception occurred in __del__ for stream {self} -> {type(e).__name__}: {e}") + warnings.warn( + f"An exception occurred in __del__ for stream {self} -> {type(e).__name__}: {e}" + ) class AsyncSocket(AsyncStream): @@ -134,8 +147,15 @@ class AsyncSocket(AsyncStream): Abstraction layer for asynchronous sockets """ - def __init__(self, sock: socket.socket, close_on_context_exit: bool = True, do_handshake_on_connect: bool = True): - super().__init__(sock.fileno(), open_fd=False, close_on_context_exit=close_on_context_exit) + def __init__( + self, + sock: socket.socket, + close_on_context_exit: bool = True, + do_handshake_on_connect: bool = True, + ): + super().__init__( + sock.fileno(), open_fd=False, close_on_context_exit=close_on_context_exit + ) self.do_handshake_on_connect = do_handshake_on_connect self.stream = socket.fromfd(self._fd, sock.family, sock.type, sock.proto) self.stream.setblocking(False) diff --git a/aiosched/kernel.py b/aiosched/kernel.py index 0a09155..2862af7 100644 --- a/aiosched/kernel.py +++ b/aiosched/kernel.py @@ -337,7 +337,9 @@ class FIFOKernel: for each I/O resource the given task owns """ - for key in filter(lambda k: k.data == task, dict(self.selector.get_map()).values()): + for key in filter( + lambda k: k.data == task, dict(self.selector.get_map()).values() + ): self.notify_closing(key.fileobj, broken=True) self.selector.unregister(key.fileobj) task.last_io = () @@ -468,6 +470,7 @@ class FIFOKernel: self.data[self.current_task] = task self.run_ready.append(task) self.reschedule_running() + self.debugger.on_task_spawn(task) def set_context(self, ctx: TaskContext): """ @@ -566,4 +569,8 @@ class FIFOKernel: # but having two tasks reading/writing at the # same time can't lead to anything good, better # disallow it - self.current_task.throw(ResourceBusy(f"The resource is being read from/written by another task")) + self.current_task.throw( + ResourceBusy( + f"The resource is being read from/written by another task" + ) + ) diff --git a/aiosched/socket.py b/aiosched/socket.py index 721ee26..9a260f7 100644 --- a/aiosched/socket.py +++ b/aiosched/socket.py @@ -34,4 +34,4 @@ def socket(*args, **kwargs): constructor """ - return wrap_socket(_socket.socket(*args, **kwargs)) \ No newline at end of file + return wrap_socket(_socket.socket(*args, **kwargs)) diff --git a/tests/context_wait.py b/tests/context_wait.py index af7f93b..60ee16b 100644 --- a/tests/context_wait.py +++ b/tests/context_wait.py @@ -14,4 +14,4 @@ async def main(children: list[tuple[str, int]]): if __name__ == "__main__": - aiosched.run(main, [("first", 1), ("second", 2), ("third", 3)], debugger=None) + aiosched.run(main, [("first", 1), ("second", 2), ("third", 3)], debugger=Debugger()) diff --git a/tests/events.py b/tests/events.py index 90ec023..fa43c4a 100644 --- a/tests/events.py +++ b/tests/events.py @@ -7,7 +7,7 @@ async def child(ev: aiosched.Event, pause: int): start_total = aiosched.clock() await ev.wait() end_pause = aiosched.clock() - start_total - print(f"[child] Parent set the event with, exiting in {pause} seconds") + print(f"[child] Parent set the event, exiting in {pause} seconds") start_sleep = aiosched.clock() await aiosched.sleep(pause) end_sleep = aiosched.clock() - start_sleep diff --git a/tests/memory_channel.py b/tests/memory_channel.py index 5326d53..6202b0a 100644 --- a/tests/memory_channel.py +++ b/tests/memory_channel.py @@ -28,4 +28,6 @@ async def main(channel: aiosched.MemoryChannel, n: int): print("All done!") -aiosched.run(main, aiosched.MemoryChannel(2), 5, debugger=()) # 2 is the max size of the channel +aiosched.run( + main, aiosched.MemoryChannel(2), 5, debugger=() +) # 2 is the max size of the channel diff --git a/tests/network_channel.py b/tests/network_channel.py index 625bef7..6b7bea8 100644 --- a/tests/network_channel.py +++ b/tests/network_channel.py @@ -28,4 +28,6 @@ async def main(channel: aiosched.NetworkChannel, n: int): print("All done!") -aiosched.run(main, aiosched.NetworkChannel(), 5, debugger=()) # 2 is the max size of the channel +aiosched.run( + main, aiosched.NetworkChannel(), 5, debugger=() +) # 2 is the max size of the channel