Skip to content

Instantly share code, notes, and snippets.

import trio
# Process pool based on concurrent.futures
import concurrent.futures
class TrioProcessExecutor(trio.abc.AsyncResource):
def __init__(self, max_workers=None):
self._executor = concurrent.futures.ProcessPoolExecutor(max_workers=max_workers)
async def run_sync(self, fn, *args):
fut = self._executor.submit(fn, *args)
from contextlib import contextmanager
import trio
class MultiCancel:
def __init__(self):
self.scopes = set()
self.cancelled = False
@contextmanager
def apply(self):
@attr.s
class TrioFuture:
_result = attr.ib(default=None)
_finished = attr.ib(default=attr.Factory(trio.Event))
def set_result(self, result):
assert not self._finished.is_set()
self._result = result
self._finished.set()
# pip install cryptography
import cryptography
import os
import numpy as np
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend
# Cribbing from
from collections import deque, OrderedDict
import trio
class MultiGetQueue:
def __init__(self, max_size):
self._max_size = max_size
# {task: abort func}
# probably should make Task._abort_func public, or maybe even change
# the reschedule code to call the abort func in general?
self._get_wait = OrderedDict()

Keybase proof

I hereby claim:

  • I am njsmith on github.
  • I am njs (https://keybase.io/njs) on keybase.
  • I have a public key ASDPDkjaSUnDcWcxBQEDBot3TR0Oca46Q_p_enJiX_v7BAo

To claim this, I am signing this object:

@njsmith
njsmith / ssl-close-notify.py
Last active June 14, 2017 09:03
ssl discrepancy between cpython and pypy
# Scenario:
# - TLS connection is set up successfully
# - client sends close_notify then closes socket
# - server receives the close_notify then attempts to send close_notify back
#
# On CPython, the last step raises BrokenPipeError. On PyPy, it raises
# SSLEOFError.
#
# SSLEOFError seems a bit perverse given that it's supposed to mean "EOF
# occurred in violation of protocol", and the client's behavior here is
@njsmith
njsmith / multierrer.py
Last active June 13, 2017 03:34
MultiError examples from my 2017 python language summit talk (slides: https://vorpus.org/~njs/misc/trio-language-summit-2017.pdf)
import trio
async def main():
async with trio.open_nursery() as nursery:
nursery.spawn(f1)
nursery.spawn(f2)
async def f1():
raise KeyError
@njsmith
njsmith / gist:0c7c8e76154cded7e554e1cff4235699
Created May 19, 2017 07:36
modules imported at startup under easy_install but not pip
argparse
atexit
bisect
_bisect
_compat_pickle
difflib
glob
http
http.client
ipaddress
import abc
class MyABC(abc.ABC):
@abc.abstractmethod
def foo(self, bar):
pass
def foo(bar):
return bar + 1