Exploring Trio
Some time ago, between Python versions 3.6 and 3.11, an interesting alternative implementation of the event loop emerged from the community (specifically led by Nathaniel J. Smith) to handle particular cases in which asyncio shows some rough edges.Trio (https://trio.readthedocs.io/en/stable/index.html) gained attention, among other things, by implementing a ‘nursery’ mechanism. A detailed explanation of the problem that nurseries solve is available at https://vorpus.org/blog/notes-on-structured-concurrency-or-go-statement-considered-harmful/#nurseries-a-structured-replacement-for-go-statements. There it is stated that a parent thread should not let child tasks run without first creating a safe execution environment – the so-called nursery. Before going forward, let's adapt our asyncio source code for Trio:
import trio
import json
import queue
import httpx
from httpx_sse import connect_sse
max_vals = 10
vals = queue.Queue(maxsize=max_vals) ...