Skip to content

Instantly share code, notes, and snippets.

@kunigami
Created February 1, 2020 06:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kunigami/0ea27c0d37074d1623d8f5f74b029982 to your computer and use it in GitHub Desktop.
Save kunigami/0ea27c0d37074d1623d8f5f74b029982 to your computer and use it in GitHub Desktop.
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