graphql-python/gql

Getting Transport Already Connected using async httpx transport

FrankC01 opened this issue · 3 comments

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

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

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

Works like a charm