This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Run a trio example mixing cancellation and exceptions. | |
Shows that the behavior can be hard to predict since it depends on factors like: | |
- whether a nursery is involved | |
- whether a checkpoint is performed after the raise | |
Produces the following output: | |
=========== cancel_and_raise =========== |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
A decorator to turn a generator into a cached, stackless recursive function. | |
The yield keyword is used to send the arguments to pass to the recursive | |
function and retrieve the return value, i.e: | |
@stackless | |
def fibonacci(n): | |
if n < 2: | |
return n |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Automatically bisect a given function, without providing upper or lower bounds. | |
The provided function is expected to: | |
- take an integer as single argument | |
- be defined for all integers (positive and negative) | |
- return a truth value | |
- contain a single transition (either true to false or false to true) | |
The argument corresponding to the true value of the transition is returned. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
""" | |
Theoretical solver of the LCS35 Time Capsule Crypto-Puzzle | |
See: https://people.csail.mit.edu/rivest/lcs35-puzzle-description.txt | |
Example usage: | |
% time ./timecapsule.py -t 100000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Results: | |
1.77s call test_trio.py::test_to_aiter[run each next call in a separate thread] | |
1.29s call test_trio.py::test_to_aiter[run iteration in a single thread (buffer_size=inf)] | |
1.28s call test_trio.py::test_to_aiter[run iteration in a single thread (buffer_size=0)] | |
1.28s call test_trio.py::test_to_aiter[run iteration in a single thread (buffer_size=1)] | |
0.45s call test_trio.py::test_to_aiter[run the iteration and sleep(0) between each item] | |
0.02s call test_trio.py::test_to_aiter[run iteration in a single thread with batches] | |
0.01s call test_trio.py::test_to_aiter[run each batch iteration in a separate thread] | |
0.00s call test_trio.py::test_to_aiter[run the iteration in a thread and return a list] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import trio | |
import pytest | |
from contextlib import asynccontextmanager | |
from service_nursery import run_service_nursery | |
@asynccontextmanager | |
async def run_deadlock_service(): | |
class service: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
This tests shows a trio behavior that we did not anticipated. | |
When a cancellation is issued in trio, all the nurseries in the scope | |
are cancelled simultaneously. This can sometimes lead to confusing | |
errors, especially when those nurseries are hidden in context | |
managers. | |
This example shows the case of two services, implemented as an async | |
context manager running a background task. Then, we assume that the |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import trio | |
from contextlib import AsyncExitStack, asynccontextmanager | |
@asynccontextmanager | |
async def some_service(): | |
class Service(): | |
def __init__(self, nursery): | |
self.nursery = nursery |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![feature(generators, generator_trait)] | |
use std::ops::{Generator, GeneratorState}; | |
use std::pin::Pin; | |
// Generator to Iterator | |
struct GeneratorToIterator<G>(G); | |
impl<G> Iterator for GeneratorToIterator<G> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# distutils: language=c++ | |
from cython.operator cimport dereference | |
from libcpp.unordered_map cimport unordered_map | |
def primes(): | |
# Yield first two primes | |
yield 2 | |
yield 3 |