Skip to content

Instantly share code, notes, and snippets.

@vxgmichel
vxgmichel / protect.py
Created September 29, 2017 16:30
Protecting against SIGINT using ThreadPoolExecutor
# Common imports
from time import sleep
# Synchronous imports
from concurrent.futures import ThreadPoolExecutor
# Asynchronous imports
# from gevent.threadpool import ThreadPoolExecutor
@vxgmichel
vxgmichel / go-style-generators.md
Last active July 26, 2023 06:50
Go-style generators in asyncio

Go-style generators in asyncio

I've recently been looking into the go concurrency model to see how it compares to asyncio.

An interesting concept caught my attention: go generators.

Generators in Go

@vxgmichel
vxgmichel / aiogo.py
Last active July 5, 2017 14:57
Go-style generators in asyncio
import asyncio
from functools import wraps
def gogenerator(aiterable=None, buffering=0):
def decorator(aiterable):
@wraps(aiterable)
def wrapper(*args, **kwargs):
return go(aiterable(*args, **kwargs), buffering)
return wrapper
@vxgmichel
vxgmichel / twindragons.py
Created February 2, 2017 15:38
Generate a twin dragon fractal using pygame
import pygame
import random
import imageio
from fractions import gcd
lcm = lambda x, y: x * y // gcd(x, y)
def random_color():
r = lambda: random.randint(0, 255)
@vxgmichel
vxgmichel / udpproxy.py
Created February 2, 2017 10:05
UDP proxy server using asyncio
"""UDP proxy server."""
import asyncio
class ProxyDatagramProtocol(asyncio.DatagramProtocol):
def __init__(self, remote_address):
self.remote_address = remote_address
self.remotes = {}
@vxgmichel
vxgmichel / monitoring.py
Created January 10, 2017 15:32
Command line interface for monitoring asyncio tasks
"""Command line interface for monitoring asyncio tasks."""
import os
import signal
import asyncio
import argparse
import traceback
import linecache
from itertools import count
@vxgmichel
vxgmichel / primes.py
Created November 22, 2016 08:48
Fast eratosthenes prime generator with a cached wheel
"""Fast eratosthenes prime generator with a cached wheel"""
import sys
import time
import operator
from itertools import cycle, chain, accumulate, islice, count
from functools import lru_cache, reduce, partial
from contextlib import contextmanager
CACHE_LEVEL = 5
@vxgmichel
vxgmichel / multitasking.py
Last active November 2, 2016 16:42
Multitasking example using coroutines
import itertools
import collections
def run(tasks):
# Prepare
results = {}
count = itertools.count()
queue = collections.deque()
for task in tasks:
@vxgmichel
vxgmichel / aioudp.py
Last active March 16, 2026 20:09
High-level UDP endpoints for asyncio
"""Provide high-level UDP endpoints for asyncio.
Example:
async def main():
# Create a local UDP enpoint
local = await open_local_endpoint('localhost', 8888)
# Create a remote UDP enpoint, pointing to the first one
@vxgmichel
vxgmichel / asyncio_executor.py
Last active September 12, 2020 12:23
An executor to run asyncio coroutines from synchronous code
"""Provide an executor to run asyncio coroutines in a shadow thread."""
import asyncio
from threading import Thread
from concurrent.futures import Executor
class AsyncioExecutor(Executor):
def __init__(self):