Skip to content

Instantly share code, notes, and snippets.

View inchoate's full-sized avatar

Jason Vertrees inchoate

View GitHub Profile
@inchoate
inchoate / running-out-of-db-connections-in-fastapi.md
Created September 2, 2024 15:57
How to Fix Running out of DB Connections in FastAPI

Ran Out of Database Connections on Your SQLModel App? Here’s a Quick Fix!

Problem:

You might have run into a situation where your SQLModel app unexpectedly runs out of database connections. If that sounds familiar, you probably have something like this in your code:

# utils/db.py
def get_db():
 """A utility function to grab a database session."""
@inchoate
inchoate / moving-to-fastapi-async.md
Created September 24, 2024 20:15
Moving to FastAPI Async

Moving from Sync to Async in FastAPI with SQLModel? Here's What You Need to Know!

Switching from a synchronous setup to an asynchronous one in your FastAPI/SQLModel app can bring some challenges. When making this switch, several important details need attention, especially around async sessions, avoiding misuse of await, and correctly handling relationships between tables.

A Typical Synchronous Setup:

# sync version
engine = create_engine(database_settings.pg_connection_string)
session = sessionmaker(bind=engine)
@inchoate
inchoate / download_site.md
Last active October 10, 2024 13:13
Simple Site Downloader
#!/bin/zsh

# Usage:
#     ./download_site.sh {URL}


url=$1
output_dir="./sites"
@inchoate
inchoate / syncwrap.md
Created April 4, 2025 12:16
Syncwrap - small function to wrap your async code to run it safely in a sync context

syncwrap

syncwrap is a light and elegant solution for running asynchronous functions in a synchronous environment. This helper neatly packages your async tasks, handling event loop creation and cleanup so you don’t have to.

Code

import asyncio

def run_async_safely(async_func, *args, **kwargs):