Skip to content

Instantly share code, notes, and snippets.

@arthur-tacca
arthur-tacca / abort.dart
Last active April 9, 2026 13:31
Proof of concept for Dart cancel token class
// Moved to:
// https://github.com/arthur-tacca/dart-cancel-examples/blob/main/lib/abort.dart
@arthur-tacca
arthur-tacca / dart_cancel_tokens.md
Last active April 9, 2026 14:08
Proposal to Add Cancellation Tokens to Dart

Proposal to Add Cancellation Tokens to Dart

Part of what makes Dart great is its excellent async support, but it is in dire need of a unified mechanism for cancellation.

Currently, there are a variety of inconsistent cancellation APIs for different operations (e.g. ConnectionTask for Socket.startConnect(), StreamSubscription.cancel(), HttpClientRequest.abort()), and some lack any cancellation support at all (most notoriously, HttpClient.getUrl() as noted in dart-lang/sdk#51267). The facilities to write your own cancellable operations (CancelableCompleter to build a [CancelableOperation

@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, '