Performance improvement - Only reset backends loaded into memory (#4274)
This commit is contained in:
		
							parent
							
								
									a147eeac67
								
							
						
					
					
						commit
						3498689d9d
					
				| @ -2,6 +2,7 @@ from __future__ import unicode_literals | ||||
| 
 | ||||
| import importlib | ||||
| import moto | ||||
| import sys | ||||
| 
 | ||||
| 
 | ||||
| decorators = [ | ||||
| @ -26,8 +27,16 @@ def backends(): | ||||
|         yield _import_backend(module_name, backends_name) | ||||
| 
 | ||||
| 
 | ||||
| def named_backends(): | ||||
|     for name, (module_name, backends_name) in BACKENDS.items(): | ||||
| def loaded_backends(): | ||||
|     loaded_modules = sys.modules.keys() | ||||
|     loaded_modules = [m for m in loaded_modules if m.startswith("moto.")] | ||||
|     imported_backends = [ | ||||
|         name | ||||
|         for name, (module_name, _) in BACKENDS.items() | ||||
|         if f"moto.{module_name}" in loaded_modules | ||||
|     ] | ||||
|     for name in imported_backends: | ||||
|         module_name, backends_name = BACKENDS[name] | ||||
|         yield name, _import_backend(module_name, backends_name) | ||||
| 
 | ||||
| 
 | ||||
|  | ||||
| @ -840,7 +840,7 @@ class MotoAPIBackend(BaseBackend): | ||||
|     def reset(self): | ||||
|         import moto.backends as backends | ||||
| 
 | ||||
|         for name, backends_ in backends.named_backends(): | ||||
|         for name, backends_ in backends.loaded_backends(): | ||||
|             if name == "moto_api": | ||||
|                 continue | ||||
|             for region_name, backend in backends_.items(): | ||||
|  | ||||
| @ -74,13 +74,13 @@ class MediaPackageBackend(BaseBackend): | ||||
|     def __init__(self, region_name=None): | ||||
|         super(MediaPackageBackend, self).__init__() | ||||
|         self.region_name = region_name | ||||
|         self._channels = OrderedDict() | ||||
|         self._origin_endpoints = OrderedDict() | ||||
| 
 | ||||
|     def reset(self): | ||||
|         region_name = self.region_name | ||||
|         self.__dict__ = {} | ||||
|         self.__init__(region_name) | ||||
|         self._channels = OrderedDict() | ||||
|         self._origin_endpoints = OrderedDict() | ||||
| 
 | ||||
|     def create_channel(self, description, id, tags): | ||||
|         arn = "arn:aws:mediapackage:channel:{}".format(id) | ||||
|  | ||||
| @ -68,9 +68,12 @@ class DomainDispatcherApplication(object): | ||||
|             if pattern.match("http://%s" % host): | ||||
|                 return backend | ||||
| 
 | ||||
|         if "amazonaws.com" in host: | ||||
|             print( | ||||
|             "Unable to find appropriate URL for {}." | ||||
|             "Remember to add the URL to urls.py, and run script/update_backend_index.py to index it." | ||||
|                 "Unable to find appropriate backend for {}." | ||||
|                 "Remember to add the URL to urls.py, and run script/update_backend_index.py to index it.".format( | ||||
|                     host | ||||
|                 ) | ||||
|             ) | ||||
| 
 | ||||
|     def infer_service_region_host(self, environ): | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user