liamkultra
07/22/2025, 7:22 PMError crawling target dummy2: Service StorageClient is already in use. Existing value: <crawlee.storage_clients._memory._memory_storage_client.MemoryStorageClient object at 0x335e5ef30>, attempted new value: <crawlee.storage_clients._memory._memory_storage_client.MemoryStorageClient object at 0x335e7c710>.
Upon investigation I discovered the docs saying:
> The FileSystemStorageClient is not safe for concurrent access from multiple crawler processes. Use it only when running a single crawler process at a time.
So, even though it appears to be working with some basic tests, I'm not confident this approach will work. I actually don't want concurrent access, I want the storage to be separated, on a per-crawler basis. (Or otherwise, segmented within the Memory or File storage.) I'm not opposed to pointing it at '/tmp' in production, but the warning makes me doubtful that it would work correctly.
I did try creating multiple memory clients by setting unique queue_id, store_id and dataset_id, but that resulted in the same error.
Is this a limitation, or perhaps is there some way of doing what I'm trying to do in some other way?
Thanks for your help!Answer Overflow
07/22/2025, 7:22 PMMantisus
07/22/2025, 8:48 PMpython
import asyncio
from crawlee import service_locator
from crawlee.crawlers import HttpCrawler, HttpCrawlingContext
from crawlee.storage_clients import MemoryStorageClient
from crawlee.storages import RequestQueue
async def main() -> None:
storage_client = MemoryStorageClient()
service_locator.set_storage_client(storage_client)
crawler_queue_1 = await RequestQueue.open(name='crawler-queue-1')
crawler_queue_2 = await RequestQueue.open(name='crawler-queue-2')
crawler1 = HttpCrawler(request_manager=crawler_queue_1)
crawler2 = HttpCrawler(request_manager=crawler_queue_2)
@crawler1.router.default_handler
@crawler2.router.default_handler
async def request_handler(context: HttpCrawlingContext) -> None:
context.log.info(f'Processing {context.request.url} ...')
await asyncio.gather(
crawler1.run(['https://crawlee.dev/']),
crawler2.run(['https://crawlee.dev/']),
)
if __name__ == '__main__':
asyncio.run(main())liamkultra
07/23/2025, 10:15 AMMantisus
07/23/2025, 11:03 AMApifyBot
07/23/2025, 11:03 AMliamkultra
07/26/2025, 4:02 PMMantisus
07/26/2025, 4:11 PMliamkultra
07/26/2025, 4:30 PMpijukatel
07/28/2025, 9:01 AMMantisus
07/28/2025, 10:55 AMliamkultra
07/29/2025, 10:42 PMMantisus
07/30/2025, 1:19 AMpython
import asyncio
from crawlee.crawlers import HttpCrawler, HttpCrawlingContext
from crawlee.storages import RequestQueue
from crawlee import Request
futures_results = {}
targets = [
{'name': 'Apify', 'urls': ['https://apify.com/', 'https://docs.apify.com/']},
{'name': 'Crawlee', 'urls': ['https://crawlee.dev/', 'https://crawlee.dev/python']}
]
async def expect_results() -> None:
process_counter = len(targets)
while process_counter > 0:
for target_name, future in futures_results.items():
if future and future.done():
print(f"Results for {target_name}: Done!")
process_counter -= 1
futures_results[target_name] = None
await asyncio.sleep(0.1) # Wait for the request to be processed
async def main() -> None:
request_queue = await RequestQueue.open()
crawler = HttpCrawler(request_manager=request_queue)
state = await crawler._use_state()
for target in targets:
state[target['name']] = len(target['urls'])
for url in target['urls']:
await request_queue.add_request(Request.from_url(url, user_data={'target_name': target['name']}))
futures_results[target['name']] = asyncio.Future()
@crawler.router.default_handler
async def request_handler(context: HttpCrawlingContext) -> None:
context.log.info(f'Processing {context.request.url} ...')
target_name = context.request.user_data.get('target_name', 'Unknown')
state = await context.use_state()
state[target_name] -= 1
if state[target_name] == 0:
futures_results[target_name].set_result(None)
await asyncio.sleep(0.1) # Simulate some processing time
asyncio.create_task(expect_results())
await crawler.run()
asyncio.run(main())Mantisus
07/30/2025, 1:19 AMMantisus
07/30/2025, 1:25 AMexpect_results function tracks which target has already been processed.
I use use_state to track progress because it is protected from state races.
But we originally planned it as a Context method. So at the top level, it looks a little messy when I have to call the private method crawler._use_state().
This may give you some ideas for improving your solution. In any case, I am glad that you have already achieved good optimization.