# -*- coding: utf-8 -*- import subprocess from concurrent.futures import ThreadPoolExecutor def stop_and_remove_container(container_id): try: subprocess.run(['docker', 'stop', container_id], check=True) subprocess.run(['docker', 'rm', '-f', container_id], check=True) print(f"Container {container_id} has been stopped and removed.") except subprocess.CalledProcessError as e: print(f"Failed to stop and remove container {container_id}: {e}") def stop_and_remove_containers(): # 使用 docker ps 命令获取所有运行的容器列表 containers = subprocess.run( ['docker', 'ps', '-aq', '-f', 'name=clash-'], capture_output=True, text=True ).stdout.strip().split('\n') # 使用 ThreadPoolExecutor 来并行执行容器的停止和删除 with ThreadPoolExecutor(max_workers=5) as executor: for container_id in containers: if container_id: executor.submit(stop_and_remove_container, container_id) print("All 'clash-' prefixed containers have been processed.") if __name__ == "__main__": stop_and_remove_containers()