def activate(self): WebHandler.web_manager = self WebHandler.factory = self.factory WebHandler.player_manager = self.plugins.player_manager AsyncIOMainLoop().install() application.listen(8888)
def current(instance=True): """Returns the current thread's `IOLoop`. If an `IOLoop` is currently running or has been marked as current by `make_current`, returns that instance. If there is no current `IOLoop` and ``instance`` is true, creates one. .. versionchanged:: 4.1 Added ``instance`` argument to control the fallback to `IOLoop.instance()`. .. versionchanged:: 5.0 The ``instance`` argument now controls whether an `IOLoop` is created automatically when there is none, instead of whether we fall back to `IOLoop.instance()` (which is now an alias for this method) """ current = getattr(IOLoop._current, "instance", None) if current is None and instance: current = None if asyncio is not None: from tornado.platform.asyncio import AsyncIOLoop, AsyncIOMainLoop if IOLoop.configured_class() is AsyncIOLoop: current = AsyncIOMainLoop() if current is None: current = IOLoop() if IOLoop._current.instance is not current: raise RuntimeError("new IOLoop did not become current") return current
def current(instance=True): """Returns the current thread's `IOLoop`. If an `IOLoop` is currently running or has been marked as current by `make_current`, returns that instance. If there is no current `IOLoop` and ``instance`` is true, creates one. .. versionchanged:: 4.1 Added ``instance`` argument to control the fallback to `IOLoop.instance()`. .. versionchanged:: 5.0 On Python 3, control of the current `IOLoop` is delegated to `asyncio`, with this and other methods as pass-through accessors. The ``instance`` argument now controls whether an `IOLoop` is created automatically when there is none, instead of whether we fall back to `IOLoop.instance()` (which is now an alias for this method). ``instance=False`` is deprecated, since even if we do not create an `IOLoop`, this method may initialize the asyncio loop. """ try: loop = asyncio.get_event_loop() except (RuntimeError, AssertionError): if not instance: return None raise try: return IOLoop._ioloop_for_asyncio[loop] except KeyError: if instance: from tornado.platform.asyncio import AsyncIOMainLoop current = AsyncIOMainLoop(make_current=True) else: current = None return current
def connect_to_mongo(self): mongo_config = self.config.get('mongo', {}) database = mongo_config.get('database', 'helbot') AsyncIOMainLoop().install() io_loop = tornado.ioloop.IOLoop.instance() me.connection.connect(database, io_loop=io_loop)
def main(): import tornado.httpserver from tornado.options import define, options from tornado.platform.asyncio import AsyncIOMainLoop import asyncio AsyncIOMainLoop().install() define("port", default=config.DEFAULT_PORT, help="run on the given port", type=int) define("address", default='', help="run on the given address", type=str) define("datadir", default=config.DEFAULT_DATA_DIR, help="the directory to put uploaded data", type=str) define("fork", default=False, help="fork after startup", type=bool) define("cloudflare", default=config.CLOUDFLARE, help="check for Cloudflare IPs", type=bool) define("password", default=config.UPLOAD_PASSWORD, help="optional password", type=str) tornado.options.parse_command_line() if options.fork: if os.fork(): sys.exit() if options.cloudflare: import cloudflare cloudflare.install() loop = asyncio.get_event_loop() loop.create_task(cloudflare.updater()) application = tornado.web.Application( [ (r"/", IndexHandler), (r"/" + SCRIPT_PATH, ToolHandler), (r"/([a-fA-F0-9]{2}/[a-fA-F0-9]{38})(?:\.\w*)?", tornado.web.StaticFileHandler, { 'path': options.datadir, }), (r"/([a-fA-F0-9/]+(?:\.\w*)?)", HashHandler), ], datadir=options.datadir, debug=config.DEBUG, template_path=os.path.join(os.path.dirname(__file__), "templates"), password=config.UPLOAD_PASSWORD, ) http_server = tornado.httpserver.HTTPServer( application, xheaders=config.XHEADERS, ) http_server.listen(options.port, address=options.address) asyncio.get_event_loop().run_forever()
def start_service(service, infos): AsyncIOMainLoop().install() loop = tornado.ioloop.IOLoop.current() loop.spawn_callback(service.start) loop.spawn_callback(service.on_control_msg) loop.spawn_callback(service.heartbeat, infos) loop.start()
def start(self): self.sockets = tornado.netutil.bind_sockets( *self.naumanni_app.config.listen) children = self.fork(0) # こっからはMasterの世界 # use asyncio for ioloop AsyncIOMainLoop().install() self.children = [ ChildProc( proc, iostream.PipeIOStream(fdr), iostream.PipeIOStream(fdw), ) for proc, fdr, fdw in children ] # run self.naumanni_app.setup(None) synchronusly io_loop = ioloop.IOLoop.current() io_loop.run_sync(functools.partial(self.naumanni_app.setup, None)) # master run loop io_loop.start() for task_id, child in enumerate(self.children): child.proc.join()
def main(): tornado.options.parse_command_line() AsyncIOMainLoop().install() loop = asyncio.get_event_loop() app = Application() app.listen(options.port) loop.run_forever()
def main(): """ Tornado AsyncIO integration needs to fork processes before asyncio event loop gets initiated per process http://www.tornadoweb.org/en/stable/asyncio.html https://stackoverflow.com/questions/42767635 """ sockets = tornado.netutil.bind_sockets(8888) tornado.process.fork_processes(1) AsyncIOMainLoop().install() app = Application(handlers=[ (r"/", MainHandler), (r"/sleep", BackGroundSleepHandler), (r"/sleep2", AwaitedSleepHandler), ]) server = HTTPServer(app) server.add_sockets(sockets) print('Server Started') event_loop = asyncio.get_event_loop() try: event_loop.run_forever() except KeyboardInterrupt: print("Shutting down server") finally: event_loop.run_until_complete(event_loop.shutdown_asyncgens()) event_loop.close()
def main(): settings = config.server settings["static_path"] = config.static_path settings["template_path"] = config.template_path port = settings.pop("port") address = settings.pop("host") sockets = bind_sockets(port, address=address) if sys.platform != "win32": import uvloop asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) fork_processes(0 if config.env != "dev" else 1) AsyncIOMainLoop().install() loop = asyncio.get_event_loop() ThreadPool.init_thread_pool(workers=config.server["thread_worker"]) app = application.Application( settings=settings, loop=loop, model_routers=MODEL_ROUTERS, before_hooks=BEFORE_HOOKS, after_hooks=AFTER_HOOKS, delay_tasks=DELAY_TASKS, periodic_tasks=PERIODIC_TASKS, ) app.start_periodic_tasks() app.start_delay_tasks() server = httpserver.HTTPServer(app) server.add_sockets(sockets) logging.debug(f"🚀 Server ready at http://{address}:{port}") loop.run_forever()
def start() -> None: version_check.check_pyopenssl_version() AsyncIOMainLoop().install() loop = asyncio.get_event_loop() intercepts = Intercepts() proxy = Proxy(loop, UpdatesHandler.broadcast) web_options, server = configuration() master = Master(web_options, loop, proxy, server) web_application = Application(master, intercepts) http_server = tornado.httpserver.HTTPServer(web_application) http_server.listen(5000, "127.0.0.1") tornado_loop = tornado.ioloop.IOLoop.instance() tornado_loop.add_callback(master.start) tornado.ioloop.PeriodicCallback(lambda: master.tick(timeout=0), 5).start() try: print("Starting proxy server") tornado_loop.start() except (KeyboardInterrupt, RuntimeError): pass
async def serve_development_app( app: Application, host: str, port: int, started: asyncio.Event | None = None, ) -> None: enable_pretty_logging() # setup up tornado to use asyncio AsyncIOMainLoop().install() server = HTTPServer(app) server.listen(port, host) if started: # at this point the server is accepting connection started.set() try: # block forever - tornado has already set up its own background tasks await asyncio.get_event_loop().create_future() finally: # stop accepting new connections server.stop() # wait for existing connections to complete await server.close_all_connections()
def main(): AsyncIOMainLoop().install() loop = asyncio.get_event_loop() async_twitch = AsyncTwitchWrapper( loop, Twitch( config['RING_BUFFER_SIZE'] * 1024 * 1024, config['STREAM_RESOLUTION'], config['TWITCH_OAUTH'], config['TWITCH_CHANNEL'] ) ) async_twitch.initialize() asyncio.Task(pereodic_stream_updates(async_twitch, 10)) uploader = AsyncUploaderWrapper( loop, uploader=resolve_uploader() ) app = application(async_twitch, uploader) app.listen(config['PORT']) print('Listening port {port}...'.format(port=config['PORT'])) loop.run_forever()
def start_tornado(self): """ Starts a Tornado server with specific endpoints specified in the dictionary endpoints. Each endpoint should have a respective Handler Class implemented in tge tornado_endpoints module. To initialize the members of the Handler Classes you pass a dictionary after the Class Argument. :return: Tornado application that was created """ urls = [ ("/api/query", AnalyzeQuery, dict( handlers={"report": MessageManagerWebsocketFromServices.report_to_connections})), ("/api/csrf" , CSRF, dict( handlers={"report": MessageManagerWebsocketFromServices.report_to_connections})), ('/api/viralurls', ViralUrls,dict( handlers={"report": MessageManagerWebsocketFromServices.report_to_connections})), ('/api/intrusion' , IntrusionDetection, dict( handlers={"report": MessageManagerWebsocketFromServices.report_to_connections})) ] print("Started Tornado") AsyncIOMainLoop().install() # Allows to use Asyncio main event loop ; https://www.tornadoweb.org/en/branch4.5/asyncio.html app = Application(urls, debug=TORNADO_DEBUG) app.listen(TORNADO_PORT) IOLoop.current().start() return app
def run(self): # pragma: no cover AsyncIOMainLoop().install() iol = tornado.ioloop.IOLoop.instance() http_server = tornado.httpserver.HTTPServer(self.app) http_server.listen(self.options.web_port, self.options.web_iface) iol.add_callback(self.start) web_url = "http://{}:{}/".format(self.options.web_iface, self.options.web_port) self.add_log( "Web server listening at {}".format(web_url), "info" ) if self.options.web_open_browser: success = open_browser(web_url) if not success: self.add_log( "No web browser found. Please open a browser and point it to {}".format(web_url), "info" ) try: iol.start() except KeyboardInterrupt: self.shutdown()
def tornado_main(args): from tornado.platform.asyncio import AsyncIOMainLoop AsyncIOMainLoop().install() loop = tornado.ioloop.IOLoop.instance() loop.add_callback(lambda: run()) loop.start()
def __init__(self, protocol, port, **kwargs): self._initialize(**kwargs) if not issubclass(protocol, Protocol): raise TypeError(r'Dot Implemented Protocol Interface') self._settings = { r'ssl_options': kwargs.get(r'ssl_options', None), r'max_buffer_size': kwargs.get(r'max_buffer_size', None), r'read_chunk_size': kwargs.get(r'read_chunk_size', None), } self._sockets = bind_sockets(port) if self._process_num > 1: self._process_id = fork_processes(self._process_num) AsyncIOMainLoop().install() self._event_loop = asyncio.get_event_loop() self._server = _TCPServer(protocol, **self._settings) signal.signal(signal.SIGINT, self.stop) signal.signal(signal.SIGTERM, self.stop) if self._async_initialize: self._event_loop.run_until_complete(self._async_initialize())
def shutdown(): """Force server and ioloop shutdown.""" logging.info('Shutting down server') app.stop() AsyncIOMainLoop().stop() server.stop() _ioloop.stop()
def __init__(self): self._is_linux = (platform.system() == r'Linux') # 判定当前平台 self._process_id = 0 self._process_num = cpu_count() # cup数 self._sockets = bind_sockets(Config.Port) # 创建socket对象,绑定端口 if self._is_linux: # 如果是linux系统 self._process_id = fork_processes(self._process_num) # self._init_options() AsyncIOMainLoop().install() self._event_loop = asyncio.get_event_loop() self._server = HTTPServer(Application(**self._settings)) signal.signal(signal.SIGINT, self.stop) signal.signal(signal.SIGTERM, self.stop) from model.base import initialize as model_initialize IOLoop.instance().run_sync(model_initialize)
def run_app(args): from tornado.platform.asyncio import AsyncIOMainLoop AsyncIOMainLoop().install() signal.signal(signal.SIGINT, sig_exit) app = make_app(args) app.listen(8888) logging.info("Federator is now ready for requests") tornado.ioloop.IOLoop.current().start()
def __init__(self): self.db = DatabaseClient() self.connections = {} self.callback = PeriodicCallback(self.execute_withdraws, WITHDRAW_DELAY) self.callback.start() AsyncIOMainLoop().install() ioloop = IOLoop.current() ioloop.start()
def run_proc(port): AsyncIOMainLoop().install() app = tornado.web.Application([ (r"/cf/(?P<protocol>[^\/]+)/?(?P<proxy>[^\/]+)", CfHandler), ]) app.listen(port) logging.info(f'DestroyerIgnaleoAP@localhost:{port}') worker_loop.run_forever()
def main(): enable_pretty_logging() AsyncIOMainLoop().install() app = make_app() server = tornado.httpserver.HTTPServer(app) server.bind(1234, '127.0.0.1') server.start() asyncio.get_event_loop().run_forever()
def run_proc(port): AsyncIOMainLoop().install() app = tornado.web.Application([ (r'/', MainHandler), ]) app.listen(port) print('DestroyerIgnaleoG@localhost:%d' % (port)) worker_loop.run_forever()
def get_new_ioloop(self): try: import asyncio from tornado.platform.asyncio import AsyncIOMainLoop AsyncIOMainLoop().install() return IOLoop.current() except: return IOLoop.current()
def run(self): # pragma: no cover AsyncIOMainLoop().install() iol = tornado.ioloop.IOLoop.instance() http_server = tornado.httpserver.HTTPServer(self.app) http_server.listen(self.options.web_port, self.options.web_host) web_url = f"http://{self.options.web_host}:{self.options.web_port}/" self.log.info(f"Web server listening at {web_url}", ) self.run_loop(iol.start)
def install_asyncio() -> None: """Install tornado's loop to asyncio.""" try: from tornado.ioloop import IOLoop from tornado.platform.asyncio import AsyncIOMainLoop if not IOLoop.initialized(): AsyncIOMainLoop().install() except ImportError: pass
def run(self): # pragma: no cover AsyncIOMainLoop().install() iol = tornado.ioloop.IOLoop.instance() http_server = tornado.httpserver.HTTPServer(self.app) http_server.listen(self.options.web_port, self.options.web_iface) web_url = "http://{}:{}/".format(self.options.web_iface, self.options.web_port) self.log.info("Web server listening at {}".format(web_url), ) self.run_loop(iol.start)
def start(self): self.sockets = tornado.netutil.bind_sockets( *self.naumanni_app.config.listen) # debugなのでautoreloadする AsyncIOMainLoop().install() from tornado import autoreload autoreload.start() self._run_server(None)
def main(): tornado.options.parse_command_line() logging.info("Server started") # TODO(avezhenya) Add log file AsyncIOMainLoop().install() loop = asyncio.get_event_loop() app = Application() app.listen(options.port) PeriodicCallback(ChatSocketHandler.pinging, 50000).start() loop.run_forever()