[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Support both asyncio and synchronous callers

Hi. I'm the author of Stem, Tor's python library [1]. Recently we
migrated to asyncio, but desire to still be usable by synchronous

We wrote a mixin [2][3] that transparently makes any class usable by
both asyncio and synchronous callers...


import asyncio
import stem.util.asyncio

class Example(stem.util.asyncio.Synchronous):
  async def hello(self):
    return 'hello'

def sync_demo():
  instance = Example()
  print('%s from a synchronous context' % instance.hello())

async def async_demo():
  instance = Example()
  print('%s from an asynchronous context' % await instance.hello())



Is there a better way to approach this?

Our wider ecosystem seem to be forking networking libraries into
sync/async variants, with requests [4] and AIOHTTP [5] as the most
prominent. Are there any libraries out there that demonstrate good
support for both kinds of callers without duplicating their API?

Thanks! -Damian

PS. I apologize if this email gets duplicated. Initially I used a
different email address which Mailman seemed to decline.