graphql-python / gql

A GraphQL client in Python
https://gql.readthedocs.io
MIT License
1.56k stars 180 forks source link

Getting Transport Already Connected using async httpx transport #480

Closed FrankC01 closed 6 months ago

FrankC01 commented 6 months ago

I instantiate the client in my domain client object:

gql_client=Client(
                transport=HTTPXAsyncTransport(
                    url=gurl,
                    verify=True,
                    http2=True,
                    timeout=120.0,
                ),
                fetch_schema_from_transport=True,
            )

In my domain client object execute I have:

        try:
            hdr = self.client_headers
            hdr = hdr if not with_headers else hdr.update(with_headers)
            async with self.client as aclient:
                sres = await aclient.execute(node, extra_args=hdr)
                return SuiRpcResult(
                    True, None, sres if not encode_fn else encode_fn(sres)
                )

        except texc.TransportQueryError as gte:
            return SuiRpcResult(
                False, "TransportQueryError", pgql_type.ErrorGQL.from_query(gte.errors)
            )

In my python console script I create 5 tasks (summary of task)

async def _get_all_gas_objects(
    client: AsyncSuiGQLClient, address_id: str
) -> list[pgql_type.SuiCoinObjectGQL]:
    """Retreive all Gas Objects."""
    coin_list: list[pgql_type.SuiCoinObjectGQL] = []
    result = await client.execute_query_node(with_node=qn.GetCoins(owner=address_id))
    if result.is_ok():
        coin_list = result.result_data
    else:
       raise ValueError(f"Execute query error: {result.result_string}")
    return coin_list

Here is the heart of the task generator:

     addys = [x for x in config.addresses]

    addy_list = [_get_all_gas_objects(client, x) for x in addys]
    gresult = await asyncio.gather(*addy_list, return_exceptions=True)

I get expected results in first task execution but the remaining have the TransportAlreadyConnected

leszekhanusz commented 6 months ago

It's only possible to have one open connection at a time with a transport.

For your use case, you could instead use an async permanent session

FrankC01 commented 6 months ago

Wow, I should read more... I'll give it a whirl.

FrankC01 commented 6 months ago

Works like a charm