Skip to content

Instantly share code, notes, and snippets.

View zzzeek's full-sized avatar
💭
SQLAlchemy 2.0 is released!

Michael Bayer zzzeek

💭
SQLAlchemy 2.0 is released!
View GitHub Profile
@zzzeek
zzzeek / test.py
Last active January 1, 2021 16:54
async sqlalchemy w/ yappi
import asyncio
import contextlib
import random
import yappi
# install sqlalchemy from github master:
# pip install git+https://github.com/sqlalchemy/sqlalchemy/
from sqlalchemy import cast
from sqlalchemy import Column
@zzzeek
zzzeek / A1_all_sqlalchemy.py
Last active January 27, 2022 09:10
comparing SQLAlchemy 1.4 async to "databases"
import asyncio
import contextlib
import cProfile
import io
import pstats
import random
from sqlalchemy import cast
from sqlalchemy import Column
from sqlalchemy import Integer
@zzzeek
zzzeek / sar.py
Created August 6, 2020 15:43
replace select([]) with select()
import sys
import re
reg = re.compile(
r'select\([\s]*\[([^\[\]]+?)\][\s]*\)', re.S
)
for file in sys.argv[1:]:
with open(file, 'r') as file_:
newfile = reg.sub(
lambda m: "select(%s)" % m.group(1),
@zzzeek
zzzeek / asyncio_greenlet_part_11.py
Created July 10, 2020 20:48
more greenlet designs
"""This is a simpler version of the greenlet
example at https://gist.github.com/zzzeek/4e89ce6226826e7a8df13e1b573ad354
Instead of the "await" keyword, we use the "await_()" function to interact with
the greenlet context. the greenlet context itself is 23 lines of code right
here.
"""
import asyncio
@zzzeek
zzzeek / simpler_asyncio.py
Last active November 11, 2024 17:11
a simpler version of async->greenlet->async written by @CaselIT
"""This is a simpler version of the greenlet
example at https://gist.github.com/zzzeek/4e89ce6226826e7a8df13e1b573ad354
Instead of the "await" keyword, we use the "await_()" function to interact with
the greenlet context. the greenlet context itself is 23 lines of code right
here.
"""
import asyncio
# https://gist.github.com/zzzeek/f5ac058e30e192f11160ffb52f5224fa incorporated by reference
import asyncio
import contextvars
import sys
import greenlet
current_greenlet_context = contextvars.ContextVar("current greenlet context")
@zzzeek
zzzeek / test.py
Created July 8, 2020 14:56
cancellation w/ asyncio -> greenlet -> asyncio
import asyncio
import contextvars
import sys
import asyncpg
import greenlet
current_greenlet_context = contextvars.ContextVar("current greenlet context")
@zzzeek
zzzeek / foo.py
Created July 8, 2020 04:13
do we need to worry about nesting of greenlet_spawn / await_()? seems like not
import asyncio
import contextvars
import sys
import greenlet
current_greenlet_context = contextvars.ContextVar("current greenlet context")
@zzzeek
zzzeek / msg373145.rst
Last active October 22, 2022 12:36
asyncio support for SQLAlchemy (and Flask, and any other blocking-IO library)

This is a cross post of something I just posted on the Python bug tracker at https://bugs.python.org/msg373145.

I seem to have two cents to offer so here it is. An obscure issue in the Python bug tracker is probably not the right place for this so consider this as an early draft of something that maybe I'll talk about more elsewhere.

> This basically divides code into two islands - async and non-async

@zzzeek
zzzeek / asyncio_plus_greenlet.py
Last active July 5, 2023 16:32
An asyncio program that runs rows into a Postgresql database, using blocking style code to actually run the database commands
"""This program is exactly the same as that of
https://gist.github.com/zzzeek/33943060f7a08cf9e82bf8df1f0f75de ,
with the exception that the add_and_select_data function is written in
synchronous style.
UPDATED!! now includes refinements by @snaury and @Caselit . SIMPLER
AND FASTER!!