Skip to content
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

Support and document server-sent-events #1149

Closed
RobertLucian opened this issue Jun 22, 2020 · 0 comments
Closed

Support and document server-sent-events #1149

RobertLucian opened this issue Jun 22, 2020 · 0 comments
Labels
blocked Blocked on another task or external event enhancement New feature or request

Comments

@RobertLucian
Copy link
Member

RobertLucian commented Jun 22, 2020

Motivation

This can be useful for APIs that want to stream results in real time, before returning from predict() (e.g. text generation models). This is an alternative to websockets if data only needs to be transferred to the client.

Additional context

This is blocked on #1637

Examples

These examples have been tested using quay.io/cortexlabsdev/python-predictor-cpu-async:0.23.0, which has #1637 merged in.

import asyncio
from sse_starlette.sse import EventSourceResponse

async def numbers(minimum, maximum):
    for i in range(minimum, maximum + 1):
        await asyncio.sleep(0.9)
        yield dict(data=i)

class PythonPredictor:
    def __init__(self, config):
        pass

    async def predict(self, payload):
        generator = numbers(1, 5)
        return EventSourceResponse(generator)

with a shell command:

import asyncio
from sse_starlette.sse import EventSourceResponse

async def run_command():
    command = "echo test1 && sleep 2 && echo test2 && sleep 2 && echo test3 && echo done"

    proc = await asyncio.create_subprocess_shell(
        command, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE
    )

    while True:
        output = await proc.stdout.readline()
        if not output:
            return
        yield output.decode().strip()

class PythonPredictor:
    def __init__(self, config):
        pass

    async def predict(self, payload):
        return EventSourceResponse(run_command())
@RobertLucian RobertLucian added the enhancement New feature or request label Jun 22, 2020
@deliahu deliahu added the blocked Blocked on another task or external event label Nov 26, 2020
@deliahu deliahu changed the title Use server-sent-events as predictions are made Support and document server-sent-events Nov 26, 2020
@deliahu deliahu closed this as completed Apr 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
blocked Blocked on another task or external event enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants