def __init__(self, settings: Dict, host: str = '0.0.0.0', port: int = 9999): super().__init__(settings, host, port) self.client_manager = selectors.KqueueSelector() # The reason for this set is that every request is handled in its own thread but before # the socket is able to be read, control is yeilded to the main thread which picks which sockets are to be read. # Since the sub thread hasn't yet exhausted that client socket, the main thread thinks that socket needs to be serviced. # So this set prevents that by only servicing client sockets not currently in the set. self.clients_currently_being_serviced = set() self.clients_to_be_serviced = Queue()
def create_event_loop(self): return asyncio.SelectorEventLoop(selectors.KqueueSelector())
# date: 2018/10/6 # coroutines # - Future # - generator # - Task import socket import time try: import selectors except ImportError: import selectors2 as selectors selector = selectors.KqueueSelector() n_task = 0 class Future(object): def __init__(self): self.callbacks = [] def resolve(self): for cb in self.callbacks: cb() class Task(object): def __init__(self, gen): self.gen = gen