[Twisted-Python] txaio: write code for both asyncio + twisted
meejah
meejah at meejah.ca
Fri Apr 3 15:18:32 MDT 2015
I recently factored some Twisted/asyncio compatibility code out of
AutobahnPython (http://autobahn.ws/python/) and the result thus far is
"txaio", available on GitHub: https://github.com/tavendo/txaio
Similar to "six" (but for event-based Python) this allows you to write
code (presumably for a library) that can work unmodified on either
asyncio or Twisted. In other words, users of your library can choose
whether they want a Twisted dependency or not (and, e.g., get plain
Deferreds or Futures as appropriate from any async methods your API
reveals).
Basically, it's just a thin wrapper over the appropriate calls into
asyncio or Twisted providing a "lowest-common-denominator" interface.
This does mean limiting yourself a little (e.g. many DeferredList
options aren't available). So you feel this pain as a library author,
but then give *users* of your library a choice as to whether they wish
to write asyncio (with no Twisted dependency) or Twisted.
What I mean is: txaio isn't trying to be full event-loop interop like
txtulip (https://github.com/itamarst/txtulip)
This isn't "released" yet (e.g. not on PyPI) but I'm posting about it
because I'm interested in any early feedback.
Here's most of the API, using the example from the README:
import txaio # auto-selects twisted > asyncio > trollius
f0 = txaio.create_future()
f1 = txaio.as_future(some_func, 1, 2, key='word')
txaio.add_callbacks(f0, callback, errback) # either can be None
txaio.add_callbacks(f1, callback, errback)
# ...
txaio.resolve(f0, "value")
txaio.reject(f1, RuntimeError("it failed"))
--
meejah
More information about the Twisted-Python
mailing list