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 / 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 October 12, 2023 13:53
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!!
@zzzeek
zzzeek / plain_asyncio.py
Last active November 13, 2021 00:01
An asyncio program that runs rows into a Postgresql database
"""A plain asyncio program that uses asyncpg to run some rows into a table and
select them.
This is a "control" program which we will compare to the one which uses calls
from an implicit IO greenlet at
https://gist.github.com/zzzeek/4e89ce6226826e7a8df13e1b573ad354.
Performance against a PG database over a wired network
Ran 40000 records in 40 concurrent requests, Total time 5.560306
@zzzeek
zzzeek / async_to_greenlet_to_async.py
Last active January 22, 2024 15:35
Write a program in asyncio, that calls into a library that knows nothing about asyncio, which then calls out to a database adapter that is only asyncio
"""Proof of concept of an adapter that will wrap a blocking IO library in
greenlets, so that it can be invoked by asyncio code and itself make calls out
to an asyncio database library.
hint: any Python ORM or database abstraction tool written against the DBAPI
could in theory allow asyncio round trips to a pure async DB driver like
asyncpg.
The approach here seems too simple to be true and I did not expect it to
collapse down to something this minimal, so it is very possible I am totally