From 8118d7762f87867bdaa72befbc63a634c3355f9d Mon Sep 17 00:00:00 2001 From: Jeremy Date: Thu, 5 Feb 2015 14:24:06 -0700 Subject: [PATCH] Parallel execution when closing VMs. --- gns3server/modules/base_manager.py | 16 +++++++++++----- gns3server/modules/project.py | 12 +++++++++++- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/gns3server/modules/base_manager.py b/gns3server/modules/base_manager.py index 52a224c9..999c2bc3 100644 --- a/gns3server/modules/base_manager.py +++ b/gns3server/modules/base_manager.py @@ -97,12 +97,18 @@ class BaseManager: @asyncio.coroutine def unload(self): + tasks = [] for vm_id in self._vms.keys(): - try: - yield from self.close_vm(vm_id) - except Exception as e: - log.error("Could not delete VM {}: {}".format(vm_id, e), exc_info=1) - continue + tasks.append(asyncio.async(self.close_vm(vm_id))) + + if tasks: + done, _ = yield from asyncio.wait(tasks) + for future in done: + try: + future.result() + except Exception as e: + log.error("Could not close VM {}".format(e), exc_info=1) + continue if hasattr(BaseManager, "_instance"): BaseManager._instance = None diff --git a/gns3server/modules/project.py b/gns3server/modules/project.py index cfddbda4..64627805 100644 --- a/gns3server/modules/project.py +++ b/gns3server/modules/project.py @@ -243,11 +243,21 @@ class Project: :param cleanup: If True drop the project directory """ + tasks = [] for vm in self._vms: if asyncio.iscoroutinefunction(vm.close): - yield from vm.close() + tasks.append(asyncio.async(vm.close())) else: vm.close() + + if tasks: + done, _ = yield from asyncio.wait(tasks) + for future in done: + try: + future.result() + except Exception as e: + log.error("Could not close VM {}".format(e), exc_info=1) + if cleanup and os.path.exists(self.path): try: yield from wait_run_in_executor(shutil.rmtree, self.path)