Skip to content

Instantly share code, notes, and snippets.

@sairamkrish
Created August 5, 2020 19:04
Show Gist options
  • Save sairamkrish/2120a3233dd4913cc9e2da99228b6056 to your computer and use it in GitHub Desktop.
Save sairamkrish/2120a3233dd4913cc9e2da99228b6056 to your computer and use it in GitHub Desktop.
import asyncio
'''
Get status as an event generator
'''
status_stream_delay = 5 # second
status_stream_retry_timeout = 30000 # milisecond
async def status_event_generator(request, param1):
previous_status = None
while True:
if await request.is_disconnected():
logger.debug('Request disconnected')
break
if previous_status and previous_status['some_end_condition']:
logger.debug('Request completed. Disconnecting now')
yield {
"event": "end",
"data" : ''
}
break
current_status = await compute_status(param1)
if previous_status != current_status:
yield {
"event": "update",
"retry": status_stream_retry_timeout,
"data": current_status
}
previous_status = current_status
logger.debug('Current status :%s', current_status)
else:
logger.debug('No change in status...')
await asyncio.sleep(status_stream_delay)
@k2ev
Copy link

k2ev commented Apr 3, 2022

Whats purpose of status_stream_retry_timeout ? It just seems to be passed in event data. Is client side suppose to handle it in anyway?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment