Skip to content

Commit 4cd537b

Browse files
committed
Print warning when multiple Actor inits take place
1 parent 127f31b commit 4cd537b

File tree

2 files changed

+6
-0
lines changed

2 files changed

+6
-0
lines changed

src/apify/_actor.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class _ActorType:
5656
"""The class of `Actor`. Only make a new instance if you're absolutely sure you need to."""
5757

5858
_is_rebooting = False
59+
_is_any_instance_initialized = False
5960

6061
def __init__(
6162
self,
@@ -199,6 +200,9 @@ async def init(self) -> None:
199200
if self._is_initialized:
200201
raise RuntimeError('The Actor was already initialized!')
201202

203+
if _ActorType._is_any_instance_initialized:
204+
self.log.warning('Repeated Actor initialization detected - this is non-standard usage, proceed with care')
205+
202206
# Make sure that the currently initialized instance is also available through the global `Actor` proxy
203207
cast(Proxy, Actor).__wrapped__ = self
204208

@@ -225,6 +229,7 @@ async def init(self) -> None:
225229
await self._event_manager.__aenter__()
226230

227231
self._is_initialized = True
232+
_ActorType._is_any_instance_initialized = True
228233

229234
async def exit(
230235
self,

tests/unit/conftest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ def prepare_test_env(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> Callabl
3939

4040
def _prepare_test_env() -> None:
4141
delattr(apify._actor.Actor, '__wrapped__')
42+
apify._actor._ActorType._is_any_instance_initialized = False
4243

4344
# Set the environment variable for the local storage directory to the temporary path.
4445
monkeypatch.setenv(ApifyEnvVars.LOCAL_STORAGE_DIR, str(tmp_path))

0 commit comments

Comments
 (0)