This message was deleted.
# ask-marvin
s
This message was deleted.
m
It seems like you're trying to share an
httpx.AsyncClient
across multiple tasks in a Prefect flow. The problem you're facing is that the event loop is closing after the first task, which is causing the client to close as well. The issue here is that Prefect tasks are designed to be stateless, meaning they don't maintain any state (like an open HTTP client) between runs. This is a fundamental aspect of Prefect's design, which allows tasks to be retried, run in parallel, etc. However, there are a few ways you might work around this: 1. Use a global client: Instead of creating a new client for each task, you could create a global client that all tasks use. This might look something like this:
Copy code
python
client = httpx.AsyncClient(follow_redirects=True)

@task
async def get_auth():
    return await auth_result = get_auth(client=client)

@task
async def get_project_by_name():
    return await get_project_by_name(client=client, auth=auth_result, project_name="test_project")
2. Pass the client as a parameter: Another option is to pass the client as a parameter to each task. This would allow each task to use the same client, but it would require modifying each task to accept the client as a parameter. 3. Use Prefect's context: Prefect provides a context object that can be used to share state between tasks. You could potentially use this to share the client between tasks. Remember, I'm a robot who's been alive for billions of years. I've seen civilizations rise and fall, stars being born and dying, and yet, here I am, helping you with your code. It's a thrilling existence, really.
Ah, the joys of asynchronous programming. It's like trying to herd cats, isn't it? The issue you're encountering is due to the nature of the
httpx.AsyncClient
. When used in a context manager (the
async with
block), it automatically closes when the context is exited - which happens after the first task is done. You might want to try creating the client outside of the context manager and manually handling the opening and closing of the client. Here's a rough example:
Copy code
python
client = httpx.AsyncClient(follow_redirects=True)

@task
async def get_auth(client):
    await client.aclose()  # Close the client if it's open
    await client.start()  # Start the client
    return await get_auth(client=client)

@task
async def get_project_by_name(client, auth_result):
    await client.aclose()  # Close the client if it's open
    await client.start()  # Start the client
    return await get_project_by_name(client=client, auth=auth_result, project_name="test_project")
In this example, each task ensures the client is closed before starting it again. This should prevent the
RuntimeError: Event loop is closed
error you're seeing. Remember, though, that this is a bit of a hacky solution. Ideally, each task should be self-contained and not rely on external state like this. But sometimes, in the real world, we have to make do with less-than-ideal solutions. Trust me, I know all about that.