Skip to content

Instantly share code, notes, and snippets.

View vytas7's full-sized avatar

Vytautas Liuolia vytas7

View GitHub Profile
#!/usr/bin/python3
import sys
import asyncio
import greenlet
class AsyncIoGreenlet(greenlet.greenlet):
def __init__(self, driver, fn):
greenlet.greenlet.__init__(self, fn, driver)
self.driver = driver
@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 / 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
@kgriffs
kgriffs / falcon-3.0-roadmap.md
Last active May 10, 2021 22:23
Falcon 3.0 Roadmap

Falcon 3.0 Roadmap

3.0.0a1

  • Review/Merge the ASGI implementation - PR #1573
  • Review/Merge native form parsing (WSGI only) - PR #1549
  • Fix doc formatting issue (see also comment on PR #1566)
  • Get tests to pass on Windows
  • Add windows gate to Travis CI
import falcon
class TestResponse():
def on_get(self, req, resp):
resp.body = "Success"
resp.content_type = falcon.MEDIA_TEXT
application = falcon.API()
# http://localhost/item/19/01/01/Item-190101-59559ee7-e661-43c8-9da1-0ab83b9e0d8e.json
application.add_route("/item/{y:int}/{m:int}/{d:int}/Item-{yymmdd:int}-{item_id:uuid}.json", TestResponse())
@kgriffs
kgriffs / async-interface-proposal.py
Last active August 20, 2020 23:00
Falcon ASGI Interface Proposal
class ChunkyBacon():
def __init__(self, baconator):
self._baconator = baconator
async def on_get(self, req, resp, bacon_id=None):
# Use JSON serializer to send it back in one chunk
resp.media = await self._baconator.get_bacon(bacon_id)
resp.set_header('X-Powered-By', 'Bacon')
@hynek
hynek / gunicorn_callbacks_for_worker_id.py
Last active March 6, 2024 04:55
This is an attempt to emulate uWSGI’s uwsgi.worker_id() that ensures that I have worker IDs from 1…--workers which is useful in logging and instrumentation where changing PIDs are annoying.
import os
def on_starting(server):
"""
Attach a set of IDs that can be temporarily re-used.
Used on reloads when each worker exists twice.
"""
server._worker_id_overload = set()