Skip to content

Commit 5147f70

Browse files
committed
do not create the local storage client
1 parent e5408c6 commit 5147f70

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

src/apify/_actor.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
from apify_shared.utils import ignore_docs, maybe_extract_enum_member_value
1616
from crawlee import service_locator
1717
from crawlee.events._types import Event, EventMigratingData, EventPersistStateData
18-
from crawlee.storage_clients import MemoryStorageClient
1918

2019
from apify._configuration import Configuration
2120
from apify._consts import EVENT_LISTENERS_TIMEOUT
@@ -35,6 +34,7 @@
3534
from typing_extensions import Self
3635

3736
from crawlee.proxy_configuration import _NewUrlFunction
37+
from crawlee.storage_clients import BaseStorageClient
3838

3939
from apify._models import Webhook
4040

@@ -72,8 +72,8 @@ def __init__(
7272
self._configure_logging = configure_logging
7373
self._apify_client = self.new_client()
7474

75-
# We need to keep both local & cloud storage clients because of the `force_cloud` option.
76-
self._local_storage_client = MemoryStorageClient.from_config(config=self.config)
75+
# Create the instance of the cloud storage client, the local storage client is obtained
76+
# from the service locator.
7777
self._cloud_storage_client = ApifyStorageClient.from_config(config=self.config)
7878

7979
# Set the event manager based on whether the Actor is running on the platform or locally.
@@ -159,6 +159,11 @@ def log(self) -> logging.Logger:
159159
"""The logging.Logger instance the Actor uses."""
160160
return logger
161161

162+
@property
163+
def _local_storage_client(self) -> BaseStorageClient:
164+
"""The local storage client the Actor instance uses."""
165+
return service_locator.get_storage_client()
166+
162167
def _raise_if_not_initialized(self) -> None:
163168
if not self._is_initialized:
164169
raise RuntimeError('The Actor was not initialized!')
@@ -190,11 +195,9 @@ async def init(self) -> None:
190195
self._is_exiting = False
191196
self._was_final_persist_state_emitted = False
192197

193-
# Register services in the service locator.
198+
# If the Actor is running on the Apify platform, we set the cloud storage client.
194199
if self.is_at_home():
195200
service_locator.set_storage_client(self._cloud_storage_client)
196-
else:
197-
service_locator.set_storage_client(self._local_storage_client)
198201

199202
service_locator.set_event_manager(self.event_manager)
200203
service_locator.set_configuration(self.configuration)
@@ -354,7 +357,7 @@ async def open_dataset(
354357
self._raise_if_not_initialized()
355358
self._raise_if_cloud_requested_but_not_configured(force_cloud=force_cloud)
356359

357-
storage_client = self._cloud_storage_client if force_cloud else service_locator.get_storage_client()
360+
storage_client = self._cloud_storage_client if force_cloud else self._local_storage_client
358361

359362
return await Dataset.open(
360363
id=id,
@@ -388,7 +391,7 @@ async def open_key_value_store(
388391
"""
389392
self._raise_if_not_initialized()
390393
self._raise_if_cloud_requested_but_not_configured(force_cloud=force_cloud)
391-
storage_client = self._cloud_storage_client if force_cloud else service_locator.get_storage_client()
394+
storage_client = self._cloud_storage_client if force_cloud else self._local_storage_client
392395

393396
return await KeyValueStore.open(
394397
id=id,
@@ -425,7 +428,7 @@ async def open_request_queue(
425428
self._raise_if_not_initialized()
426429
self._raise_if_cloud_requested_but_not_configured(force_cloud=force_cloud)
427430

428-
storage_client = self._cloud_storage_client if force_cloud else service_locator.get_storage_client()
431+
storage_client = self._cloud_storage_client if force_cloud else self._local_storage_client
429432

430433
return await RequestQueue.open(
431434
id=id,

0 commit comments

Comments
 (0)