Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Comments

✨ ExportWebSocketState infastapi.websockets#4376

Merged
tiangolo merged 2 commits intofastapi:masterfrom
matiuszka:expose-websocketstate-enum
Sep 4, 2022
Merged

✨ ExportWebSocketState infastapi.websockets#4376
tiangolo merged 2 commits intofastapi:masterfrom
matiuszka:expose-websocketstate-enum

Conversation

@matiuszka
Copy link
Contributor

Trivial improvement about exposingWebSocketState. It will eliminate the need of importing it fromstarlette directly.

Previously:

fromfastapi.websocketsimportWebSocketfromstarlette.websocketsimportWebSocketState

After:

fromfastapi.websocketsimportWebSocket,WebSocketState

BaekjunKim2 reacted with thumbs up emoji
@matiuszkamatiuszkaforce-pushed theexpose-websocketstate-enum branch from9fa01ba to87b8536CompareJanuary 6, 2022 09:02
@Kludex
Copy link
Member

How do you useWebSocketState?

@matiuszka
Copy link
ContributorAuthor

So there is an endpoint that is sending some data, usually very rarely but still, and is receiving nothing. So while we are not sending any data and the WebSocket connection was closed by the client, the task is still running. So to add functionality that if the connection is closed task will be closed also we are monitoring the WebSocket status, by simple.recevie.

The endpoint more or less looks like that:

@router.websocket("/subscribe/some/data/changes")asyncdefsubscribe_connectors_status(websocket:WebSocket,)->None:asyncdefsend_some_data()->None:last_data=awaitget_data()awaitwebsocket.send_json({"data":last_data})whileTrue:awaitasyncio.sleep(1)new_data=awaitget_data()ifnew_data!=last_data:# This happens very rarely that's why I would like to close all of these unused tasks.last_data=new_dataawaitwebsocket.send_json({"foo":last_data})asyncdefwatch_status()->None:whilewebsocket.client_state==WebSocketState.CONNECTED:# We are not expecting any data, but this will change to the client state if the connection will be closed.awaitwebsocket.receive()tg.cancel_scope.cancel()awaitwebsocket.accept()asyncwithcreate_task_group()astg:tg.start_soon(watch_status)tg.start_soon(send_some_data)

@codecov
Copy link

codecovbot commentedAug 31, 2022
edited
Loading

Codecov Report

Merging#4376 (36dadce) intomaster (0bb8920) willnot change coverage.
The diff coverage is100.00%.

@@            Coverage Diff            @@##            master     #4376   +/-   ##=========================================  Coverage   100.00%   100.00%           =========================================  Files          535       535             Lines        13825     13826    +1     =========================================+ Hits         13825     13826    +1
Impacted FilesCoverage Δ
fastapi/websockets.py100.00% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell ushow you rate us. Have a feature suggestion?Share it here.

@github-actions
Copy link
Contributor

📝 Docs preview for commit36dadce at:https://630fd8992dbce202ce9f181a--fastapi.netlify.app

@tiangolotiangolo changed the titleExposeWebSocketState✨ ExportWebSocketState infastapi.websocketsSep 4, 2022
@tiangolo
Copy link
Member

Thanks for the contribution@matiuszka! 🍰

And thanks@Kludex for the help! 🙇

Knowing how you are using it made me feel it makes sense to have this in, so I'll take it. 🚀

Thanks! ☕

@tiangolotiangolo merged commitdacb689 intofastapi:masterSep 4, 2022
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

1 more reviewer

@TheOnlyWayUpTheOnlyWayUpTheOnlyWayUp approved these changes

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

4 participants

@matiuszka@Kludex@tiangolo@TheOnlyWayUp

[8]ページ先頭

©2009-2026 Movatter.jp