Dear diary, pandemic day 7,114. Placed a fancy hat on the fish. Questioned sanity, but voices reassure me I’m still sane. Treasonous cucumber sentenced to food processor, but asparagus now conspires too? Pez dispenser is all I can trust.
Vegetable coups aside, Illia and I migrated Stem to asyncio! Txtorcon was previously the only asynchronous game in town, but now Stem supports both synchronous and asynchronous usage.
import asyncio from stem.control import Controller async def print_version_async(): async with Controller.from_port() as controller: await controller.authenticate() print('[with asyncio] tor is version %s' % await controller.get_version()) def print_version_sync(): with Controller.from_port() as controller: controller.authenticate() print('[without asyncio] tor is version %s' % controller.get_version()) print_version_sync() asyncio.run(print_version_async())
% python demo.py [without asyncio] tor is version 0.4.5.0-alpha-dev (git-9d922b8eaae54242) [with asyncio] tor is version 0.4.5.0-alpha-dev (git-9d922b8eaae54242)
Internally Stem is now asynchronous from the bottom up. This provides deeper control over our execution, for example asyncio.wait_for() can apply a timeout to any of our methods.
Usually asyncio doesn’t play well with conventional usage, but Stem transparently applies a compatibility layer so synchronous users likely won’t even notice this change.