Skip to content

Commit

Permalink
Pre-load all timezone disk reads on import (#386)
Browse files Browse the repository at this point in the history
* Pre-load all timezone disk reads on import

* Remove dead code
  • Loading branch information
allenporter authored Jun 30, 2024
1 parent 272b4a4 commit e5b1fb4
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions ical/tzif/timezoneinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def _read_system_timezones() -> set[str]:
return zoneinfo.available_timezones()


@cache
def _find_tzfile(key: str) -> str | None:
"""Retrieve the path to a TZif file from a key."""
for search_path in zoneinfo.TZPATH:
Expand Down Expand Up @@ -63,6 +64,7 @@ def _iana_key_to_resource(key: str) -> tuple[str, str]:
return package, resource


@cache
def read(key: str) -> TimezoneInfo:
"""Read the TZif file from the tzdata package and return timezone records."""
if key not in _read_system_timezones() and key not in _read_tzdata_timezones():
Expand Down Expand Up @@ -167,3 +169,11 @@ def read_tzinfo(key: str) -> TzInfo:
return TzInfo.from_timezoneinfo(timezoneinfo)
except ValueError as err:
raise TimezoneInfoError(f"Unable create TzInfo: {key}") from err


# Avoid blocking disk reads in async function by pre-loading all timezone reads
for key in _read_system_timezones():
try:
read_tzinfo(key)
except TimezoneInfoError:
pass

0 comments on commit e5b1fb4

Please sign in to comment.