Skip to content

Instantly share code, notes, and snippets.

View sorcio's full-sized avatar

Davide Rizzo sorcio

View GitHub Profile
@sorcio
sorcio / trio_graceful_shutdown.py
Created July 31, 2018 17:56
graceful service shutdown with Trio
from itertools import count
import signal
from async_generator import asynccontextmanager
import trio
class ShutdownHandler:
def __init__(self):
self.event = trio.Event()
@sorcio
sorcio / triolet.py
Last active May 15, 2018 17:10
sync -> trio magic wrapper (proof of concept)
from contextlib import contextmanager
from functools import partial
import threading
import types
from queue import Queue
import sys
import outcome
import trio
END_OF_QUEUE = object()
class QueueTerminated(Exception): pass
class ObjectSender:
def __init__(self, queue):
self.queue = queue
self.senders = 0
@sorcio
sorcio / queue_bench.py
Last active March 19, 2018 14:45
benchmarking trio.Queue
import timeit
import trio
class Stop(Exception):
pass
async def benchmark1(qsize):
@pytest.mark.trio
async def test_worker_thread_autojump(nursery, autojump_clock):
def slowish_task():
for i in range(1000000):
pass
return 'foo'
async def async_run_task(queue):
result = await trio.run_sync_in_worker_thread(slowish_task)
await queue.put(result)
@sorcio
sorcio / randre.py
Last active July 3, 2017 16:54 — forked from anonymous/randre.py
Exploiting Python sre_parse internal to generate random text with regular expressions
import random
import sre_parse
MAX_ALLOWED_REPEATS = 100
def randre(re_text):
pattern = sre_parse.parse(re_text)
return Generator().gen_pattern(pattern)
@sorcio
sorcio / keybase.md
Created January 21, 2015 01:23
My Keybase proof

Keybase proof

I hereby claim:

  • I am sorcio on github.
  • I am davider (https://keybase.io/davider) on keybase.
  • I have a public key whose fingerprint is 0021 FE8B 13A5 5D6D 8218 B06C 8E3B 5D19 94BC D692

To claim this, I am signing this object:

@sorcio
sorcio / property.py
Last active December 17, 2015 01:59
An implementation of Python properties akin to Python property() built-in for the purpose of showing descriptors. It has some limitations: 1) it doesn't implement the del operation 2) decorator syntax can only be used for read-only properties. Example usage is included (see Foo class below). Read the full article: http://daviderizzo.com/2013/05/…
class PythonProperty(object):
"""
A property-like descriptor written in Python.
Simplifications from built-in property():
- it doesn't implement the del operation
- decorator syntax can only be used for read-only properties
"""
def __init__(self, getter, setter=None):