Skip to content

Instantly share code, notes, and snippets.

@AguacateVelarde
Created June 12, 2023 23:39
Show Gist options
  • Save AguacateVelarde/f490bf7fbe55279c2879cab924bb560f to your computer and use it in GitHub Desktop.
Save AguacateVelarde/f490bf7fbe55279c2879cab924bb560f to your computer and use it in GitHub Desktop.
Abort signal with yield propagated to backend
(async () => {
const controller = new AbortController();
const signal = controller.signal;
const response = await fetch('http://127.0.0.1:8000/hi', { signal });
const reader = response.body.getReader()
while (true) {
const { done, value } = await reader.read()
if (done) {
break
}
var string = new TextDecoder().decode(value);
console.log(string)
setInterval(() => {
controller.abort('Auto-killed in the frontend');
console.log('Aborted!');
}, 2000)
}
})()
from fastapi import FastAPI, HTTPException
from fastapi.responses import StreamingResponse
import time
app = FastAPI()
def fake_data_streamer():
while True:
yield b'some fake data\n\n'
print('faked')
time.sleep(0.5)
@app.get("/hi")
def hi():
return StreamingResponse(fake_data_streamer(), media_type='text/event-stream')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment