def __init__(self): self._loop = None self._connections = set() self._error_handlers = [] self._request_extensions = {} self._log_request = None self._protocol_factory = Protocol self._debug = False self.request = Request() self.requests = None self.response = Response() self.router = router.Router() self.tasks = [] self.config = {} self.listeners = {"at_start": [], "at_end": []} self._mc = None self._mrq = None self.uses_session = False self.uses_mrq = False self.err404 = "<html><head><title>404 Not Found</title></head><body><h1>Not Found</h1><p>The requested page was not found</p></body></html>"
def expand_requests(self): for x in range(len(self.requests)): self.requests.append(Request())
def serve(self, *, sock, host, port, loop, run_async=False): faulthandler.enable() #pr = cProfile.Profile() #pr.enable() #cProfile.runctx('test(num)', globals(), locals(), 'prof%d.prof' %num) #sock.setsockopt(socket.SOL_SOCKET, socket.SO_OOBINLINE, 0) #TODO uvloop .9.1 sets this #profiler_start(b"mrhttp.log") if not loop: loop = self.loop asyncio.set_event_loop(loop) else: self._loop = loop self.requests = [Request() for x in range(128)] self.cinit() self.router.finalize_routes() self.router.setupRoutes() self._appStart() self.trigger_event("at_start") if self.uses_mrq: #mrqconf = self.config.get("mrq", None) #if not mrqconf: #print("When using MrQ app.config['mrq'] must be set. Exiting") #exit(1) srvs = self.config.get("mrq", None) if type(srvs) != list or len(srvs) == 0 or type(srvs[0]) != tuple: print( "When using MrQ app.config['mrq'] must be set to a list of (host,port) tuple pairs. Exiting" ) exit(1) self._mrq = MrqClient(srvs, self.loop) if self.uses_session: srvs = self.config.get("memcache", None) if type(srvs) != list or len(srvs) == 0 or type(srvs[0]) != tuple: print( "When using sessions app.config['memcache'] must be set to a list of (host,port) tuple pairs. Exiting" ) exit(1) self._mc = MemcachedClient(srvs, self.loop) loop.add_signal_handler(signal.SIGTERM, loop.stop) server_coro = loop.create_server(lambda: self._protocol_factory(self), sock=sock) if run_async: return server_coro # Try except here? server = loop.run_until_complete(server_coro) print('Accepting connections on http://{}:{}'.format(host, port)) try: loop.run_forever() except KeyboardInterrupt: pass finally: #loop.run_until_complete(loop.shutdown_asyncgens()) server.close() loop = asyncio.get_event_loop() loop.run_until_complete(server.wait_closed()) loop.run_until_complete(self.drain()) self.trigger_event("at_end") loop.close() for r in self.requests: r.cleanup() self.requests = None