Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pre-load all timezone disk reads on import #386

Merged
merged 2 commits into from
Jun 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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