-
Notifications
You must be signed in to change notification settings - Fork 51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: support server sent event #333
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
a723b68
init the sse_inference
kemingy 13cdf51
rebase
kemingy 3bef6db
Merge branch 'main' into sse
kemingy a85fed7
impl
kemingy 8e8731e
rebase
kemingy c0549f5
handle sse error
kemingy 28b6228
fix sse error event type
kemingy 78d6313
add sse metrics
kemingy 8bdba45
Merge branch 'main' into sse
kemingy d2ab072
pass the err msg to the client
kemingy a869958
add sse test
kemingy 6c3648e
rebase
kemingy 4d80ba9
format rs import
kemingy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Copyright 2023 MOSEC Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import httpx | ||
from httpx_sse import connect_sse | ||
|
||
with httpx.Client() as client: | ||
with connect_sse( | ||
client, "POST", "http://127.0.0.1:8000/sse_inference", json={"text": "mosec"} | ||
) as event_source: | ||
for sse in event_source.iter_sse(): | ||
print(f"Event({sse.event}): {sse.data}") | ||
|
||
# error handling | ||
with httpx.Client() as client: | ||
with connect_sse( | ||
client, "POST", "http://127.0.0.1:8000/sse_inference", json={"error": "mosec"} | ||
) as event_source: | ||
for sse in event_source.iter_sse(): | ||
print(f"Event({sse.event}): {sse.data}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# Copyright 2023 MOSEC Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from mosec import Server, ValidationError, Worker, get_logger | ||
|
||
logger = get_logger() | ||
|
||
|
||
class Preprocess(Worker): | ||
def forward(self, data): | ||
text = data.get("text") | ||
if text is None: | ||
raise ValidationError("text is required") | ||
return text | ||
|
||
|
||
class Inference(Worker): | ||
def forward(self, data): | ||
epoch = 5 | ||
for i in range(epoch): | ||
for j in range(len(data)): | ||
self.send_stream_event( | ||
f"inference: ({i + 1}/{epoch}) {data[j]}", index=j | ||
) | ||
|
||
# this return value will be ignored | ||
return data | ||
|
||
|
||
if __name__ == "__main__": | ||
server = Server() | ||
server.append_worker(Preprocess) | ||
server.append_worker(Inference, max_batch_size=2) | ||
server.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,4 @@ pre-commit>=2.15.0 | |
msgpack>=1.0.5 | ||
numpy>=1.24 | ||
httpx==0.24.1 | ||
httpx-sse==0.3.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,6 @@ | ||
unstable_features = true | ||
reorder_imports = true | ||
format_strings = true | ||
imports_granularity = "Module" | ||
group_imports = "StdExternalCrate" | ||
reorder_impl_items = true |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since both normal server and sse server will fetch request from this
protocol_recv
, chances are that sse server will get data from/inference
request when clients query the wrong endpoint. In this case, the server will process the data but the client will get nothing.Similarly, if the client query a normal server with
/sse_inference
, weird thing will happen.We'd better to handle this by sending error for now; for future we should not have both endpoints exposed by default, but use multi-route kind of feature to support the specified endpoint only when the server implements it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
impl sse but called /inference => cannot find the stream event channel (info log), but return nothing to the client
impl inference but called /sse_inference => processed but return nothing
Shall we return an error if it cannot find the stream event channel? This might not be 100% accurate.
Not sure how to deal with the 2. under the current design.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possible to do a check at server side, knowing whether it is a normal sever or sse server, then send this info to rust side. In doing so, the rust side will know whether it should reject the requests from non-implemented route. Another benefit is that these rejected requests will never go to python side for processing.
A simple possible implementation is to let users set a class attribute to the sse worker? Only when it is set the
send_stream_event
can be called normally.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This might need to implement an SSE worker class. But passing the type of worker might require a more complex config file than the current command line arguments.