Skip to content

Instantly share code, notes, and snippets.

@kunigami

kunigami/io_wait.py

Created Feb 1, 2020
Embed
What would you like to do?
class ReadWait(SystemCall):
def __init__(self, file):
self.file = file
def handle(self):
fd = self.file.fileno()
self.sched.waitforread(self.task, fd)
class Scheduler(object):
# Add task and file descriptor to the list
# to be checked on polling
def waitforread(self, task, fd):
self.read_waiting[fd] = task
# select.select is an OS API to determine
# when a file descriptor is "ready"
def iopoll(self,timeout):
if self.read_waiting or self.write_waiting:
read_fds, write_fds, _x = select.select(
self.read_waiting,
# ...
)
for fd in read_fds: self.schedule(self.read_waiting.pop(fd))
# ...
# Models polling as a regular task - whenever
# it gets run by the scheduler it counts as a
# poll
def iotask(self):
while True:
if self.ready.empty():
self.iopoll(None)
else:
self.iopoll(0)
yield
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.