Skip to content

Instantly share code, notes, and snippets.

View johntlangton's full-sized avatar

John T. Langton johntlangton

View GitHub Profile
@johntlangton
johntlangton / sqlalchemy_asyncio_example.py
Created August 26, 2022 14:27
Example of executing multiple queries concurrently (in parallel) with sqlalchemy core using sqlalchemy.ext.asyncio, the asyncio extension
"""
This seems like such a common use case, but I couldn't find any examples after looking through all of the documentation
of SQLAlchemy and random spots on the internet. Posting in case it's helpful to others.
You can run this on any data, just have to update the queries. The code compares serial and parallel query execution.
It shows the number of connections used from the engine's pool by each (serial is one, parallel is 5). I did the view creation
just trying to hit queries that would take time, but any queries will do.
There are other recipes for parallel execution that may be more applicable to your use case.
"""