Crochet: Use Twisted Anywhere!

Crochet is an MIT-licensed library that makes it easier for blocking and threaded applications like Flask or Django to use the Twisted networking framework.

Here’s an example of a program using Crochet:

#!/usr/bin/python
"""
Download a web page in a blocking manner.
"""

from __future__ import print_function

import sys

from twisted.web.client import getPage
from crochet import setup, wait_for_reactor
setup()

@wait_for_reactor
def download_page(url):
    return getPage(url)

# download_page() now behaves like a normal blocking function:
print(download_page(sys.argv[1]))

Run on the command line:

$ python blockingdownload.py http://google.com
<!doctype html><html itemscope="itemscope" ... etc. ...

Notice that you get a completely blocking interface to Twisted and do not need to run the Twisted reactor, the event loop, yourself.