Crochet: Use Twisted Anywhere!

Crochet is an MIT-licensed library that makes it easier to use Twisted from regular blocking code. Some use cases include:

  • Easily use Twisted from a blocking framework like Django or Flask.
  • Write a library that provides a blocking API, but uses Twisted for its implementation.
  • Port blocking code to Twisted more easily, by keeping a backwards compatibility layer.
  • Allow normal Twisted programs that use threads to interact with Twisted more cleanly from their threaded parts. For example this can be useful when using Twisted as a WSGI container.

Crochet is maintained by Itamar Turner-Trauring.

Downloads are available on PyPI.

Documentation can be found on Read The Docs.

Bugs and feature requests should be filed at the project Github page.


Crochet aims for 100% unit test coverage, and supports Python 2.6, 2.7, 3.3 and 3.4 as well as PyPy.

Build Status

Crochet provides the following general features:

  • Allow blocking code to call into Twisted and block until results are available or a timeout is hit, using the crochet.wait_for decorator.
  • A lower-level API (crochet.run_in_reactor) allows blocking code to run code “in the background” in the Twisted thread, with ability to repeatedly check if it’s done.

Additionally Crochet can:

  • Transparently start Twisted’s reactor in a thread it manages.
  • The reactor shuts down automatically when the process’ main thread finishes.
  • Hooks up Twisted’s log system to the Python standard library logging framework. Unlike Twisted’s built-in logging bridge, this includes support for blocking Handler instances.


Background Scheduling

You can use Crochet to schedule events that will run in the background without slowing down the page rendering of your web applications:

An example of scheduling time-based events in the background.

Download the latest EUR/USD exchange rate from Yahoo every 30 seconds in the
background; the rendered Flask web page can use the latest value without
having to do the request itself.

Note this is example is for demonstration purposes only, and is not actually
used in the real world. You should not do this in a real application without
reading Yahoo's terms-of-service and following them.

from __future__ import print_function

from flask import Flask

from twisted.internet.task import LoopingCall
from twisted.web.client import getPage
from twisted.python import log

from crochet import wait_for, run_in_reactor, setup

# Twisted code:
class _ExchangeRate(object):
    """Download an exchange rate from Yahoo Finance using Twisted."""

    def __init__(self, name):
        self._value = None
        self._name = name

    # External API:
    def latest_value(self):
        """Return the latest exchange rate value.

        May be None if no value is available.
        return self._value

    def start(self):
        """Start the background process."""
        self._lc = LoopingCall(self._download)
        # Run immediately, and then every 30 seconds:
        self._lc.start(30, now=True)

    def _download(self):
        """Download the page."""
        def parse(result):
            print("Got %r back from Yahoo." % (result,))
            values = result.strip().split(",")
            self._value = float(values[1])
        d = getPage(
            % (self._name,))
        return d

# Blocking wrapper:
class ExchangeRate(object):
    """Blocking API for downloading exchange rate."""

    def __init__(self, name):
        self._exchange = _ExchangeRate(name)

    def start(self):

    def latest_value(self):
        """Return the latest exchange rate value.

        May be None if no value is available.
        return self._exchange.latest_value()

EURUSD = ExchangeRate("EURUSD")
app = Flask(__name__)

def index():
    rate = EURUSD.latest_value()
    if rate is None:
        rate = "unavailable, please refresh the page"
    return "Current EUR/USD exchange rate is %s." % (rate,)

if __name__ == '__main__':
    import sys, logging
    logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)

SSH into your Server

You can SSH into your Python process and get a Python prompt, allowing you to poke around in the internals of your running program:

A demonstration of Conch, allowing you to SSH into a running Python server and
inspect objects at a Python prompt.

If you're using the system install of Twisted, you may need to install Conch
separately, e.g. on Ubuntu:

   $ sudo apt-get install python-twisted-conch

Once you've started the program, you can ssh in by doing:

    $ ssh admin@localhost -p 5022

The password is 'secret'. Once you've reached the Python prompt, you have
access to the app object, and can import code, etc.:

    >>> 3 + 4
    >>> print(app)
    < object at 0x18e1690>


import logging

from flask import Flask
from crochet import setup, run_in_reactor

# Web server:
app = Flask(__name__)

def index():
    return "Welcome to my boring web server!"

def start_ssh_server(port, username, password, namespace):
    Start an SSH server on the given port, exposing a Python prompt with the
    given namespace.
    # This is a lot of boilerplate, see for a ticket to
    # provide a utility function that simplifies this.
    from twisted.internet import reactor
    from twisted.conch.insults import insults
    from twisted.conch import manhole, manhole_ssh
    from twisted.cred.checkers import (
        InMemoryUsernamePasswordDatabaseDontUse as MemoryDB)
    from twisted.cred.portal import Portal

    sshRealm = manhole_ssh.TerminalRealm()
    def chainedProtocolFactory():
        return insults.ServerProtocol(manhole.Manhole, namespace)
    sshRealm.chainedProtocolFactory = chainedProtocolFactory

    sshPortal = Portal(sshRealm, [MemoryDB(**{username: password})])
    reactor.listenTCP(port, manhole_ssh.ConchFactory(sshPortal),

if __name__ == '__main__':
    import sys
    logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)
    start_ssh_server(5022, "admin", "secret", {"app": app})

DNS Query

Twisted also has a fully featured DNS library:

A command-line application that uses Twisted to do an MX DNS query.

from __future__ import print_function

from twisted.names.client import lookupMailExchange
from crochet import setup, wait_for

# Twisted code:
def _mx(domain):
    Return Deferred that fires with a list of (priority, MX domain) tuples for
    a given domain.
    def got_records(result):
        return sorted(
            [(int(record.payload.preference), str(
             for record in result[0]])
    d = lookupMailExchange(domain)
    return d

# Blocking wrapper:
def mx(domain):
    Return list of (priority, MX domain) tuples for a given domain.
    return _mx(domain)

# Application code:
def main(domain):
    print("Mail servers for %s:" % (domain,))
    for priority, mailserver in mx(domain):
        print(priority, mailserver)

if __name__ == '__main__':
    import sys

Using Crochet in Normal Twisted Code

You can use Crochet’s APIs for calling into the reactor thread from normal Twisted applications:

An example of using Crochet from a normal Twisted application.

import sys

from crochet import no_setup, wait_for
# Tell Crochet not to run the reactor:

from twisted.internet import reactor
from twisted.python import log
from twisted.web.wsgi import WSGIResource
from twisted.web.server import Site
from twisted.names import client

# A WSGI application, will be run in thread pool:
def application(environ, start_response):
    start_response('200 OK', [])
        ip = gethostbyname('')
        return "%s has IP %s" % ('', ip)
    except Exception, e:
        return 'Error doing lookup: %s' % (e,)

# A blocking API that will be called from the WSGI application, but actually
# uses DNS:
def gethostbyname(name):
    d = client.lookupAddress(name)
    d.addCallback(lambda result: result[0][0].payload.dottedQuad())
    return d

# Normal Twisted code, serving the WSGI application and running the reactor:
def main():
    pool = reactor.getThreadPool()
    reactor.listenTCP(5000, Site(WSGIResource(reactor, pool, application)))

if __name__ == '__main__':