Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

HTTPX · Function

aclose

AsyncClient.aclose() asynchronously closes the client's transport and proxies. Await it when explicitly managing an AsyncClient instead of relying on an async context manager.

How it works

aclose() asynchronously shuts down the AsyncClient's transport and proxies. Await it when a client was created without an async context manager, typically after all requests have completed.

Syntax

await client.aclose()

Returns

None

Key points

  • The method closes the asynchronous client's transport and configured proxies.
  • Await it when managing the client manually instead of using context-managed usage.
  • It belongs to AsyncClient, which supports asynchronous requests and connection pooling.
  • A single client may be shared across concurrent tasks and closed after those tasks complete.

Common mistakes

  • Because this method performs asynchronous cleanup, it must be awaited; forgetting to await it leaves the client's resources unmanaged.

Example: Explicitly close an AsyncClient

import asyncio
import httpx

async def main():
    client = httpx.AsyncClient()
    try:
        response = await client.get("https://example.org")
        print(response.status_code)
    finally:
        await client.aclose()

asyncio.run(main())

This creates an asynchronous client, performs a request, and explicitly releases its resources in a finally block.

When to use it

Close a client created outside `async with`

Manual lifecycle management

Release resources from a `finally` block

Guaranteed cleanup

Close one client after concurrent tasks finish

Shared asynchronous client

Interview questions

QWhat does AsyncClient.aclose() do?

Call await client.aclose() when an AsyncClient should be closed explicitly. The method closes the client's transport and proxies.

Follow-up: How can explicit cleanup be avoided? Use async with httpx.AsyncClient() as client: so the context manager closes the client when the block exits.

QWhat is the signature and return value?

aclose() is asynchronous and takes only self, so it has no configuration parameters. Its documented return value is None.

Follow-up: What should code do if it needs to continue making requests? Keep the client open until all requests finish, then await aclose() once.

QWhen should an application call it?

Use explicit closure when the client is managed outside a context-manager block, such as a longer-lived application component. Put cleanup in a finally block so it also runs when a request fails.

Follow-up: What is the simpler scoped alternative? Use an async with block around the client and perform all related requests inside it.

  • AsyncClient
  • AsyncClient.__aenter__
  • AsyncClient.__aexit__
  • Client.close

Source: Developer Interface - HTTPX. Checked 2026-10-15.