Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ Heikki Orsila
Henrik Sozzi
Hugh McNamara
Hugo van Kemenade
Isabelle COWAN-BERGMAN
Jacky Han
Jacob Punter
Jaemin Kim
Expand Down
11 changes: 10 additions & 1 deletion holidays/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# License: MIT (see LICENSE file)

import importlib
from threading import RLock
from typing import Any, Dict, Iterable, Optional, Tuple, Union

from holidays.holiday_base import HolidayBase
Expand Down Expand Up @@ -178,6 +179,11 @@
"ny_stock_exchange": ("NewYorkStockExchange", "NYSE", "XNYS"),
}

# A re-entrant lock. Once a thread has acquired a re-entrant lock,
# the same thread may acquire it again without blocking.
# https://docs.python.org/3/library/threading.html#rlock-objects
IMPORT_LOCK = RLock()

Comment thread
Izzette marked this conversation as resolved.

class EntityLoader:
"""Country and financial holidays entities lazy loader."""
Expand Down Expand Up @@ -223,7 +229,10 @@ def __str__(self) -> str:
def get_entity(self) -> Optional[HolidayBase]:
"""Return lazy-loaded entity."""
if self.entity is None:
self.entity = getattr(importlib.import_module(self.module_name), self.entity_name)
# Avoid deadlock due to importlib.import_module not being thread-safe by caching all
# the first imports in a dedicated thread.
with IMPORT_LOCK:
self.entity = getattr(importlib.import_module(self.module_name), self.entity_name)

return self.entity

Expand Down