Skip to content

Commit

Permalink
Use QuerySet.iterator to reduce memory usage
Browse files Browse the repository at this point in the history
  • Loading branch information
jjnesbitt committed Oct 15, 2024
1 parent ed4c2d0 commit 567bbe3
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions dandiapi/api/management/commands/extract_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from dandischema.models import get_schema_version
from django.contrib.auth.models import User
from django.db import transaction
from django.db.models import QuerySet
import djclick as click
from tqdm import tqdm

Expand Down Expand Up @@ -77,7 +78,7 @@ def extract_asset_metadata(asset: Asset, draft_version: Version):

def extract_dandiset_assets(dandiset: Dandiset):
# Only update NWB assets which are out of date and do not belong to a published version
assets = dandiset.draft_version.assets.filter(
assets: QuerySet[Asset] = dandiset.draft_version.assets.filter(
published=False,
path__iendswith='.nwb',
metadata__schemaVersion__lt=get_schema_version(),
Expand All @@ -86,7 +87,7 @@ def extract_dandiset_assets(dandiset: Dandiset):
logger.info('No old draft NWB assets found in dandiset %s. Skipping...', dandiset)
return

for asset in tqdm(assets):
for asset in tqdm(assets.iterator(), total=assets.count()):
extract_asset_metadata(asset=asset, draft_version=dandiset.draft_version)


Expand All @@ -101,7 +102,7 @@ def asset(asset_id: str):
)

# Re-extract for every draft version
for version in draft_versions:
for version in draft_versions.iterator():
extract_asset_metadata(asset=asset, draft_version=version)


Expand All @@ -116,6 +117,6 @@ def dandiset(dandiset_id: str):

@group.command(name='all', help='Re-extracts the metadata of all assets in all draft versions')
def all_dandisets():
for dandiset in Dandiset.objects.all():
for dandiset in Dandiset.objects.iterator():
logger.info('DANDISET: %s', dandiset.identifier)
extract_dandiset_assets(dandiset)

0 comments on commit 567bbe3

Please sign in to comment.