omnilib / aiosqlite

asyncio bridge to the standard sqlite3 module
https://aiosqlite.omnilib.dev
MIT License
1.16k stars 93 forks source link

Spawn threads as daemon so program will exit with main thread. #299

Open ethan-vanderheijden opened 3 months ago

ethan-vanderheijden commented 3 months ago

Description

Currently, the threads spawned by aiosqlite's Connection are non-daemon threads, so if you don't explicitly close the connection, the program will stay alive after the main thread exits.

The following aiosqlite code will never exit:

import asyncio
import aiosqlite

async def create_db():
    db = await aiosqlite.connect('../database/partners_list.db')

asyncio.run(create_db())

But the equivalent sqlite code does exit:

import sqlite3

def create_db():
    db = sqlite3.connect('../database/partners_list.db')

create_db()

In my use case, I want to use aiosqlite with Quart, but because I don't want to create a database connection + thread for every request, I will do all my databasing with a single, global connection. However, I'm not sure when the web server will exit and I don't want the program to hang if the database connection hasn't been explicitly closed.

Details