Skip to content

Commit

Permalink
Merge pull request #2553 from tkalir/bug-2547-memory-issue-in-fill-ca…
Browse files Browse the repository at this point in the history
…che-for-streets

Bug 2547 memory issue in fill cache for streets
  • Loading branch information
atalyaalon authored Jan 17, 2024
2 parents a27fb23 + e463a0f commit a630689
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
26 changes: 14 additions & 12 deletions anyway/parsers/infographics_data_cache_updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from anyway.request_params import RequestParams
import anyway.infographics_utils
from anyway.widgets.widget import widgets_dict
from anyway.utilities import chunked_generator
import logging
import json

Expand Down Expand Up @@ -287,18 +288,19 @@ def build_street_cache_into_temp():
start = datetime.now()
db.session.query(InfographicsStreetDataCacheTemp).delete()
db.session.commit()
db.get_engine().execute(
InfographicsStreetDataCacheTemp.__table__.insert(), # pylint: disable=no-member
[
{
"yishuv_symbol": d["yishuv_symbol"],
"street": d["street1"],
"years_ago": d["years_ago"],
"data": anyway.infographics_utils.create_infographics_data_for_location(d),
}
for d in get_street_infographic_keys()
],
)
for chunk in chunked_generator(get_street_infographic_keys(), 4960):
db.get_engine().execute(
InfographicsStreetDataCacheTemp.__table__.insert(), # pylint: disable=no-member
[
{
"yishuv_symbol": d["yishuv_symbol"],
"street": d["street1"],
"years_ago": d["years_ago"],
"data": anyway.infographics_utils.create_infographics_data_for_location(d),
}
for d in chunk
],
)
db.session.commit()
logging.info(f"cache rebuild took:{str(datetime.now() - start)}")

Expand Down
12 changes: 12 additions & 0 deletions anyway/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,18 @@ def chunks(l, n):
yield l[i : i + n]


def chunked_generator(input_generator, chunk_size):
chunk = []
for item in input_generator:
chunk.append(item)
if len(chunk) == chunk_size:
yield chunk
chunk = []

if chunk:
yield chunk


def parse_age_from_range(age_range: int) -> typing.Optional[typing.Tuple[int, int]]:
# Convert from 'age_group' field in the table 'involved_markers_hebrew' to age range numbers
ret_age_code_to_age_range = {
Expand Down

0 comments on commit a630689

Please sign in to comment.