Minor style changes
This commit is contained in:
parent
99656eedbe
commit
ce1583e9c2
|
@ -40,5 +40,5 @@ __all__ = [
|
||||||
"MemoryChannel",
|
"MemoryChannel",
|
||||||
"checkpoint",
|
"checkpoint",
|
||||||
"NetworkChannel",
|
"NetworkChannel",
|
||||||
"socket"
|
"socket",
|
||||||
]
|
]
|
||||||
|
|
|
@ -22,7 +22,12 @@ import warnings
|
||||||
import os
|
import os
|
||||||
import aiosched
|
import aiosched
|
||||||
from aiosched.errors import ResourceClosed
|
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:
|
try:
|
||||||
|
@ -42,7 +47,13 @@ class AsyncStream:
|
||||||
is OS-dependent
|
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._fd = fd
|
||||||
self.stream = None
|
self.stream = None
|
||||||
if open_fd:
|
if open_fd:
|
||||||
|
@ -126,7 +137,9 @@ class AsyncStream:
|
||||||
os.set_blocking(self._fd, False)
|
os.set_blocking(self._fd, False)
|
||||||
os.close(self._fd)
|
os.close(self._fd)
|
||||||
except OSError as e:
|
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):
|
class AsyncSocket(AsyncStream):
|
||||||
|
@ -134,8 +147,15 @@ class AsyncSocket(AsyncStream):
|
||||||
Abstraction layer for asynchronous sockets
|
Abstraction layer for asynchronous sockets
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, sock: socket.socket, close_on_context_exit: bool = True, do_handshake_on_connect: bool = True):
|
def __init__(
|
||||||
super().__init__(sock.fileno(), open_fd=False, close_on_context_exit=close_on_context_exit)
|
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.do_handshake_on_connect = do_handshake_on_connect
|
||||||
self.stream = socket.fromfd(self._fd, sock.family, sock.type, sock.proto)
|
self.stream = socket.fromfd(self._fd, sock.family, sock.type, sock.proto)
|
||||||
self.stream.setblocking(False)
|
self.stream.setblocking(False)
|
||||||
|
|
|
@ -337,7 +337,9 @@ class FIFOKernel:
|
||||||
for each I/O resource the given task owns
|
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.notify_closing(key.fileobj, broken=True)
|
||||||
self.selector.unregister(key.fileobj)
|
self.selector.unregister(key.fileobj)
|
||||||
task.last_io = ()
|
task.last_io = ()
|
||||||
|
@ -468,6 +470,7 @@ class FIFOKernel:
|
||||||
self.data[self.current_task] = task
|
self.data[self.current_task] = task
|
||||||
self.run_ready.append(task)
|
self.run_ready.append(task)
|
||||||
self.reschedule_running()
|
self.reschedule_running()
|
||||||
|
self.debugger.on_task_spawn(task)
|
||||||
|
|
||||||
def set_context(self, ctx: TaskContext):
|
def set_context(self, ctx: TaskContext):
|
||||||
"""
|
"""
|
||||||
|
@ -566,4 +569,8 @@ class FIFOKernel:
|
||||||
# but having two tasks reading/writing at the
|
# but having two tasks reading/writing at the
|
||||||
# same time can't lead to anything good, better
|
# same time can't lead to anything good, better
|
||||||
# disallow it
|
# 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"
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
|
@ -34,4 +34,4 @@ def socket(*args, **kwargs):
|
||||||
constructor
|
constructor
|
||||||
"""
|
"""
|
||||||
|
|
||||||
return wrap_socket(_socket.socket(*args, **kwargs))
|
return wrap_socket(_socket.socket(*args, **kwargs))
|
||||||
|
|
|
@ -14,4 +14,4 @@ async def main(children: list[tuple[str, int]]):
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
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())
|
||||||
|
|
|
@ -7,7 +7,7 @@ async def child(ev: aiosched.Event, pause: int):
|
||||||
start_total = aiosched.clock()
|
start_total = aiosched.clock()
|
||||||
await ev.wait()
|
await ev.wait()
|
||||||
end_pause = aiosched.clock() - start_total
|
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()
|
start_sleep = aiosched.clock()
|
||||||
await aiosched.sleep(pause)
|
await aiosched.sleep(pause)
|
||||||
end_sleep = aiosched.clock() - start_sleep
|
end_sleep = aiosched.clock() - start_sleep
|
||||||
|
|
|
@ -28,4 +28,6 @@ async def main(channel: aiosched.MemoryChannel, n: int):
|
||||||
print("All done!")
|
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
|
||||||
|
|
|
@ -28,4 +28,6 @@ async def main(channel: aiosched.NetworkChannel, n: int):
|
||||||
print("All done!")
|
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
|
||||||
|
|
Reference in New Issue