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):
@inchoate
inchoate / Makefile.pipsucks
Created May 21, 2025 20:02
Simple uv wrapper to keep uv-based project dependencies in sync.
# Makefile for managing Python dependencies with uv
#
# I built this because I got tired of juggling uv, pip, pyproject.toml,
# and requirements.txt by hand. This Makefile gives me fast, reproducible
# dependency management without forgetting to freeze, clean, or sync anything.
#
# Were I smarter I'd probably not have to do this. But, alas, I'm not.
#
# Key commands:
#
@inchoate
inchoate / cycle_stack.sh
Created May 29, 2025 15:53
Shell script to cycle (down, rebuild, up) specific services in a docker compose stack
#!/bin/bash
# cycle_stack.sh - Script to tear down and rebuild Docker Compose services
# Usage: ./cycle_stack.sh [service_name1] [service_name2] ...
set -e # Exit on error
# Check if Docker Compose is installed
if ! command -v docker compose &>/dev/null; then
echo "Error: docker compose is not installed or not in PATH"