allow a cllback in streamer
This commit is contained in:
1 parent
6e9c8212e2
commit
e87378b07f
4 files changed
+25
-22
No files matched your search
+1
-1
Submodule gluon/packages/dal updated: 77e0d3f386...79ec4e99d0.
+19
-16
@@ -25,22 +25,25 @@ regex_stop_range = re.compile('(?<=\-)\d+')
|
||||
|
||||
DEFAULT_CHUNK_SIZE = 64 * 1024
|
||||
|
||||
def streamer(stream, chunk_size=DEFAULT_CHUNK_SIZE, bytes=None):
|
||||
offset = 0
|
||||
while bytes is None or offset < bytes:
|
||||
if not bytes is None and bytes - offset < chunk_size:
|
||||
chunk_size = bytes - offset
|
||||
data = stream.read(chunk_size)
|
||||
length = len(data)
|
||||
if not length:
|
||||
break
|
||||
else:
|
||||
yield data
|
||||
if length < chunk_size:
|
||||
break
|
||||
offset += length
|
||||
stream.close()
|
||||
|
||||
def streamer(stream, chunk_size=DEFAULT_CHUNK_SIZE, bytes=None, callback=None):
|
||||
try:
|
||||
offset = 0
|
||||
while bytes is None or offset < bytes:
|
||||
if not bytes is None and bytes - offset < chunk_size:
|
||||
chunk_size = bytes - offset
|
||||
data = stream.read(chunk_size)
|
||||
length = len(data)
|
||||
if not length:
|
||||
break
|
||||
else:
|
||||
yield data
|
||||
if length < chunk_size:
|
||||
break
|
||||
offset += length
|
||||
finally:
|
||||
stream.close()
|
||||
if callback:
|
||||
callback()
|
||||
|
||||
def stream_file_or_304_or_206(
|
||||
static_file,
|
||||
|
||||
Reference in new issue
Block a user