async def test_dispatch_event_cancel(self, source): """Test that dispatching an event when there are no listeners will still work.""" source.event_a.connect(lambda event: None) future = source.event_a.dispatch() future.cancel() task = next(t for t in all_tasks() if t is not current_task()) await task
def run_loop(self, loop): self.start() asyncio.ensure_future(self.running()) exc = None try: loop() except Exception: # pragma: no cover exc = traceback.format_exc() finally: if not self.should_exit.is_set(): # pragma: no cover self.shutdown() loop = asyncio.get_event_loop() tasks = asyncio.all_tasks(loop) if sys.version_info >= (3, 7) else asyncio.Task.all_tasks(loop) for p in tasks: p.cancel() loop.close() if exc: # pragma: no cover print(exc, file=sys.stderr) print("mitmproxy has crashed!", file=sys.stderr) print("Please lodge a bug report at:", file=sys.stderr) print("\thttps://github.com/mitmproxy/mitmproxy", file=sys.stderr) self.addons.trigger("done")
async def amain(*, args, prog): params = docopt.docopt( __doc__.replace("\t", " " * 4).format(prog=os.path.basename(prog)), argv=args, help=True, version=True, options_first=False ) url = params.pop("URL") assert not params, params log = asyncio.Queue() pending_tasks = [] printer_task = asyncio.create_task( printer(queue=log, fo=sys.stdout) ) git = Git(output_queue=log, git_path="git") out = await git.credential("fill", { "url": url, }) await log.put(out) await asyncio.gather(*pending_tasks) current_task, all_tasks = (asyncio.current_task(), asyncio.all_tasks()) assert {current_task, printer_task} == all_tasks, (current_task, all_tasks) await log.put(None) await printer_task
def show_current_tasks(self): ret = [] for task in asyncio.all_tasks(): name = task._coro.__name__ if not name in ('_run', 'process'): ret.append(task._coro.__name__) return ret
def _asynciostacks(*args, **kwargs): # pragma: no cover ''' A signal handler used to print asyncio task stacks and thread stacks. ''' print(80 * '*') print('Asyncio tasks stacks:') tasks = asyncio.all_tasks(_glob_loop) for task in tasks: task.print_stack() print(80 * '*') print('Faulthandler stack frames per thread:') faulthandler.dump_traceback() print(80 * '*')
async def __terminate(self): for service_name, service in self.get_services().items(): # We're using this instead of having no-op `terminate` # in base class to reduce number of awaits if hasattr(service, "terminate"): try: await asyncio.wait_for(service.terminate(), 10) except Exception: self.logger.error('Failed to terminate %s', service_name, exc_info=True) for task in asyncio.all_tasks(loop=self.__loop): task.cancel() self.__loop.stop()
def tearDown(self) -> None: # Native coroutines tend to produce warnings if they're not # allowed to run to completion. It's difficult to ensure that # this always happens in tests, so cancel any tasks that are # still pending by the time we get here. asyncio_loop = self.io_loop.asyncio_loop # type: ignore if hasattr(asyncio, "all_tasks"): # py37 tasks = asyncio.all_tasks(asyncio_loop) # type: ignore else: tasks = asyncio.Task.all_tasks(asyncio_loop) # Tasks that are done may still appear here and may contain # non-cancellation exceptions, so filter them out. tasks = [t for t in tasks if not t.done()] for t in tasks: t.cancel() # Allow the tasks to run and finalize themselves (which means # raising a CancelledError inside the coroutine). This may # just transform the "task was destroyed but it is pending" # warning into a "uncaught CancelledError" warning, but # catching CancelledErrors in coroutines that may leak is # simpler than ensuring that no coroutines leak. if tasks: done, pending = self.io_loop.run_sync(lambda: asyncio.wait(tasks)) assert not pending # If any task failed with anything but a CancelledError, raise it. for f in done: try: f.result() except asyncio.CancelledError: pass # Clean up Subprocess, so it can be used again with a new ioloop. Subprocess.uninitialize() self.io_loop.clear_current() if not isinstance(self.io_loop, _NON_OWNED_IOLOOPS): # Try to clean up any file descriptors left open in the ioloop. # This avoids leaks, especially when tests are run repeatedly # in the same process with autoreload (because curl does not # set FD_CLOEXEC on its file descriptors) self.io_loop.close(all_fds=True) super(AsyncTestCase, self).tearDown() # In case an exception escaped or the StackContext caught an exception # when there wasn't a wait() to re-raise it, do so here. # This is our last chance to raise an exception in a way that the # unittest machinery understands. self.__rethrow()
def all_tasks(): """schedule the coroutine to run""" try: # Python <3.7 has no the method return asyncio.all_tasks() except AttributeError: return asyncio.Task.all_tasks()
async def shutdown(s, loop): if "connection" in state: await state["connection"].close() for task in [t for t in asyncio.all_tasks() if t is not asyncio.current_task()]: task.cancel() loop.stop()
async def process_signal(signum, loop): tasks = [t for t in asyncio.all_tasks() if t is not asyncio.current_task()] for task in tasks: task.cancel()
loop.create_task(plotting(connection, t), name="Task2") ############# # App Main ############# read_characteristic = "0000ffe1-0000-1000-8000-00805f9b34fb" write_characteristic = "0000ffe1-0000-1000-8000-00805f9b34fb" if __name__ == "__main__": nest_asyncio.apply() # Create the event loop. loop = asyncio.get_event_loop() loop.set_debug(True) connection = Connection(loop, read_characteristic, write_characteristic) plt.style.use('seaborn') try: Task1 = loop.create_task(main(loop, connection), name="Task1") loop.run_forever() except KeyboardInterrupt: print("User stopped loop 2.") for task in asyncio.all_tasks(): task.cancel() print("User stopped program.") finally: print("Disconnecting...") loop.run_until_complete(connection.cleanup()) loop.stop()
async def test_reconnect(mock_session, unused_tcp_port): got_update = asyncio.Event() def dev_data_cb(data): _LOGGER.debug(f"Received dev_data callback {data}") def update_cb(data): _LOGGER.debug(f"Received update callback {data}") got_update.set() test_connected = asyncio.Event() mock_server = MockServer(unused_tcp_port, connect_event=test_connected) await mock_server.initialise() mock_session._api_host = f"http://localhost:{unused_tcp_port}" socket_session = SocketSession(mock_session, _MOCK_DEV_ID, dev_data_cb, update_cb, add_sigint_handler=True, ping_interval=_TEST_PING_INTERVAL) client_task = asyncio.create_task(socket_session.run()) await test_connected.wait() test_connected.clear() # check we connected with the right access_token assert f"token={_MOCK_ACCESS_TOKEN}" in mock_server.query_string.split('&') await got_update.wait() got_update.clear() # force a reconnect _LOGGER.debug("Forcing reconnect") await mock_server.disconnect_client() _LOGGER.debug("Stopping site") await mock_server.site.stop() # change the access token mock_session._access_token = _MOCK_ACCESS_TOKEN_2 _LOGGER.debug("Restarting site") await mock_server.site.start() # should now reconnect await test_connected.wait() # check we connected with the new access_token assert f"token={_MOCK_ACCESS_TOKEN_2}" in mock_server.query_string.split( '&') # should get another update await got_update.wait() _LOGGER.info("Stopping client") await socket_session.cancel() await client_task await mock_server.cleanup() # engineio leaves a service task around _LOGGER.info("Cancelling remaining tasks") for task in asyncio.all_tasks(): if task != asyncio.current_task(): _LOGGER.info(f"Task: {task}") _LOGGER.info(task.cancel())
async def shut_down(): taskList= asyncio.all_tasks(asyncio.get_running_loop()) for task in taskList: task.cancel()
async def main(): forks = [False] * 5 philosophers = ['Aristotle', 'Confucius', 'Descartes', 'Foucault', 'Kant'] for number, name in enumerate(philosophers): asyncio.create_task(start(name, number, forks)) await asyncio.gather(*asyncio.all_tasks())
def _shutdown() -> None: _LOG.info(_LOG_STR, 'received stop signal, cancelling tasks ...') for task in asyncio.all_tasks(): task.cancel() _LOG.info(_LOG_STR, 'all tasks cancelled !')
def get(self): # write custom metrics to MetricsAPI in the future request_queue_length = len(asyncio.all_tasks()) logger.info(f"Request count: {request_queue_length}") self.write(request_queue_length)
async def _get_word_info_and_dispose(self, list_id: str, word: str, is_second: bool = False): asyncio.current_task().set_name(f"{list_id}_{word}") try: answer = await self.get_detail_by_word(self.id, list_id, word) except NoPermission: _logger.i(f"[{self.id}/{list_id}]疑似vip课程章节,无权访问,跳过该章节答案加载,本次不会缓存本地词库") self.is_success = False for task in asyncio.all_tasks(): name = task.get_name() if name.find(list_id) != -1: task.cancel() pass return except CancelledError as e: pass except Exception as e: print(e) if not is_second: _logger.w(f"警告!单词:{word}({self.id}/{list_id})加载失败,稍后软件将会尝试二次加载") else: _logger.w(f"警告!单词:{word}({self.id}/{list_id})二次加载失败,本次不会缓存本地词库") self.is_success = False if self._fail_list.get(list_id) is None: self._fail_list[list_id] = [word] else: self._fail_list[list_id].append(word) return else: if is_second: _logger.d(f"单词:{word}二次加载成功!") if self.data.get(word) is None: self.data[word] = answer else: # 废弃代码self.data[word]["content"].extend(answer["content"]) # 存在单词翻译相同情况,该BUG由群友104***748提供,若不处理,会让题型32的特殊情况出现问题 tem_map = {} for index, item in enumerate(self.data[word]["content"]): tem_map[item["mean"]] = index for item in answer["content"]: mean = tem_map.get(item["mean"]) if mean is None: self.data[word]["content"].append(item) else: tem_data = self.data[word]["content"][mean] for key in item["usage"]: if tem_data["usage"].get(key) is None: tem_data["usage"][key] = item["usage"][key] else: tem_list = [] for item_usage in item["usage"][key]: has_repetition = False item_usage_set = Set(item_usage) for usage in tem_data["usage"][key]: if len(Set(usage) & item_usage_set) == len(usage): has_repetition = True break if not has_repetition: tem_list.append(item_usage) tem_data["usage"][key].extend(tem_list) for key in item["example"]: if tem_data["example"].get(key) is None: tem_data["example"][key] = item["example"][key] del tem_map self.data[word]["assist"].extend(answer["assist"]) self.data[word]["assist"] = list(set(self.data[word]["assist"]))
def cancel_ccxt_throttle_task(): for task in asyncio.all_tasks(): # manually cancel ccxt async throttle task since it apparently can't be cancelled otherwise if str(task._coro).startswith("<coroutine object Throttler.looper at"): task.cancel()
async def get_running_tasks() -> List[TaskInfo]: return [_create_task_info(task) for task in all_tasks() if not task.done()]
def signal_handler(signal, frame): logging.warning('Interrupción de teclado. Finalizando.') asyncio.gather(*asyncio.all_tasks()).cancel() sys.exit(0)
import asyncio import signal from functools import partial import sys from simpervisor import atexitasync def _handle_sigterm(number, received_signum): # Print the received signum so we know our handler was called print("handler {} received".format(number), int(received_signum), flush=True) handlercount = int(sys.argv[1]) for i in range(handlercount): atexitasync.add_handler(partial(_handle_sigterm, i)) loop = asyncio.get_event_loop() try: loop.run_forever() finally: # Cleanup properly so we get a clean exit try: remaining_tasks = asyncio.all_tasks(loop=loop) except AttributeError: # asyncio.all_tasks was added in 3. Provides reverse compatability. remaining_tasks = asyncio.Task.all_tasks(loop=loop) loop.run_until_complete(asyncio.gather(*remaining_tasks)) loop.close()
def get_loop_info() -> str: stream = StringIO() for n, task in enumerate(asyncio.all_tasks()): prefix = "*" if task == asyncio.current_task() else " " print(f"{prefix}{n+1:2d}) {task}", file=stream) return stream.getvalue()
def handle_disconnect(_: BleakClient): print("Device was disconnected, goodbye.") # cancelling all tasks effectively ends the program for task in asyncio.all_tasks(): task.cancel()
await asyncio.sleep(1) except asyncio.CancelledError: for i in range(3): print("<Your app is shutting down...>") await asyncio.sleep(1) def handler(sig): loop.stop() print(f"Got signal: {sig!s}, shutting down.") loop.remove_signal_handler(SIGTERM) loop.add_signal_handler(SIGINT, lambda: None) if __name__ == "__main__": loop = asyncio.get_event_loop() for sig in (SIGTERM, SIGINT): loop.add_signal_handler(sig, handler, sig) loop.create_task(main()) loop.run_forever() tasks = asyncio.all_tasks(loop=loop) for t in tasks: t.cancel() group = asyncio.gather(*tasks, return_exceptions=True) loop.run_until_complete(group) loop.close()
async def runner() -> None: log(f'=== Starting up {self.name} ===', Ansi.LMAGENTA) loop = asyncio.get_running_loop() # Call our before_serving coroutine, # if theres one specified. if self.before_serving: await self.before_serving() # Start pending coroutine tasks. if self.debug: log(f'-> Starting {len(self._task_coros)} tasks.', Ansi.LMAGENTA) for coro in self._task_coros: task = loop.create_task(coro) task.add_done_callback(self._default_cb) # XXX: never removed? self.tasks.add(task) self._task_coros.clear() # Setup socket & begin listening if self.using_unix_socket: if os.path.exists(addr): os.remove(addr) # read/write signal listening socks sig_rsock, sig_wsock = os.pipe() os.set_blocking(sig_wsock, False) signal.set_wakeup_fd(sig_wsock) # connection listening sock lsock = socket.socket(self.sock_family) lsock.setblocking(False) lsock.bind(addr) if self.using_unix_socket: os.chmod(addr, 0o777) lsock.listen(self.max_conns) log(f'-> Listening @ {addr}', AnsiRGB(0x00ff7f)) # TODO: terminal input support (tty, termios fuckery) # though, tbh this should be moved into gulag as it's # mostly a gulag-specific thing, and it'll be easier # to manage all the printing stuff that way. should_close = False should_restart = False while True: await asyncio.sleep(0.01) # skip loop iteration rlist, _, _ = select.select([lsock, sig_rsock], [], [], 0) for reader in rlist: if reader is lsock: # new connection received for server client, _ = await loop.sock_accept(lsock) task = loop.create_task(self.handle(client)) task.add_done_callback(self._default_cb) elif reader is sig_rsock: # received a blocked signal, shutdown sig_received = signal.Signals(os.read(sig_rsock, 1)[0]) if sig_received is signal.SIGINT: print('\x1b[2K', end='\r') # clear ^C from console elif sig_received is signal.SIGUSR1: should_restart = True log(f'Received {signal.strsignal(sig_received)}', Ansi.LRED) should_close = True else: raise RuntimeError(f'Unknown reader {reader}') if should_close: break # server closed, clean things up. for sock_fd in {lsock.fileno(), sig_rsock, sig_wsock}: os.close(sock_fd) signal.set_wakeup_fd(-1) if self.using_unix_socket: os.remove(addr) log('-> Cancelling tasks', Ansi.LMAGENTA) for task in self.tasks: task.cancel() await asyncio.gather(*self.tasks, return_exceptions=True) if in_progress := [ t for t in asyncio.all_tasks() if t is not asyncio.current_task() ]: try: # allow up to 5 seconds for in-progress handlers # to finish their execution, just incase they're # in a half-complete state. we wouldn't want to # get any sql tables into a weird state, or alike. log( f'-> Awaiting {len(in_progress)} ' 'in-progress handler(s).', Ansi.LMAGENTA) await asyncio.wait(in_progress, loop=loop, timeout=5.0) except asyncio.TimeoutError: log('-> Timed out awaiting handlers, cancelling them.', Ansi.LMAGENTA) to_await = [] for task in in_progress: if not task.cancelled(): task.cancel() to_await.append(task) await asyncio.gather(*to_await, return_exceptions=True)
async def test_workflow( postgres_db: sa.engine.Engine, docker_registry: str, simcore_services, fake_project_data, catalog_subsystem_mock, client, logged_user, primary_group: Dict[str, str], standard_groups: List[Dict[str, str]], storage_subsystem_mock, director_v2_service_mock, ): # empty list projects = await _request_list(client) assert not projects # creation await _request_create(client, fake_project_data) catalog_subsystem_mock([fake_project_data]) # list not empty projects = await _request_list(client) assert len(projects) == 1 assert not ProjectState(**projects[0].pop("state")).locked.value for key in projects[0].keys(): if key not in ( "uuid", "prjOwner", "creationDate", "lastChangeDate", "accessRights", ): assert projects[0][key] == fake_project_data[key] assert projects[0]["prjOwner"] == logged_user["email"] assert projects[0]["accessRights"] == { str(primary_group["gid"]): { "read": True, "write": True, "delete": True } } modified_project = deepcopy(projects[0]) modified_project["name"] = "some other name" modified_project["description"] = "John Raynor killed Kerrigan" new_node_id = str(uuid4()) modified_project["workbench"][new_node_id] = modified_project[ "workbench"].pop(list(modified_project["workbench"].keys())[0]) modified_project["workbench"][new_node_id]["position"]["x"] = 0 # share with some group modified_project["accessRights"].update({ str(standard_groups[0]["gid"]): { "read": True, "write": True, "delete": False } }) # modify pid = modified_project["uuid"] await _request_update(client, modified_project, pid) # list not empty projects = await _request_list(client) assert len(projects) == 1 for key in projects[0].keys(): if key not in ("lastChangeDate", "state"): assert projects[0][key] == modified_project[key] # get project = await _request_get(client, pid) for key in project.keys(): if key not in ("lastChangeDate", "state"): assert project[key] == modified_project[key] # delete await _request_delete(client, pid) # wait for delete tasks to finish tasks = asyncio.all_tasks() for task in tasks: # TODO: 'async_generator_asend' has no __name__ attr. Python 3.8 gets coros names # Expects "delete_project" coros to have __name__ attrs # pylint: disable=protected-access if "delete_project" in getattr(task.get_coro(), "__name__", ""): await asyncio.wait_for(task, timeout=60.0) # list empty projects = await _request_list(client) assert not projects
async def func(): return asyncio.all_tasks()
def _all_tasks() -> Iterable[asyncio.Task]: return asyncio.all_tasks()
def _run(self, future): loop = asyncio.get_event_loop() loop.run_until_complete(future) loop.run_until_complete(asyncio.gather(*asyncio.all_tasks(loop)))
async def listen(queue): from halfpipe.logging import setup as setuplogging setuplogging(queue) loop = get_running_loop() printWriter = PrintWriter(levelno=25) # fmriprep's IMPORTANT logWriter = FileWriter(levelno=logging.DEBUG) errWriter = FileWriter(levelno=logging.WARNING) writers = [printWriter, logWriter, errWriter] [loop.create_task(writer.start()) for writer in writers] subscribers = [writer.queue for writer in writers] while True: message = await loop.run_in_executor(None, queue.get) # from pprint import pprint # pprint(schema.dump(message)) if isinstance(message, Message): if len(schema.validate(message)) > 0: continue # ignore invalid else: try: message = schema.load(message) except ValidationError: continue # ignore invalid if message.type == "log": for subscriber in subscribers: await subscriber.put(message) elif message.type == "set_workdir": workdir = message.workdir if not isinstance(workdir, Path): workdir = Path(workdir) workdir.mkdir(exist_ok=True, parents=True) logWriter.filename = workdir / "log.txt" logWriter.canWrite.set() errWriter.filename = workdir / "err.txt" errWriter.canWrite.set() elif message.type == "enable_verbose": printWriter.levelno = logging.DEBUG elif message.type == "enable_print": printWriter.canWrite.set() elif message.type == "disable_print": printWriter.canWrite.clear() elif message.type == "teardown": # make sure that all writers have finished writing await gather(*[subscriber.join() for subscriber in subscribers]) # then cancel all tasks tasks = [t for t in all_tasks() if t is not current_task()] [task.cancel() for task in tasks] await gather(*tasks) loop.stop() break queue.task_done()
def cancel_tasks(): print('Got a SIGINT!') tasks: Set[asyncio.Task] = asyncio.all_tasks() print(f'Cancelling {len(tasks)} task(s).') [task.cancel() for task in tasks]
if sys.platform.startswith("win"): loop = (asyncio.ProactorEventLoop() ) # subprocess pipes only work with this under Win asyncio.set_event_loop(loop) else: loop = asyncio.get_event_loop() loop.create_task(Main().launch()) try: loop.run_forever() except KeyboardInterrupt: def shutdown_handler(_loop, context): if "exception" not in context or not isinstance( context["exception"], asyncio.CancelledError): _loop.default_exception_handler(context) # TODO: fix context loop.set_exception_handler(shutdown_handler) tasks = asyncio.gather(*asyncio.all_tasks(loop=loop), loop=loop, return_exceptions=True) tasks.add_done_callback(lambda t: loop.stop()) tasks.cancel() while not tasks.done() and not loop.is_closed(): loop.run_forever() finally: if hasattr(loop, "shutdown_asyncgens"): loop.run_until_complete(loop.shutdown_asyncgens()) loop.close()
async def gen_main(cmd_input: Optional[List[str]] = None, ) -> int: # Make sure all files are created with global rw permissions os.umask(0o000) # Setup parser parser = argparse.ArgumentParser( description= "idb: a versatile tool to communicate with iOS Simulators and Devices", epilog="See Also: https://www.fbidb.io/docs/guided-tour", formatter_class=argparse.RawTextHelpFormatter, ) parser.add_argument( "--log", dest="log_level", choices=["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"], default="WARNING", help="Set the logging level", ) parser.add_argument( "--companion", type=str, default=os.environ.get("IDB_COMPANION"), help= "A string of the form HOSTNAME:PORT that will describe the companion connect to." "Can also be set with the IDB_COMPANION environment variable", ) parser.add_argument( "--companion-path", type=str, default="/usr/local/bin/idb_companion" if sys.platform == "darwin" else None, help= "The path to the idb companion binary. This is only valid when running on macOS platforms", ) commands: List[Command] = [ AppInstallCommand(), AppUninstallCommand(), AppListCommand(), LaunchCommand(), AppTerminateCommand(), CommandGroup( name="xctest", description="Operations with xctest on target", commands=[ XctestInstallCommand(), XctestsListBundlesCommand(), XctestListTestsCommand(), XctestRunCommand(), ], ), CommandGroup( name="file", description="File operations on target", commands=[ FSMoveCommand(), FSPullCommand(), FSPushCommand(), FSMkdirCommand(), FSRemoveCommand(), FSListCommand(), ], ), CommandGroup( name="contacts", description="Contacts database operations on target", commands=[ContactsUpdateCommand()], ), LogCommand(), CommandGroup( name="record", description="Record what the screen is doing", commands=[RecordVideoCommand()], ), RecordVideoCommand(), DeprecatedPushCommand(), DeprecatedPullCommand(), UrlOpenCommand(), KeychainClearCommand(), LocationSetCommand(), ApproveCommand(), TargetConnectCommand(), TargetDisconnectCommand(), TargetListCommand(), TargetDescribeCommand(), TargetCreateCommand(), TargetBootCommand(), TargetShutdownCommand(), TargetEraseCommand(), TargetDeleteCommand(), TargetDeleteAllCommand(), DaemonCommand(), ScreenshotCommand(), CommandGroup( name="ui", description="UI interactions on target", commands=[ AccessibilityInfoAllCommand(), AccessibilityInfoAtPointCommand(), TapCommand(), ButtonCommand(), TextCommand(), KeyCommand(), KeySequenceCommand(), SwipeCommand(), ], ), CommandGroup( name="crash", description="Operations on crashes", commands=[ CrashListCommand(), CrashShowCommand(), CrashDeleteCommand() ], ), InstrumentsCommand(), KillCommand(), MediaAddCommand(), FocusCommand(), CommandGroup( name="debugserver", description="debugserver interactions", commands=[ DebugServerStartCommand(), DebugServerStopCommand(), DebugServerStatusCommand(), ], ), CommandGroup(name="dsym", description="dsym commands", commands=[DsymInstallCommand()]), CommandGroup(name="dylib", description="dylib commands", commands=[DylibInstallCommand()]), CommandGroup( name="framework", description="framework commands", commands=[FrameworkInstallCommand()], ), CommandGroup( name="companion", description="commands related to the companion", commands=[CompanionLogCommand()], ), ] commands.extend(plugin.get_commands()) root_command = CommandGroup( name="root_command", description="", commands=sorted(commands, key=lambda command: command.name), ) root_command.add_parser_arguments(parser) # Parse input and run cmd_input = cmd_input or sys.argv[1:] try: args = parser.parse_args(cmd_input) plugin.on_launch(logger) await root_command.run(args) return 0 except ConnectCommandException as e: print(str(e), file=sys.stderr) return 1 except IdbException as e: print(e.args[0], file=sys.stderr) return 1 except Exception: logger.exception("Exception thrown in main") return 1 finally: await plugin.on_close(logger) pending = set(asyncio.all_tasks()) current_task = asyncio.current_task() if current_task is not None: pending.discard(current_task) await drain_coroutines(pending)
def dump_info(signal=None, frame=None, file=sys.stdout, testing=False): # pragma: no cover with redirect_stdout(file): print("****************************************************") print("Summary") print("=======") try: import psutil except: print("(psutil not installed, skipping some debug info)") else: p = psutil.Process() print("num threads: ", p.num_threads()) if hasattr(p, "num_fds"): print("num fds: ", p.num_fds()) print("memory: ", p.memory_info()) print() print("Files") print("=====") for i in p.open_files(): print(i) print() print("Connections") print("===========") for i in p.connections(): print(i) print() print("Threads") print("=======") bthreads = [] for i in threading.enumerate(): if hasattr(i, "_threadinfo"): bthreads.append(i) else: print(i.name) bthreads.sort(key=lambda x: x._thread_started) for i in bthreads: print(i._threadinfo()) print() print("Memory") print("=======") gc.collect() d = {} for i in gc.get_objects(): t = str(type(i)) if "mitmproxy" in t: d[t] = d.setdefault(t, 0) + 1 itms = list(d.items()) itms.sort(key=lambda x: x[1]) for i in itms[-20:]: print(i[1], i[0]) try: if sys.version_info < (3, 8): raise RuntimeError asyncio.get_running_loop() except RuntimeError: pass else: print() print("Tasks") print("=======") for task in asyncio.all_tasks(): f = task.get_stack(limit=1)[0] line = linecache.getline(f.f_code.co_filename, f.f_lineno, f.f_globals).strip() line = f"{line} # at {os.path.basename(f.f_code.co_filename)}:{f.f_lineno}" print(f"{asyncio_utils.task_repr(task)}\n" f" {line}") print("****************************************************") if not testing: sys.exit(1)
#!/usr/bin/env python3 import asyncio import time async def main(): print(f"{time.ctime()} hello") await asyncio.sleep(0.1) print(f"{time.ctime()} world") loop = asyncio.get_event_loop() task = loop.create_task(main()) loop.run_until_complete(task) pending = asyncio.all_tasks(loop=loop) for task in pending: task.cancel() group = asyncio.gather(*pending, return_exceptions=True) loop.run_until_complete(group) loop.close()
def on_stop(self): tasks = [ t for t in asyncio.all_tasks() if t is not asyncio.current_task() ] [task.cancel() for task in tasks] Logger.info(f"MediaApp: Cancelling {len(tasks)} outstanding tasks")
def tearDownClass(cls) -> None: for task in asyncio.all_tasks(loop=cls.ev_loop): task.cancel()