def _target(self) -> None: # Every thread must have its own loop. The parent thread (pytest) # needs to know when the loop is set up, to be able to shut it down. loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) self._ready.set() # Execute the requested CLI command in the thread & thread's loop. # Remember the result & exception for re-raising in the parent thread. try: ctxobj = cli.CLIControls(registry=self.registry, settings=self.settings, stop_flag=self._stop) runner = click.testing.CliRunner() result = runner.invoke(cli.main, *self.args, **self.kwargs, obj=ctxobj) except BaseException as e: self._future.set_exception(e) else: self._future.set_result(result) finally: # Shut down the API-watching streams. loop.run_until_complete(loop.shutdown_asyncgens()) # Shut down the transports and prevent ResourceWarning: unclosed transport. # See: https://docs.aiohttp.org/en/stable/client_advanced.html#graceful-shutdown # TODO: Try a hack: https://github.com/aio-libs/aiohttp/issues/1925#issuecomment-575754386 loop.run_until_complete(asyncio.sleep(1.0)) loop.close()
def _target(self) -> None: # Every thread must have its own loop. The parent thread (pytest) # needs to know when the loop is set up, to be able to shut it down. asyncio.set_event_loop(self._loop) self._ready.set() # Execute the requested CLI command in the thread & thread's loop. # Remember the result & exception for re-raising in the parent thread. try: ctxobj = cli.CLIControls(stop_flag=self._stop) runner = click.testing.CliRunner() result = runner.invoke(cli.main, *self.args, **self.kwargs, obj=ctxobj) except BaseException as e: self._future.set_exception(e) else: self._future.set_result(result)