"Generator raised StopIteration" error when sending static file from filesystem
miguelgrinberg opened this issue · 1 comments
Discussed in #142
Originally posted by ned-pcs June 6, 2023
I really like Microdot (which greatly resembles Sinatra, which I've used a lot in Ruby).
I'm using the latest version of Microdot-asyncio on an ESP32.
I would like to pre-compile my static files into frozen strings (and use pkg_resources.resource_stream('static', 'filename')
).
I've tried both that method (which returns an io.BytesIO
object), and serving these files from my filesystem.
I've tried returning an opened file handle in the Response
, as well as using send_file()
.
In all of the above cases, what I get is a RuntimeError: generator raised StopIteration
exception:
Send file /lib/static/js/toast.js
Response header {'Content-Type': 'application/javascript', 'Cache-Control': 'max-age=86400'}
Traceback (most recent call last):
File "uasyncio/core.py", line 1, in run_until_complete
File "microdot/microdot_asyncio.py", line 269, in serve
File "microdot/microdot_asyncio.py", line 337, in handle_request
File "microdot/microdot_asyncio.py", line 155, in write
RuntimeError: generator raised StopIteration
Templates (using utemplate
) and static strings work fine in the Response
s.
Here's how I'm trying to return an opened file:
f = open(filename, 'rb')
return Response(body=f, status_code=status_code, headers=response_header)
Using send_file()
:
return send_file(filename, status_code=status_code, content_type=content_type)
Sending from compiled resources:
f = pkg_resources.resource_stream('static', fname)
return Response(body=f, status_code=status_code, headers=response_header)
or sending from compiled resources using send_file()
:
f = pkg_resources.resource_stream('static', fname)
return send_file(filename, status_code=status_code, content_type=content_type, stream=f)
Any suggestions?
I believe Microdot 2 (just released today) addresses this problem. If you get a chance to upgrade, please let me know if there are any remaining issues.