Skip to content

Instantly share code, notes, and snippets.

@Sharpz7
Created June 13, 2024 02:33
from fastapi import FastAPI, BackgroundTasks
import requests
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry
import time
import asyncio
app = FastAPI()
# Configure your parameters
URL = 'https://your.server.address/file'
TIMEOUT = 10 # seconds
RETRY_STRATEGY = Retry(
total=5, # Total number of retries
backoff_factor=1, # A backoff factor to apply between attempts
status_forcelist=[429, 500, 502, 503, 504], # Retry on these status codes
method_whitelist=["HEAD", "GET", "OPTIONS"] # Methods to retry
)
# Set up a session with the retry strategy
session = requests.Session()
adapter = HTTPAdapter(max_retries=RETRY_STRATEGY)
session.mount("https://", adapter)
session.mount("http://", adapter)
# Simulated Heartbeater
def heartbeater():
while True:
print("Heartbeat...")
time.sleep(5)
# Simulated I/O Blocking Task for downloading a file
def download_file(url, timeout):
try:
# Stream the response to handle large files
with session.get(url, timeout=timeout, stream=True) as response:
response.raise_for_status() # Raise an error for bad responses
with open('downloaded_file', 'wb') as file:
for chunk in response.iter_content(chunk_size=8192):
file.write(chunk)
print("Download completed successfully.")
except requests.exceptions.Timeout:
print("The request timed out.")
except requests.exceptions.RequestException as e:
print(f"An error occurred: {e}")
@app.on_event("startup")
async def startup_event():
# Starting Heartbeater in a separate thread
import threading
threading.Thread(target=heartbeater, daemon=True).start()
@app.post("/run-download-task")
async def run_download_task(background_tasks: BackgroundTasks):
background_tasks.add_task(download_file, URL, TIMEOUT)
return {"message": "Download task started"}
# To run the server, use: uvicorn this_script_name:app --reload
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment