Identify and close specific connection in AsyncClient

Open
#910 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
35/100
Issue type
Feature
Clarity
Needs clarification
Activity status
Quiet
Tech stack
python
Domain
api, backend

Research direction

The issue names no files, tests, or code entry points. Start by reviewing AsyncClient's documented connection controls and the current connection-pool behavior; a completed investigation should establish whether per-connection identification and selective closure are supported or require a scoped design.

Written by the indexing model from the issue text.

Description

Originally opened by @BlackCloud37 on 2025-08-25 06:04:51 in encode/httpx

Hello,

I'm trying to use httpx(in asyncio) for my web scraper program.

In my use case, to prevent my connection from banning, I usually want to limit the request on single connection, which means:

  1. Close a connection after N requests.
  2. If a connection is banned, for example, received HTTP code 429 or any invalid response.

By reading the document, I cannot find any methods that I can:

  1. Identify each request's underlying connection, so I can count requests on certain connection.
  2. To close this single connection if satisfied any condition above.

So I currently implement this in "one connection per async client" way. I limited the max_connections to 1 for my client, and each time I want to drop a connection, I throw the client away and create a new one.

As the document says, it's a bad idea to do so. So is there any way I can reuse the clients for my purpose?

Any help would be much appreciated!

Dominant language
Python
Stars
1.6k
Forks
103
Avg merge
1d 19h
Merged PRs (30d)
25

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from pydantic/httpx2

All issues in pydantic/httpx2

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.