Skip to content

Add Client.stream() method. - #600

Merged
lovelydinosaur merged 3 commits into
masterfrom
add-client-stream-method
Dec 5, 2019
Merged

lovelydinosaur merged 3 commits into
masterfrom
add-client-stream-method

Conversation

@lovelydinosaur

@lovelydinosaur lovelydinosaur commented Dec 5, 2019 •

Copy link
Copy Markdown
Member

Streaming a request looks like this...

async with httpx.stream("GET", "https://www.example.com") as response:
    async for line in response.stream_lines():
        print(line)

This ensures that our streaming I/O is always properly context scoped.

Essentially closes #555
Closes #588

TODO:

  • Documentation.
  • Notes in Requests compat section.

@florimondmanca florimondmanca left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great!

(Aren鈥檛 you missing an 芦聽async聽禄 in front of 芦聽for聽禄 in your example?)

@lovelydinosaur

Copy link
Copy Markdown
Member Author

Thanks, yes! (Amended now)

@lovelydinosaur lovelydinosaur added the enhancement New feature or request label Dec 5, 2019
@lovelydinosaur

Copy link
Copy Markdown
Member Author

@florimondmanca Thanks for having looked this over! Got the docs in now, too.

This one makes me particularly happy - just seems might nicer having any streaming I/O tightly scoped like this. 馃ぉ

@lovelydinosaur
lovelydinosaur merged commit 8d8ea8b into master Dec 5, 2019
@lovelydinosaur
lovelydinosaur deleted the add-client-stream-method branch December 5, 2019 17:25

@florimondmanca florimondmanca left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Darn typos! 馃檲

Comment thread docs/compatibility.md
but also provide lower-cased versions for API compatibility with `requests`.
* `stream=True`. - Streaming responses provide the `.stream()` and `.raw()` byte iterator interfaces, rather than the `.iter_content()` method and the `.raw` socket interface.
* `httpx.codes` - In our documentation we prefer the uppercased versions, such as `codes.NOT_FOUND`, but also provide lower-cased versions for API compatibility with `requests`.
* `stream()`. - HTTPX provides a `.stream()` interface rather than using `stream=True`. This ensures that streaming responses are always properly closed outside of the stream block, and makes it visually clearer at which points streaming I/O APIs may be used with a response. Streaming request data is made avialable with `.stream_bytes()`, `.stream_text()`, `.stream_lines()`, and `.stream_raw()`.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* `stream()`. - HTTPX provides a `.stream()` interface rather than using `stream=True`. This ensures that streaming responses are always properly closed outside of the stream block, and makes it visually clearer at which points streaming I/O APIs may be used with a response. Streaming request data is made avialable with `.stream_bytes()`, `.stream_text()`, `.stream_lines()`, and `.stream_raw()`.
* `stream()` - HTTPX provides a `.stream()` interface rather than using `stream=True`. This ensures that streaming responses are always properly closed outside of the stream block, and makes it visually clearer at which points streaming I/O APIs may be used with a response. Streaming request data is made available with `.stream_bytes()`, `.stream_text()`, `.stream_lines()`, and `.stream_raw()`.

@vjpr

vjpr commented May 11, 2020 •

Copy link
Copy Markdown

This was marked as making #393 obsolete, however it now departs from the aiohttp api which allows:

  async with aiohttp.ClientSession() as client:
            async with client.request() as resp:
                ...

SOLVED: client.request can be aliased to client.stream and everything works fine.

Kludex referenced this pull request in pydantic/httpx2 May 11, 2026
Bumps [mkdocs-material](https://github.com/squidfunk/mkdocs-material) from 8.5.6 to 8.5.7.
- [Release notes](https://github.com/squidfunk/mkdocs-material/releases)
- [Changelog](https://github.com/squidfunk/mkdocs-material/blob/master/CHANGELOG)
- [Commits](squidfunk/mkdocs-material@8.5.6...8.5.7)

---
updated-dependencies:
- dependency-name: mkdocs-material
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Tom Christie <tom@tomchristie.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Development

Successfully merging this pull request may close these issues.

Reviewing the streaming API Garbage collected responses should warn if still open.

3 participants