def handle(self, input, addresses, jobs, *args, **options): async def runner(): nonlocal lock lock = asyncio.Lock() tasks = [ asyncio.create_task(ping_worker(), name=f"ping-{i}") for i in range(min(jobs, len(addr_list))) ] await asyncio.gather(*tasks) async def ping_worker(): while True: async with lock: if not addr_list: break # Done addr = addr_list.pop(0) rtt, attempts = await ping.ping_check_rtt(addr, count=1, timeout=1000) if rtt: self.stdout.write(f"{addr} {rtt * 1000:.2f}ms\n") else: self.stdout.write(f"{addr} FAIL\n") # Run ping addr_list = self.get_addresses(addresses, input) lock: Optional[asyncio.Lock] = None ping = Ping() setup_asyncio() run_sync(runner)
def handle(self, input, addresses, jobs, *args, **options): self.addresses = set() # Direct addresses for a in addresses: if is_ipv4(a): self.addresses.add(a) # Read addresses from files if input: for fn in input: try: with open(fn) as f: for line in f: line = line.strip() if is_ipv4(line): self.addresses.add(line) except OSError as e: self.die("Cannot read file %s: %s\n" % (fn, e)) # Ping setup_asyncio() self.ping = Ping() self.jobs = jobs self.queue = tornado.queues.Queue(self.jobs) for i in range(self.jobs): IOLoop.current().spawn_callback(self.ping_worker) IOLoop.current().run_sync(self.ping_task)
def _runner(self): self.logger.debug("Starting DCS runner thread") setup_asyncio() self.loop = asyncio.get_event_loop() self.ready_event.set() self.loop.run_forever() self.logger.debug("Stopping DCS runner thread")
def get_service(): global _service if not _service: from noc.core.ioloop.util import setup_asyncio from .stub import ServiceStub setup_asyncio() _service = ServiceStub() _service.start() return _service
def start(self): """ Run main server loop """ self.startup_ts = perf_counter() parser = self.create_parser() self.add_arguments(parser) options = parser.parse_args(sys.argv[1:]) cmd_options = vars(options) cmd_options.pop("args", ()) # Bootstrap logging with --loglevel self.setup_logging(cmd_options["loglevel"]) self.log_separator() # Setup timezone try: self.logger.info("Setting timezone to %s", config.timezone) setup_timezone() except ValueError as e: self.die(str(e)) # Setup title self.set_proc_title() # Setup signal handlers self.setup_signal_handlers() self.on_start() # Starting IOLoop self.is_active = True if self.pooled: self.logger.warning("Running service %s (pool: %s)", self.name, config.pool) else: self.logger.warning("Running service %s", self.name) try: setup_asyncio() self.loop = asyncio.get_event_loop() # Initialize DCS self.dcs = get_dcs(cmd_options["dcs"]) # Activate service self.loop.create_task(self.activate()) self.logger.warning("Starting IOLoop") self.loop.run_forever() except KeyboardInterrupt: self.logger.warning("Interrupted by Ctrl+C") except self.RegistrationError: self.logger.info("Registration failed") except Exception: error_report() finally: if self.loop: self.loop.create_task(self.deactivate()) self.logger.warning("Service %s has been terminated", self.name)
def handle(self, cmd, *args, **options): setup_asyncio() return getattr(self, "handle_%s" % cmd.replace("-", "_"))(*args, **options)
def __init__(self, pool): self.config = self.ServiceConfig(pool=pool) setup_asyncio()