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

Added streaming jsonl format #860

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

traverseda
Copy link

Right now there doesn't seem to be any good way to stream chunks of content out of hug. That would be useful for all kinds of things, things like chat servers, event subscription, or just dealing with really large chunks of data.

This would enable a very simple "long-polling" style of streaming, as opposed to something more complex like websockets. Some deployments might be vulnerable to slowloris style denial-of-service attacks when combined with long-polling, but this is something like the third time I've written this for a project so I figure it's probably worth including. the ASGI servers shouldn't have any problems with it.

@traverseda
Copy link
Author

traverseda commented Jun 16, 2020

It lets you do things like

import hug, time
@hug.get(output=hug.output_format.json_streaming_lines)
def foo():
    yield {'biz','baz\n','bar\n'}
    yield range(0,30)
    yield {'key':'value'}
    itercount=0
    while True:
        yield {'key':itercount}
        itercount+=1
        time.sleep(1)

without having to wait for that while True loop to finish. Json-lines is an informal spec, but tools like jq have no problem dealing with it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant