Skip to content

Commit 791930c

Browse files
committed
Optimized cities_light cmd, fixed memory leak ...
- Command was affected by this: ehttps://docs.djangoproject.com/en/dev/faq/models/#why-is-django-leaking-memory - Also, cache ids rather than model objects in the Command, - Also, del the cache when it's not needed anymore.
1 parent b82300d commit 791930c

File tree

1 file changed

+27
-19
lines changed

1 file changed

+27
-19
lines changed

cities_light/management/commands/cities_light.py

+27-19
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111
except ImportError:
1212
import pickle
1313

14+
from pympler.asizeof import asizeof
15+
1416
import progressbar
1517

1618
from django.core.management.base import BaseCommand
17-
from django.db import transaction
19+
from django.db import transaction, reset_queries
1820
from django.utils.encoding import force_unicode
1921

2022
from ...exceptions import *
@@ -136,6 +138,8 @@ def handle(self, *args, **options):
136138
elif url in TRANSLATION_SOURCES:
137139
self.translation_parse(items)
138140

141+
reset_queries()
142+
139143
i += 1
140144
progress.update(i)
141145

@@ -152,34 +156,34 @@ def handle(self, *args, **options):
152156
self.logger.info('Importing parsed translation in the database')
153157
self.translation_import()
154158

155-
def _get_country(self, code2):
159+
def _get_country_id(self, code2):
156160
'''
157161
Simple lazy identity map for code2->country
158162
'''
159163
if not hasattr(self, '_country_codes'):
160164
self._country_codes = {}
161165

162166
if code2 not in self._country_codes.keys():
163-
self._country_codes[code2] = Country.objects.get(code2=code2)
167+
self._country_codes[code2] = Country.objects.get(code2=code2).pk
164168

165169
return self._country_codes[code2]
166170

167-
def _get_region(self, country_code2, region_id):
171+
def _get_region_id(self, country_code2, region_id):
168172
'''
169173
Simple lazy identity map for (country_code2, region_id)->region
170174
'''
171175
if not hasattr(self, '_region_codes'):
172176
self._region_codes = {}
173177

174-
country = self._get_country(country_code2)
175-
if country.code2 not in self._region_codes:
176-
self._region_codes[country.code2] = {}
178+
country_id = self._get_country_id(country_code2)
179+
if country_id not in self._region_codes:
180+
self._region_codes[country_id] = {}
177181

178-
if region_id not in self._region_codes[country.code2]:
179-
self._region_codes[country.code2][region_id] = Region.objects.get(
180-
country=country, geoname_code=region_id)
182+
if region_id not in self._region_codes[country_id]:
183+
self._region_codes[country_id][region_id] = Region.objects.get(
184+
country_id=country_id, geoname_code=region_id).pk
181185

182-
return self._region_codes[country.code2][region_id]
186+
return self._region_codes[country_id][region_id]
183187

184188
def country_import(self, items):
185189
try:
@@ -211,14 +215,14 @@ def region_import(self, items):
211215

212216
code2, geoname_code = items[0].split('.')
213217

214-
country = self._get_country(code2)
218+
country_id = self._get_country_id(code2)
215219

216220
if items[3]:
217221
kwargs = dict(geoname_id=items[3])
218222
else:
219223
try:
220224
kwargs = dict(name=name,
221-
country=country)
225+
country_id=country_id)
222226
except Country.DoesNotExist:
223227
if self.noinsert:
224228
return
@@ -236,7 +240,7 @@ def region_import(self, items):
236240
region.name = name
237241

238242
if not region.country_id:
239-
region.country = country
243+
region.country_id = country_id
240244

241245
if not region.geoname_code:
242246
region.geoname_code = geoname_code
@@ -254,7 +258,7 @@ def city_import(self, items):
254258
return
255259

256260
try:
257-
country = self._get_country(items[8])
261+
country_id = self._get_country_id(items[8])
258262
except Country.DoesNotExist:
259263
if self.noinsert:
260264
return
@@ -263,7 +267,7 @@ def city_import(self, items):
263267

264268
try:
265269
kwargs = dict(name=force_unicode(items[1]),
266-
country=self._get_country(items[8]))
270+
country_id=self._get_country_id(items[8]))
267271
except Country.DoesNotExist:
268272
if self.noinsert:
269273
return
@@ -276,17 +280,17 @@ def city_import(self, items):
276280
try:
277281
city = City.objects.get(geoname_id=items[0])
278282
city.name = force_unicode(items[1])
279-
city.country = self._get_country(items[8])
283+
city.country_id = self._get_country_id(items[8])
280284
except City.DoesNotExist:
281285
if self.noinsert:
282286
return
283287

284288
city = City(**kwargs)
285289

286290
save = False
287-
if not city.region:
291+
if not city.region_id:
288292
try:
289-
city.region = self._get_region(items[8], items[10])
293+
city.region_id = self._get_region_id(items[8], items[10])
290294
except Region.DoesNotExist:
291295
pass
292296
else:
@@ -317,6 +321,10 @@ def city_import(self, items):
317321
city.save()
318322

319323
def translation_parse(self, items):
324+
# free some memory
325+
self._country_codes = None
326+
self._region_codes = None
327+
320328
if not hasattr(self, 'translation_data'):
321329
self.country_ids = Country.objects.values_list('geoname_id',
322330
flat=True)

0 commit comments

Comments
 (0)