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

Understanding prepare() RuntimeError on StreamResponse object #1807

Closed
mdublin opened this issue Apr 10, 2017 · 2 comments
Closed

Understanding prepare() RuntimeError on StreamResponse object #1807

mdublin opened this issue Apr 10, 2017 · 2 comments
Labels

Comments

@mdublin
Copy link

mdublin commented Apr 10, 2017

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.

@mdublin mdublin changed the title Understanding start() AttributeError on StreamResponse object Understanding prepare() RuntimeError on StreamResponse object Apr 10, 2017
fafhrd91 added a commit that referenced this issue Apr 10, 2017
@fafhrd91
Copy link
Member

I updated exception text,

here is reference to stream response http://aiohttp.readthedocs.io/en/stable/web_reference.html#streamresponse

fafhrd91 added a commit that referenced this issue Apr 12, 2017
@lock
Copy link

lock bot commented Oct 28, 2019

This thread has been automatically locked since there has not been
any recent activity after it was closed. Please open a new issue for
related bugs.

If you feel like there's important points made in this discussion,
please include those exceprts into that new issue.

@lock lock bot added the outdated label Oct 28, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 28, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants