Created
June 12, 2023 23:39
-
-
Save AguacateVelarde/f490bf7fbe55279c2879cab924bb560f to your computer and use it in GitHub Desktop.
Abort signal with yield propagated to backend
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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) | |
} | |
})() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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