Event Loop issues with AsyncFoundryClient from platform sdk in Python FoO

Playing around with the AsyncFoundryClient in FoO, I managed to run some real async code querying the ontology which is great because it sped up my calculations by a factor of 5. However, the I noticed at least in the code repository environment that the success of the function is random. Yes it is still in preview but I would like to understand the problem a little bit better…

I am mixing synchronous client “FoundryClient()” with the “AsyncFoundryClient” to fall back to synchronous in some cases where i need it due to some dependency injection pattern into synchronous functions. Anyways. Is there a way to ensure that the event loop does not close during function execution?

edit: also regarding the whole TCP session handling, I would’ve expected to get some sort of context manager that handles the async sessions…

This solves the issue by using a custom async context manager

from contextlib import asynccontextmanager
from foundry_sdk import AsyncFoundryClient
@asynccontextmanager
async def safe_async_client_sessions(client: AsyncFoundryClient):
    try:
        yield client
    finally:
        # close all clients
        await client.ontologies.Query._api_client._client.aclose()
        await client.ontologies.Ontology.QueryType._api_client._client.aclose()
        await client.ontologies.OntologyObject._api_client._client.aclose()
        await client.ontologies.LinkedObject._api_client._client.aclose()

        await client.ontologies.Ontology._api_client._client.aclose()
        await client.ontologies.OntologyObjectSet._api_client._client.aclose()

Unfortunately, I get an error when I try to use the function in the ontology that says:


So with the FoundrySDK-client from the ontology sdk it works but not with the foundry_sdk client or async client… How can I solve that? :frowning:

Found the issue… I forgot to pass the Config() from the OSDK to the AsyncFoundryClient.
If i pass the config, auth and hostname directly from OSDK client, everything works as expected :smiley:

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.