Only retain "used importers" if instructed, in import handler

apparently retaining those will prevent garbage collection! so caller must opt
in to that kind of behavior, sheesh
This commit is contained in:
Lance Edgar 2019-10-29 22:38:54 -05:00
parent 5128e3f2fe
commit 0c9e9b7245

View file

@ -359,8 +359,15 @@ class ImportHandler(object):
def import_data(self, *keys, **kwargs): def import_data(self, *keys, **kwargs):
""" """
Import all data for the given importer/model keys. Import all data for the given importer/model keys.
:param retain_used_importers: Optional flag to indicate the handler
should retain references to all importers it creates/runs. If this
flag is set then the handler will have a ``used_importers``
attribute after this method completes. This would be a dictionary
whose keys are model names and values are the importer instances.
""" """
self.import_began = make_utc(tzinfo=True) self.import_began = make_utc(tzinfo=True)
retain_used_importers = kwargs.pop('retain_used_importers', False)
if 'dry_run' in kwargs: if 'dry_run' in kwargs:
self.dry_run = kwargs['dry_run'] self.dry_run = kwargs['dry_run']
self.progress = kwargs.pop('progress', getattr(self, 'progress', None)) self.progress = kwargs.pop('progress', getattr(self, 'progress', None))
@ -376,7 +383,8 @@ class ImportHandler(object):
# since some of them may have state which callers need to reference for # since some of them may have state which callers need to reference for
# display to user etc. this doesn't seem like the most elegant # display to user etc. this doesn't seem like the most elegant
# solution perhaps, but gets the job done for now. # solution perhaps, but gets the job done for now.
self.used_importers = {} if retain_used_importers:
self.used_importers = {}
try: try:
for key in keys: for key in keys:
@ -394,7 +402,8 @@ class ImportHandler(object):
changes[key] = created, updated, deleted changes[key] = created, updated, deleted
# keep track of actual importer instance, in case caller # keep track of actual importer instance, in case caller
# needs to reference its state etc. # needs to reference its state etc.
self.used_importers[key] = importer if retain_used_importers:
self.used_importers[key] = importer
else: else:
log.warning("skipping unknown importer: {}".format(key)) log.warning("skipping unknown importer: {}".format(key))
except: except: