Skip to content

Understanding prepare() RuntimeError on StreamResponse object #1807

Closed
@mdublin

Description

I am attempting to build a really simple server endpoint that streams some byte data (supposed to response to a Range request from a client) using an instance of the web.StreamResponse class. I know in the older versions, start() is required before write(), but is prepare() meant to replace start() ?

# simple func to provide byte data
def bytesource():
    videofile = open('bytedump.txt', 'rb')
    byteload = videofile.read()
    return byteload[10:2000] 

# basic view intended to provide byte data
async def bytedata(request):
    stream = web.StreamResponse(status=200, reason='OK')

    stream.headers['Content-Type'] = 'text/html; charset=utf-8'
    stream.headers['Cache-Control'] = 'no-cache'
    stream.headers['Connection'] = 'keep-alive'
    stream.headers['Accept-Ranges'] = 'bytes'

    #stream.start(request)
    #stream._start(request)
    #stream.prepare(request)

    msg= bytesource()
    stream.write(msg)

    stream.write_eof()
    return stream

If I uncomment out stream.prepare(request), I get back:

RuntimeError: Cannot call write() before start()

If I try stream.start(request) I obviously get:

AttributeError: 'StreamResponse' objet has no attribute 'start'

Finally however, if I try: stream._start(request) everything works, and I get my byte data response sent to the client, and everything is ok.

But clearly, I am not meant to use a private class method here, right? What am I doing wrong or what am I missing in my understanding of how to implement such a view correctly?

Thank you for any help you can provide.

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions