def run(): logger.info(f'Starting {name} %s!', __version__) app = build_app("spatialite://db.sqlite3") loop = asyncio.get_event_loop() aiomonitor.start_monitor(loop=loop, locals={"app": app}, monitor=Tap2GoMonitor) web.run_app(app)
async def _run(self) -> None: if isinstance(self.wsgi, Application): app = self.wsgi elif asyncio.iscoroutinefunction(self.wsgi): app = await self.wsgi() else: raise RuntimeError( "wsgi app should be either Application or " "async function returning Application, got {}".format( self.wsgi)) access_log = self.log.access_log if self.cfg.accesslog else None runner = web.AppRunner(app, logger=self.log, keepalive_timeout=self.cfg.keepalive, access_log=access_log, access_log_format=self._get_valid_log_format( self.cfg.access_log_format)) await runner.setup() aiomonitor.start_monitor(loop=self.loop, locals={"app": app}, monitor=Tap2GoMonitor) ctx = self._create_ssl_context(self.cfg) if self.cfg.is_ssl else None runner = runner server = runner.server for sock in self.sockets: site = web.SockSite(runner, sock, ssl_context=ctx, shutdown_timeout=self.cfg.graceful_timeout / 100 * 95) await site.start() # If our parent changed then we shut down. pid = os.getpid() try: while self.alive: # type: ignore self.notify() cnt = server.requests_count if self.cfg.max_requests and cnt > self.cfg.max_requests: self.alive = False self.log.info("Max requests, shutting down: %s", self) elif pid == os.getpid() and self.ppid != os.getppid(): self.alive = False self.log.info("Parent changed, shutting down: %s", self) else: await self._wait_next_notify() except BaseException: pass await runner.cleanup()
def run(options): """ starting point for a run """ t00 = time.time() # now # print("OPTIONS:\n", options) if options.shell_loop: exec_shell_loop(sys.argv[1:], options.loop_delay) return # just to make it more obvious. previous line never returns cfg = timon.config.get_config(options=options) use_trio = cfg.get_plugin_param( 'trio.enabled', default=False) if use_trio: print("load trio plugin") from timon.plugins.trio import run as run_trio return run_trio(options, cfg, run_once, t00, run_loop) if options.loop: # if we run in loop mode we must also see how to handle HUP signals # or force request for specific nodes via the so far not implemented # web API. use_aiomonitor = cfg.get_plugin_param( 'aiomonitor.enabled', default=False) print("With loop option (trio=%r, aiomon=%r)" % (use_trio, use_aiomonitor)) loop = asyncio.get_event_loop() if use_aiomonitor: import aiomonitor time.sleep(1) aiomonitor.start_monitor(loop=loop) # TODO: Read how exactly signal handlers are working # print("Will install signal handlers") # for signame in ('SIGINT', 'SIGTERM'): # loop.add_signal_handler( # getattr(signal, signame), # lambda: asyncio.ensure_future( # asyncio.gather(ask_exit(loop, signame)))) else: print("Without loop option") loop = asyncio.get_event_loop() dly, rslt_loop, notifiers = loop.run_until_complete( run_loop(options, cfg, run_once, t00=t00))
def test_ctor(loop, unused_port, console_enabled): with Monitor(loop, console_enabled=console_enabled): loop.run_until_complete(asyncio.sleep(0.01, loop=loop)) with start_monitor(loop, console_enabled=console_enabled) as m: loop.run_until_complete(asyncio.sleep(0.01, loop=loop)) assert m.closed m = Monitor(loop, console_enabled=console_enabled) m.start() try: loop.run_until_complete(asyncio.sleep(0.01, loop=loop)) finally: m.close() m.close() # make sure call is idempotent assert m.closed m = Monitor(loop, console_enabled=console_enabled) m.start() with m: loop.run_until_complete(asyncio.sleep(0.01, loop=loop)) assert m.closed # make sure that monitor inside async func can exit correctly async def f(): with Monitor(loop, console_enabled=console_enabled): await asyncio.sleep(0.01, loop=loop) loop.run_until_complete(f())
def start_server(config_context): # pragma: no coverage app = web.Application() event_propagation_router = EventPropagationRouter() app["config_context"] = config_context # get loop and db loop = asyncio.get_event_loop() # init supervisor from msa import core as msa_core from msa.core.supervisor import Supervisor supervisor = Supervisor() msa_core.supervisor_instance = supervisor app["supervisor"] = supervisor supervisor.init(loop, app["config_context"], route_adapter_instance) loop.run_until_complete(start_db_engine()) event_propagation_router.register_propagate_subscription(supervisor.event_bus) # register routes route_adapter_instance.register_app(app) app.add_routes(route_adapter_instance.get_route_table()) # startup hooks app.on_startup.append(event_propagation_router.app_start) app.on_startup.append(start_supervisor) # onshutdown app.on_shutdown.append(on_shutdown) # cleanup routes app.on_cleanup.append(stop_supervisor) app.on_cleanup.append(event_propagation_router.app_stop) app.on_cleanup.append(stop_db_engine) try: import aiomonitor host, port = "127.0.0.1", 8080 locals_ = {"port": port, "host": host} with aiomonitor.start_monitor(loop=loop, locals=locals_): # run application with built in aiohttp run_app function try: web.run_app(app, port=port, host=host) except Exception as e: print(e) finally: loop.close() except: try: web.run_app(app) except Exception as e: print(e) finally: loop.close()
def galacteekGui(args): progName = args.binaryname if args.binaryname else sys.argv[0] gc.enable() if inPyInstaller(): folder = pyInstallerBundleFolder() binPath = folder.joinpath('bin') os.environ['PATH'] = str(binPath) + os.pathsep + os.environ['PATH'] os.chdir(str(folder)) if args.mallocdebug: tracemalloc.start() if args.debug: faulthandler.enable() # QtWebEngine init QtWebEngine.initialize() # Initialize webengine schemes before creating the application initializeSchemes() gApp = application.GalacteekApplication( profile=args.profile, debug=args.debug, enableOrbital=args.enableorbital, sslverify=False if args.nosslverify else True, progName=progName, cmdArgs=args) level = 'DEBUG' if args.debug else 'INFO' if args.logstderr: glogger.basicConfig(level=level, colorized=args.logcolorized, loop=gApp.loop) else: glogger.basicConfig(outputFile=gApp.mainLogFileLocation, level=level, colorized=args.logcolorized, loop=gApp.loop) if not gApp.acquireLock(): gApp.onExit() return gApp.configure() # Monitor event loop if aiomonitor is available # Use the context manager so loop cleanup/close is automatic loop = gApp.loop if args.monitor is True and haveAiomonitor: with aiomonitor.start_monitor(loop=loop): with loop: loop.run_forever() else: with loop: loop.run_forever()
def source_cmd(server, token, monitor, log_to_journal, disk_cache_filename): if log_to_journal: try: from systemd import journal logger.handlers[0] = journal.JournaldLogHandler() except ImportError: logger.error( "Can't enable journal logger, systemd package not found!") src = BacnetSource(token=token, management_url=server, disk_cache_filename=disk_cache_filename) try: if monitor: with aiomonitor.start_monitor(src.event_loop, locals={"source": src}): src.run(cancel_on_exception=True) else: src.run(cancel_on_exception=True) except Exception as exception: logger.error( f"Source stopped by unhandled exception", exc_info=(exception.__class__, exception, exception.__traceback__), ) sys.exit(1)
def run(config, debug, no_cache): # noinspection PyArgumentList logging.basicConfig(handlers=[InterceptHandler()], level=0) logger.remove() logger.enable("discord_chan") logger.add(sys.stderr, level="INFO", filter="discord_chan") logger.add(sys.stderr, level="ERROR", filter="discord") config = ConfigBox(config_dict(config)) if not config.enviroment.bool("disable"): load_environ( **dict([var for var in config.enviroment.items() if var[0] != "disable"]) ) if debug: asyncio.get_event_loop().set_debug(True) logging.getLogger("asyncio").setLevel(logging.DEBUG) kwargs = {} if no_cache: kwargs["guild_subscriptions"] = False kwargs["fetch_offline_members"] = False bot = discord_chan.DiscordChan(config, **kwargs) # Todo: make sure to remove this debug call # bot.dispatch('ready') loop = asyncio.get_event_loop() with start_monitor( loop, monitor=discord_chan.DiscordChanMonitor, locals={"bot": bot} ): bot.run()
def run(self, arguments, settings, app): if arguments.monitor: if not HAS_AIOMONITOR: sys.stderr.write( 'You must install aiomonitor for the ' '--monitor option to work.\n' 'Use `pip install aiomonitor` to install aiomonitor.\n') return 1 # init monitor just before run_app loop = self.get_loop() with aiomonitor.start_monitor(loop=loop): self._run(arguments, settings, app) if arguments.reload: if not HAS_AUTORELOAD: sys.stderr.write( 'You must install aiohttp_autoreload for the --reload option to work.\n' 'Use `pip install aiohttp_autoreload` to install aiohttp_autoreload.\n' ) return 1 aiohttp_autoreload.start() self._run(arguments, settings, app) if self.profiler is not None: if arguments.profile_output: self.profiler.dump_stats(arguments.profile_output) else: # dump to screen self.profiler.print_stats(-1) if self.line_profiler is not None: self.line_profiler.disable_by_count() if arguments.line_profiler_output: self.line_profiler.dump_stats(arguments.line_profiler_output) else: self.line_profiler.print_stats()
def start_console(loop=None, locals=None): if loop is None: loop = asyncio.get_event_loop() def app(): try: loop.run_forever() except KeyboardInterrupt: print("Closing wizwalker; hooks should be rewritten") loop.run_until_complete(locals["walker"].close()) tasks = asyncio.Task.all_tasks(loop) for task in tasks: if not task.done(): task.cancel() loop.run_until_complete( asyncio.gather(*tasks, return_exceptions=True)) def run_monitor(): cli.monitor_client(cli.MONITOR_HOST, cli.MONITOR_PORT) monitor_thread = threading.Thread(target=run_monitor, daemon=True) monitor_thread.start() # monitor_proc = multiprocessing.Process(target=test_monitor, daemon=True) # loop.call_later(2, monitor_proc.start) with start_monitor(loop, monitor=WizWalkerConsole, locals=locals): app()
def start_aiomonitor(loop, args, locals): """Optionally create and start aiomonitor, depending on command-line arguments. The return value should be used as a context manager e.g.:: with start_aiomonitor(loop, args, locals=locals()): loop.run_until_complete(main()) If ``--aiomonitor`` is not passed on the command line, it does not start aiomonitor, although the package must still be present. Parameters ---------- loop : :class:`asyncio.AbstractEventLoop` Event loop to pass to the server args : :class:`argparse.Namespace` Command-line arguments, returned from a :class:`katsdpservices.ArgumentParser` created with ``aiomonitor=True``. locals : dict, optional Local variables, made available in aioconsole """ import aiomonitor if not args.aiomonitor: return _DummyContext() else: return aiomonitor.start_monitor( loop=loop, host=args.aiomonitor_host, port=args.aiomonitor_port, console_port=args.aioconsole_port, locals=locals)
def run(server, token): orig_sig_handler['interrupt'] = signal.getsignal(signal.SIGINT) orig_sig_handler['terminate'] = signal.getsignal(signal.SIGTERM) try: src = PduSource(token=token, management_url=server) with aiomonitor.start_monitor(src.event_loop, locals={'src': src}): src.run() # catch_signals=()) except KeyboardInterrupt: print('Keyboard interrupt, exiting process')
def main(): """ Functions that launches the asynchronous REPL. """ # Get the current event loop loop = asyncio.get_event_loop() # While the moonitor is running with aiomonitor.start_monitor(loop=loop): # Keep the loop working loop.run_forever()
def main(services, host, port, terminal_port, users): loop = asyncio.get_event_loop() loop.run_until_complete(init(host, port, services, users)) try: loc = dict(services=services) with aiomonitor.start_monitor(loop=loop, monitor=TerminalApp, port=terminal_port, locals=loc): logging.debug('Starting CALDERA at %s:%s' % (host, port)) loop.run_forever() except KeyboardInterrupt: pass
async def _add_monitor(self) -> Any: try: import aiomonitor except ImportError: self.log.warn('Cannot start console: aiomonitor is not installed') else: monitor = aiomonitor.start_monitor( port=self.console_port, loop=self.loop, ) self.add_context(monitor)
def start_servers(config): web_app = init_web(config) web_app.on_startup.append(on_startup) with aiomonitor.start_monitor(asyncio.get_event_loop(), port=config.monitor): try: run_web(web_app) except RuntimeError as exc: if not asyncio.get_event_loop().is_closed(): logging.error(f'遇到错误: {exc}, 程序终止')
def run(self, arguments, settings, app): port = settings.get('address', settings.get('port')) # init monitor just before run_app loop = asyncio.get_event_loop() monitor = settings.get('monitor', False) if monitor: with aiomonitor.start_monitor(loop=loop): web.run_app(app, host=settings.get('host', '0.0.0.0'), port=port) else: web.run_app(app, host=settings.get('host', '0.0.0.0'), port=port)
def main() -> None: print('running...') loop = asyncio.get_event_loop() task1 = loop.create_task(worker('one')) task2 = loop.create_task(worker('two')) loop.create_task(cancellor(task1)) with aiomonitor.start_monitor(loop=loop, locals={ 'loop': loop, 'task1': task1, 'task2': task2 }): loop.run_forever()
def main(loglevel=None): if loglevel is None: loglevel = get_loglevel() logging.basicConfig( format="%(asctime)s:%(levelname)s:%(name)s:%(message)s", level=loglevel) loop = asyncio.get_event_loop() loop.create_task(proxy_mail()) try: with aiomonitor.start_monitor(loop=loop): logging.info("Now you can connect with: nc localhost 50101") loop.run_forever() except KeyboardInterrupt: logging.info('Got KeyboardInterrupt')
def main(ctx, start_time, end_time, username, hostname, port, output_dir, password, camera_names, timezone, max_connections, dry_run, debug, verbose, quiet): """Download videos for cameras for a specific time frame. Times default to America/Denver.""" # Set up logging if debug: console_log_level = logging.DEBUG elif verbose: console_log_level = logging.INFO elif quiet: console_log_level = logging.ERROR else: console_log_level = logging.WARN # Base logger logger.setLevel(logging.DEBUG) # Create console handler with a higher log level ch = logging.StreamHandler() ch.setLevel(console_log_level) # Create log format formatter = logging.Formatter('%(name)s - %(levelname)s - %(message)s') # Set the format ch.setFormatter(formatter) # Add console handler to main logger logger.addHandler(ch) # Drop into an event loop loop = asyncio.get_event_loop() if debug: logger.debug( "Debug mode enabled. Use Netcat or Telnet to connect to 127.0.0.1 port 50101 for AIO internal information. This uses the library 'aiomonitor'." ) with aiomonitor.start_monitor(loop=loop): loop.run_until_complete( sub_main(ctx, start_time, end_time, username, hostname, port, output_dir, password, camera_names, timezone, max_connections, dry_run, logger, debug, loop)) else: loop.run_until_complete( sub_main(ctx, start_time, end_time, username, hostname, port, output_dir, password, camera_names, timezone, max_connections, dry_run, logger, debug, loop))
def run(self, arguments, settings, app): if arguments.monitor: if not HAS_AIOMONITOR: return print('You must install aiomonitor for the --monitor option to work' 'Use `pip install aiomonitor` to install aiomonitor.') # init monitor just before run_app loop = self.get_loop() with aiomonitor.start_monitor(loop=loop): self._run(arguments, settings, app) if arguments.reload: if not HAS_AUTORELOAD: return print('You must install aiohttp_autoreload for the --reload option to work' 'Use `pip install aiohttp_autoreload` to install aiohttp_autoreload.') aiohttp_autoreload.start() self._run(arguments, settings, app)
def app_factory(): """Application factory""" # Initialize logging config logging.config.dictConfig(settings.LOGGING) # Get loop loop = settings.loop # Set up monitoring tool with aiomonitor.start_monitor(loop=loop, locals={ "host": settings.APP_HOST, "port": settings.APP_PORT }): # Run application app = loop.run_until_complete(init_app(loop)) web.run_app(app, host=settings.APP_HOST, port=settings.APP_PORT)
def _run_complete(self, main_task): aio_main_task = main_task._aio_task try: if self.monitor: with aiomonitor.start_monitor(loop=self.loop): self.loop.run_until_complete(aio_main_task) else: self.loop.run_until_complete(aio_main_task) except BaseException: # 处理KeyboardInterrupt等异常,保证main_task完整运行结束 aio_main_task.cancel() if not aio_main_task.done(): try: self.loop.run_until_complete(aio_main_task) except asyncio.CancelledError: pass # ignore raise else: return aio_main_task.result()
def test_ctor(loop, unused_port): with Monitor(loop, console_enabled=False): loop.run_until_complete(asyncio.sleep(0.01, loop=loop)) with start_monitor(loop, console_enabled=False) as m: loop.run_until_complete(asyncio.sleep(0.01, loop=loop)) assert m.closed m = Monitor(loop, console_enabled=False) m.start() try: loop.run_until_complete(asyncio.sleep(0.01, loop=loop)) finally: m.close() m.close() # make sure call is idempotent assert m.closed m = Monitor(loop, console_enabled=False) m.start() with m: loop.run_until_complete(asyncio.sleep(0.01, loop=loop)) assert m.closed
async def hello(request): resp = web.StreamResponse() name = request.match_info.get('name', 'Anonymous') answer = ('Hello, ' + name).encode('utf8') resp.content_length = len(answer) resp.content_type = 'text/plain' await resp.prepare(request) await asyncio.sleep(100, loop=loop) resp.write(answer) await resp.write_eof() return resp async def init(loop): app = web.Application(loop=loop) app.router.add_get('/simple', simple) app.router.add_get('/hello/{name}', hello) app.router.add_get('/hello', hello) return app host, port = 'localhost', 8090 loop = uvloop.new_event_loop() asyncio.set_event_loop(loop) app = loop.run_until_complete(init(loop)) with aiomonitor.start_monitor(loop, locals=locals()): web.run_app(app, port=port, host=host)
# * Neither the name of metricq nor the names of its contributors # may be used to endorse or promote products derived from this software # without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. import asyncio import logging import aiomonitor import metricq if __name__ == "__main__": logging.basicConfig(level=logging.DEBUG) loop = asyncio.get_event_loop() c = metricq.Client("pytest", "amqps://localhost", event_loop=loop) loop.create_task(c.run()) with aiomonitor.start_monitor(loop, locals={"connection": c}): loop.run_forever()
# close websockets for user in app["users"].values(): if not user.bot: for ws in user.game_sockets.values(): await ws.close() for ws in app['websockets'].values(): await ws.close() app['websockets'].clear() app["client"].close() for task in set(app["tasks"]): task.cancel() if __name__ == "__main__": parser = argparse.ArgumentParser(description='PyChess chess variants server') parser.add_argument('-v', action='store_true', help='Verbose output. Changes log level from INFO to DEBUG.') parser.add_argument('-w', action='store_true', help='Less verbose output. Changes log level from INFO to WARNING.') args = parser.parse_args() logging.basicConfig() logging.getLogger().setLevel(level=logging.DEBUG if args.v else logging.WARNING if args.w else logging.INFO) loop = asyncio.get_event_loop() app = loop.run_until_complete(make_app(loop)) with aiomonitor.start_monitor(loop=loop, locals={"app": app}): web.run_app(app, port=os.environ.get("PORT", 8080))
def __run_with_monitor(self, app, settings): loop = self.get_loop() with aiomonitor.start_monitor(loop=loop): self.__run(app, settings)
# register methods for DirectCmd p2p.event.setup_events_from_class(DirectCmd) # throw cmd by `await p2p.send_direct_cmd(DirectCmd.what_is_your_name, 'kelly')` # or `await p2p.send_direct_cmd('what_is_your_name', 'kelly')` # You can setup broadcast policy (default disabled) # WARNING: You must set strict policy or will be broken by users async def broadcast_check_normal(user, data): return True # overwrite method p2p.broadcast_check = broadcast_check_normal # setup netcat monitor local = locals().copy() local.update({k: v for k, v in globals().items() if not k.startswith('__')}) log.info('local', list(local.keys())) aiomonitor.start_monitor(loop, port=3000, locals=local) log.info(f"you can connect by `nc 127.0.0.1 3000`") # start event loop # close by `close()` on netcat console try: loop.run_forever() except KeyboardInterrupt: log.info("closing") loop.close()
import asyncio import aiomonitor loop = asyncio.get_event_loop() with aiomonitor.start_monitor(loop=loop): print("Now you can connect with: nc localhost 50101") loop.run_forever()