Skip to content

Impossible to use w/o the context manager? #27

Closed Answered by frankie567
ZzZombo asked this question in Q&A
Discussion options

You must be logged in to vote

Yes, it only works as context manager. Under the hood, it uses httpx.stream, which is a context manager itself.

If you really can't use a context manager, a nice trick is to use an ExitStack (or AsyncExitStack for async) from the standard contextlib.

exit_stack =  contextlib.ExitStack()
ws = exit_stack.enter_context(connect_ws("http://localhost:8000/ws"))
message = ws.receive_text()
exit_stack.close()

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by ZzZombo
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants