Skip to content
This repository has been archived by the owner on May 31, 2021. It is now read-only.

Latest commit

 

History

History
45 lines (34 loc) · 1.96 KB

twisted.rst

File metadata and controls

45 lines (34 loc) · 1.96 KB

Learn asyncio if you come from Twisted

The Twisted project is probably one of the oldest libraries that supports asynchronous programming in Python. It has been used by many programmers to develop a variety of applications. It supports many network protocols and can be used for many different types of network programming. In fact, asyncio was heavily inspired by Twisted. The expertise of several Twisted developers had been incorporated in asyncio. Soon, there will be a version of Twisted that is based on asyncio.

Rosetta Stone

This tables shows equivalent concepts in Twisted and asyncio.

Twisted asyncio
Deferred asyncio.Future
deferToThread(func) loop.run_in_executor(None, func)
@inlineCallbacks async def
reactor.run() loop.run_forever()

Deferred example

This small example shows two equivalent programs, one implemented in Twisted and one in asyncio.

Twisted asyncio

Basic Twisted example using deferred:

.. literalinclude:: examples/twisted_deferred.py

Similar example written using asyncio:

.. literalinclude:: examples/asyncio_deferred.py