Skip to content

Instantly share code, notes, and snippets.

@arthur-tacca
arthur-tacca / taskgroups.py
Last active March 21, 2024 15:56
Possible modifications to asyncio.TaskGroup class
# Adapted with permission from the EdgeDB project;
# license: PSFL.
__all__ = ("TaskGroup",)
from . import events
from . import exceptions
from . import tasks
@arthur-tacca
arthur-tacca / trio_cancel_race.py
Created March 14, 2024 12:30
Demo of trio task waking without exception from `await trio.something()` despite being in a recently-cancelled cancel scope
# Question: if we cancel a scope, and another task is active in that scope, is it *guaranteed* that
# it will raise CancelledError when it next wakes? (Even if it is already queued to wake for some
# other reason.)
#
# The answer, at least in practice, is no: in this example, send_and_cancel() sends some data to
# a memory channel then cancels a scope, and then receive() (which is running in that scope) wakes
# with the first value that was sent.
#
# On my computer, this code produces the following output:
#
@arthur-tacca
arthur-tacca / asyncio_taskgroup_missed_cleanup.py
Created March 3, 2024 19:32
Illustration of missed cleanup in an asyncio task group
# Illustration of missed cleanup in an asyncio task group due to the way it
# cancels a task that has not started by never running it at all. See issue:
# https://github.com/python/cpython/issues/116048
#
# When run on my computer, I usually get the output:
#
# Total connections created: 3300
# Connections left unclosed: 0
#
# i.e., all connections that were created were cleaned up. But sometimes I get:
@arthur-tacca
arthur-tacca / stream_results_nursery.py
Last active March 23, 2024 10:41
Results-gathering nursery wrapper, using aioresult.ResultCapture for the results
# aioresult variant of StreamResultsNursery
# Original idea by smurfix: https://gist.github.com/smurfix/0130817fa5ba6d3bb4a0f00e4d93cf86
# Fixed non-aioresult version: https://gist.github.com/arthur-tacca/6c676a21d0dcc0582edb50c9c2aa3e3c
from collections import deque
import math
from aioresult import ResultCapture
import trio
@arthur-tacca
arthur-tacca / wrap.py
Last active February 14, 2024 17:08 — forked from smurfix/wrap.py
Trio: results-gathering nursery wrapper
# Original idea by smurfix: https://gist.github.com/smurfix/0130817fa5ba6d3bb4a0f00e4d93cf86
# aioresult variant: https://gist.github.com/arthur-tacca/5c717ae68ac037e72ae45fd1e9ca1345
from collections import deque
import math
import trio
class StreamResultsNursery:
def __init__(self, max_running_tasks=math.inf):
@arthur-tacca
arthur-tacca / trio_result_capture.py
Last active February 12, 2024 13:31
ResultCapture class for Trio - now adapted into aioresult: https://github.com/arthur-tacca/aioresult
#
# -- An improved version of this code is now in the aioresult library --
#
# https://github.com/arthur-tacca/aioresult
#
#
# - aioresult has a ResultCapture class similar to the one below
# - It also has a Future class that allows manually setting the result, which shares a base class with ResultCapture
# - There is a utility function with a similar effect to StartableResultCapture below (but much simpler)
# - There are utility functions wait_any(), wait_all() and to_channel() (which also work with Future instances)
// Like solution 4, but uses a dummy Node object for leaf nodes instead of *this
// The implicitly declared constructor is fine because we don't need to fix up *this
class Node
{
public:
Node(int d, Pool& pool)
: level{d}
, l{d > 0 ? pool.emplace_back(Node(d - 1, pool)) : dummy}
, r{d > 0 ? pool.emplace_back(Node(d - 1, pool)) : dummy}
@arthur-tacca
arthur-tacca / setup.py
Last active July 14, 2020 09:14
setup.py for caffe
#!/usr/bin/env python
from setuptools import setup
setup(
name="caffe",
version='1.0rc3',
url='https://github.com/BVLC/caffe',
license='BSD',
description=('Caffe is a deep learning framework made with expression, '
@arthur-tacca
arthur-tacca / test_rabbit.py
Created September 15, 2016 14:18
Example of bug in pika code for bug report
#!/usr/bin/env python3
import pika
from uuid import uuid4
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
queue_name = "test_queue_abc"
queue_result = channel.queue_declare(queue=queue_name)