Skip to content

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.

1 participant