Streaming api.

This commit is contained in:
Roberto Abdelkader Martínez Pérez
2019-03-28 13:17:08 +01:00
parent 7de537db8b
commit debb3dcaf0
+5 -7
View File
@@ -134,7 +134,6 @@ def get_manager(resource, context):
try: try:
async with aiofiles.open(filename, 'rb') as fifo: async with aiofiles.open(filename, 'rb') as fifo:
while True: while True:
print("Reading stream")
chunk = await fifo.read(128) chunk = await fifo.read(128)
if chunk: if chunk:
if not initialized: if not initialized:
@@ -146,8 +145,7 @@ def get_manager(resource, context):
context["stream"] = response context["stream"] = response
await response.prepare(context["request"]) await response.prepare(context["request"])
initialized = True initialized = True
print("initialized!") await response.write(chunk)
await response.write(b"hola")
else: else:
break break
finally: finally:
@@ -216,9 +214,10 @@ class KapowTemplate(Template):
print(code) print(code)
print('-'*80) print('-'*80)
shell_task = await asyncio.create_subprocess_shell(code)
manager_tasks = [asyncio.create_task(v.coro) manager_tasks = [asyncio.create_task(v.coro)
for v in resources.values()] for v in resources.values()]
await asyncio.sleep(0)
shell_task = await asyncio.create_subprocess_shell(code)
await shell_task.wait() # Run the subshell process await shell_task.wait() # Run the subshell process
done, pending = await asyncio.wait(manager_tasks, timeout=1) # XXX: Managers commit changes done, pending = await asyncio.wait(manager_tasks, timeout=1) # XXX: Managers commit changes
@@ -226,7 +225,7 @@ class KapowTemplate(Template):
# print(f"Warning: Resources not consumed ({len(pending)})") # print(f"Warning: Resources not consumed ({len(pending)})")
for task in pending: for task in pending:
task.cancel() task.cancel()
await asyncio.sleep(0)
def create_runner(code): def create_runner(code):
return KapowTemplate(code).run return KapowTemplate(code).run
@@ -243,8 +242,7 @@ def create_context(request):
async def response_from_context(context): async def response_from_context(context):
if context["stream"]: if context["stream"] is not None:
print("is stream!")
await context["stream"].write_eof() await context["stream"].write_eof()
return context["stream"] return context["stream"]
else: else: