-
Say I have a basic class Fetcher:
def __init__(self):
self._session = None
self.url = None
async def fetch(self, data):
async with self._session.get(self.url,data=data) as resp:
return await resp.json() And other helper functions, such as def run_async_tasks(tasks):
"""Run a list of async tasks."""
tasks_to_execute = tasks
callback_manager = CallbackManager([])
async def _gather():
return await asyncio.gather(*tasks_to_execute)
with callback_manager.as_trace("index_construction"):
outputs = asyncio.run(_gather())
return outputs Now, I want to write a class CustomMetadataExtractor():
async def aextract(self, nodes):
tasks = [self.fetcher.fetch(node) for node in nodes] # I have no idea about how to create a shared fetcher or ClientSession to manage the parallel requests in one single connection pool.
return await asyncio.gather(*tasks) And, the async I want to maintain a single persist There is a similar process the async index building. But I didn't see any manual |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
🤖 To manage a shared class Fetcher:
def __init__(self):
self._session = aiohttp.ClientSession()
async def fetch(self, data):
async with self._session.get(self.url, data=data) as resp:
return await resp.json()
async def close(self):
await self._session.close() Then, in your class CustomMetadataExtractor():
def __init__(self):
self.fetcher = Fetcher()
async def aextract(self, nodes):
tasks = [self.fetcher.fetch(node) for node in nodes]
results = await asyncio.gather(*tasks)
await self.fetcher.close()
return results This way, the same Please note that you should handle exceptions properly to ensure the class CustomMetadataExtractor():
def __init__(self):
self.fetcher = Fetcher()
async def aextract(self, nodes):
try:
tasks = [self.fetcher.fetch(node) for node in nodes]
results = await asyncio.gather(*tasks)
finally:
await self.fetcher.close()
return results This should solve the SourcesThis response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research. Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant. If you want to continue the conversation, start your reply with @dosu-bot. |
Beta Was this translation helpful? Give feedback.
-
I wrote a minimal custom Async |
Beta Was this translation helpful? Give feedback.
I wrote a minimal custom Async
MetadataExtractor
withllama_index 0.8.59
. Hope this could be helpful for you if you have the same problem.